Я хочу создать библиотечный класс для внешней цепи управления, которая взаимодействует через последовательный порт. Схема имеет встроенные функции для получения / установки различных настроек с использованием последовательной связи (например, отправка "SR, HC, 01,1, \ r" включает датчик 1 в положение ON). Существует около 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.
}
}
Я подумал, что таким образом не будет 100 методов / свойств, втиснутых в класс CircuitController
. Я мог бы использовать свойство get, например, для получения экземпляра sensorSettings, а затем вызвать нужный метод / свойство: circuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1);
. Я получаю сообщение об ошибке компиляции, к которому я пытаюсь получить доступ SendReceive()
из вложенного класса. Есть ли способ сделать это?
Спасибо!