我想为外部控制电路创建一个库类,通过串口进行通信。该电路具有内置功能,以使用串行通信获得/设置各种设置(例如,发送“SR,HC,01,1,\ r”将传感器1打开)。有大约100个功能分为以下类别:传感器设置,输出设置和环境设置。这是我试过的。
public class CircuitController
{
// Fields.
private SerialPort controllerSerialPort;
private SensorSettings sensorSettings;
private OutputSettings outputSettings;
private EnvironmentSettings environmentSettings;
...
// Properties.
// Properties to get sensorSettings, outputSettings, and environmentSettings.
// Methods.
public string SendReceive(string sendCommand) // Send command and receive response.
{
...
}
// Nested classes.
public class SensorSettings
{
// Fields.
// The various sensor settings here.
// Properties.
// Properties to get/set the sensor settings. Note: Get/Set is done through calling one of the following methods.
// Methods.
public double GetSensorUnits(int sensorNumber)
{
...
string commandToSend = String.Format("HE,WL,1,{0}", sensorNumber); // Setup command string.
string commandResponse = SendReceive(commandToSend); // Send command and receive response. ERROR here, cannot access higher level, non-static methods.
// Logic to process commandResponse.
...
}
// Other methods to create, send, and process the circuit's sensor settings "functions".
}
public class OutputSettings
{
// Same logic as SensorSettings class.
}
public class EnvironmentSettings
{
// Same logic as SensorSettings class.
}
}
我认为这样就不会在CircuitController
类下填充100个方法/属性。例如,我可以使用get属性来获取sensorSettings实例,然后调用所需的方法/属性:circuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1);
。我收到一个编译错误,我试图从嵌套类访问SendReceive()
。有没有办法做到这一点?
谢谢!
答案 0 :(得分:1)
嵌套类不会“看到”其主机中声明的内容。
您应该将主机引用传递给任何嵌套类,例如,在构造函数中。