diff --git a/TBF/Rig/Ambient/Comet/Ambient.cs b/TBF/Rig/Ambient/Comet/Ambient.cs index dc663c344..374b29fba 100644 --- a/TBF/Rig/Ambient/Comet/Ambient.cs +++ b/TBF/Rig/Ambient/Comet/Ambient.cs @@ -21,7 +21,7 @@ namespace TBF.Rig.Ambient.Comet public class Ambient : ComponentBase, IDevice, IOperation, GenericDevices.IAmbient, IDrawingItCmpntWithMeasuredVal { private static readonly ILog log = LogManager.GetLogger(typeof(Ambient)); - public override string ToString() { return string.Format("Ambient({0})", Cfg.ToString(-1)); } + public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } /// /// Configuration and wrappers @@ -51,7 +51,8 @@ namespace TBF.Rig.Ambient.Comet public double MeasuredVal { get { return temperature; } } public string AltString { - get { + get + { return string.Format(ambientCfg.Format, Units.ConvertTo(TempUnit, temperature), Units.ConvertTo(PressureUnit, pressure), Units.ConvertTo(HumiUnit, humidity)); @@ -62,62 +63,48 @@ namespace TBF.Rig.Ambient.Comet /// Private fields SerialPort serialPort; - /// The state of the measurement - MsrmntState msrmntState; - int msrmntTimeStamp; - /// Measured values when MsrmntState == MsrmntState.Valid double temperature; /// [°C] double pressure; /// [bar] double humidity; /// [R%] + /// Time stamp of the measurement + int msrmntTimeStamp; - public Ambient() - { - } + + public Ambient() { } /// /// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232) /// - public Ambient(AmbientCfg cfg, IList components) - : this(cfg) - { - } - - public Ambient(Generic.IComponentCfg cfg) + public Ambient(Generic.IComponentCfg cfg) : base(cfg) { ambientCfg = cfg as AmbientCfg; - - temperature = DefaultTemperature; - pressure = DefaultPressure; - humidity = DefaultHumidity; - log.Warn(this.ToString()); } public void Initialize() { - if (ambientCfg.DebugLevel == DebugMode.Simulate) - { - msrmntTimeStamp = StateMachine.Time; - msrmntState = MsrmntState.Valid; - return; - } - - string comPortName = "COM" + ambientCfg.ComPortNr.ToString(); - serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits); - serialPort.Handshake = ambientCfg.Handshake; - serialPort.Open(); - if (ambientCfg.SetDtrToOne) - { - serialPort.DtrEnable = true; - } - + temperature = DefaultTemperature; + pressure = DefaultPressure; + humidity = DefaultHumidity; msrmntTimeStamp = 0; - msrmntState = MsrmntState.Busy; - log.FatalFormat("Successfully initialized device {0}", ToString()); + if (ambientCfg.DebugLevel == DebugMode.Normal) + { + string comPortName = "COM" + ambientCfg.ComPortNr.ToString(); + serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits); + serialPort.Handshake = ambientCfg.Handshake; + serialPort.Open(); + if (ambientCfg.SetDtrToOne) serialPort.DtrEnable = true; + log.FatalFormat("{0} - Device successfully initialized", Name); + } + else + { + serialPort = null; + log.FatalFormat("{0} - Device simulated", Name); + } } /// Run this device @@ -150,7 +137,6 @@ namespace TBF.Rig.Ambient.Comet pressure = value / 10000.0; msrmntTimeStamp = StateMachine.Time; - msrmntState = MsrmntState.Valid; UpdateProcessData(temperature, pressure, humidity); @@ -221,15 +207,12 @@ namespace TBF.Rig.Ambient.Comet /// Stop this device public void StopDevice() { - if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null)) - { - serialPort.Close(); - serialPort = null; - } + if (serialPort != null && serialPort.IsOpen) serialPort.Close(); } public void StopDevice2() { } + void UpdateProcessData(double temperature, double pressure, double humidity) { TBF.Rig.Sequences.ProcessData.AmbTemp.Val = temperature; @@ -256,9 +239,9 @@ namespace TBF.Rig.Ambient.Comet /// Start this operation public void Start() { - if (tempBox != null) tempBox.Val = temperature; + if (tempBox != null) tempBox.Val = temperature; if (pressureBox != null) pressureBox.Val = pressure; - if (humiBox != null) humiBox.Val = humidity; + if (humiBox != null) humiBox.Val = humidity; } /// Run this operation @@ -267,9 +250,9 @@ namespace TBF.Rig.Ambient.Comet /// public Event Run() { - if (tempBox != null) tempBox.Val = temperature; + if (tempBox != null) tempBox.Val = temperature; if (pressureBox != null) pressureBox.Val = pressure; - if (humiBox != null) humiBox.Val = humidity; + if (humiBox != null) humiBox.Val = humidity; return Event.AmbientDone; } diff --git a/TBF/Rig/Ambient/Comet/Factory.cs b/TBF/Rig/Ambient/Comet/Factory.cs index 6438a4e62..072f21c0c 100644 --- a/TBF/Rig/Ambient/Comet/Factory.cs +++ b/TBF/Rig/Ambient/Comet/Factory.cs @@ -15,7 +15,7 @@ namespace TBF.Rig.Ambient.Comet public IComponent DummyComponent() { return new Ambient(); } - public IComponent GetComponent(IComponentCfg cfg, IList components) { return new Ambient(cfg); } + public IComponent GetComponent(IComponentCfg cfg, IList components) { return new Ambient(cfg); } public IComponentCfg DefaultConfig() { return new AmbientCfg("Ambient", this); } diff --git a/TBF/Rig/Ambient/Greco/Ambient.cs b/TBF/Rig/Ambient/Greco/Ambient.cs index 50941591a..eb18a7449 100644 --- a/TBF/Rig/Ambient/Greco/Ambient.cs +++ b/TBF/Rig/Ambient/Greco/Ambient.cs @@ -22,7 +22,7 @@ namespace TBF.Rig.Ambient.Greco public class Ambient : ComponentBase, IDevice, IOperation, GenericDevices.IAmbient, IDrawingItCmpntWithMeasuredVal { private static readonly ILog log = LogManager.GetLogger(typeof(Ambient)); - public override string ToString() { return string.Format("Ambient({0})", Cfg.ToString(-1)); } + public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } private readonly AmbientCfg ambientCfg; /// @@ -49,7 +49,8 @@ namespace TBF.Rig.Ambient.Greco public double MeasuredVal { get { return temperature; } } public string AltString { - get { + get + { return string.Format(ambientCfg.Format, Units.ConvertTo(TempUnit, temperature), Units.ConvertTo(PressureUnit, pressure), Units.ConvertTo(HumiUnit, humidity)); @@ -60,9 +61,6 @@ namespace TBF.Rig.Ambient.Greco SerialPort serialPort; StringBuilder measurementBuilder; - /// The state of the measurement - MsrmntState msrmntState; - /// /// Measured values when MsrmntState == MsrmntState.Valid /// @@ -73,48 +71,41 @@ namespace TBF.Rig.Ambient.Greco /// Measurement time stamp when MsrmntState == MsrmntState.Valid int msrmntTimeStamp; - public Ambient() - { - } + + public Ambient() { } /// /// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232) /// Connection settings: 19200 Bd 8-bits No-parity 1-stop-bit Flow control: none or hardware. /// - public Ambient(AmbientCfg cfg, IList components) - : this(cfg) - { - } - - public Ambient(Generic.IComponentCfg cfg) + public Ambient(Generic.IComponentCfg cfg) : base(cfg) { ambientCfg = cfg as AmbientCfg; - - temperature = DefaultTemperature; - pressure = DefaultPressure; - humidity = DefaultHumidity; - log.Warn(this.ToString()); } public void Initialize() { - if (ambientCfg.DebugLevel == DebugMode.Simulate) - { - msrmntTimeStamp = StateMachine.Time; - msrmntState = MsrmntState.Valid; - return; - } + temperature = DefaultTemperature; + pressure = DefaultPressure; + humidity = DefaultHumidity; + msrmntTimeStamp = 0; - string comPortName = "COM" + ambientCfg.ComPortNr.ToString(); - serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits); - serialPort.Handshake = ambientCfg.Handshake; - serialPort.Open(); - measurementBuilder = new StringBuilder(40); - msrmntState = MsrmntState.Busy; - - log.FatalFormat("Successfully initialized device {0}", ToString()); + if (ambientCfg.DebugLevel == DebugMode.Normal) + { + string comPortName = "COM" + ambientCfg.ComPortNr.ToString(); + serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits); + serialPort.Handshake = ambientCfg.Handshake; + serialPort.Open(); + measurementBuilder = new StringBuilder(40); + log.FatalFormat("{0} - Device successfully initialized", Name); + } + else + { + serialPort = null; + log.FatalFormat("{0} - Device simulated", Name); + } } /// Run this device @@ -170,7 +161,6 @@ namespace TBF.Rig.Ambient.Greco UpdateProcessData(temperature, pressure, humidity); msrmntTimeStamp = StateMachine.Time; - msrmntState = MsrmntState.Valid; log.InfoFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure); } else @@ -193,18 +183,17 @@ namespace TBF.Rig.Ambient.Greco } /// Run this device - public void RunDeviceAfter() - { - } + public void RunDeviceAfter() { } /// Stop this device public void StopDevice() { - if (serialPort != null) serialPort.Close(); + if (serialPort != null && serialPort.IsOpen) serialPort.Close(); } public void StopDevice2() { } + void UpdateProcessData(double temperature, double pressure, double humidity) { TBF.Rig.Sequences.ProcessData.AmbTemp.Val = temperature; diff --git a/TBF/Rig/Ambient/Greco/Factory.cs b/TBF/Rig/Ambient/Greco/Factory.cs index 97e64276f..345b2adb1 100644 --- a/TBF/Rig/Ambient/Greco/Factory.cs +++ b/TBF/Rig/Ambient/Greco/Factory.cs @@ -15,7 +15,7 @@ namespace TBF.Rig.Ambient.Greco public IComponent DummyComponent() { return new Ambient(); } - public IComponent GetComponent(IComponentCfg cfg, IList components) { return new Ambient(cfg); } + public IComponent GetComponent(IComponentCfg cfg, IList components) { return new Ambient(cfg); } public IComponentCfg DefaultConfig() { return new AmbientCfg("Ambient", this); } diff --git a/TBF/Rig/Generic/IDevice.cs b/TBF/Rig/Generic/IDevice.cs index 811381392..8d36d8945 100644 --- a/TBF/Rig/Generic/IDevice.cs +++ b/TBF/Rig/Generic/IDevice.cs @@ -5,7 +5,12 @@ namespace TBF.Rig.Generic { public interface IDevice : IComponent { - void Initialize(); + /// + /// Invoked once on program start-up before the state machine is started, + /// before RunDeviceBefore() functions of all devices and before Start() + /// functions of all operations. + /// + void Initialize(); /// /// This function is invoked regularly each 1000 ms (once per second) @@ -30,8 +35,9 @@ namespace TBF.Rig.Generic void RunDeviceAfter(); /// - /// Should be invoked once on program shut-down after state machine is stopped - /// and after all Stop() functions for all runing operations were called. + /// Invoked once on program shut-down after state machine is stopped, + /// after all Stop() functions for all runing operations were called + /// and after all RunDeviceAfter() of all devices. /// void StopDevice(); diff --git a/TBF/Rig/Modbus/Ambient/Comet/Ambient.cs b/TBF/Rig/Modbus/Ambient/Comet/Ambient.cs index bd2340eb2..f32aaceb4 100644 --- a/TBF/Rig/Modbus/Ambient/Comet/Ambient.cs +++ b/TBF/Rig/Modbus/Ambient/Comet/Ambient.cs @@ -19,7 +19,7 @@ namespace TBF.Rig.Modbus.Ambient.Comet public class Ambient : ComponentBase, IDevice, IOperation, GenericDevices.IAmbient, IDrawingItCmpntWithMeasuredVal { private static readonly ILog log = LogManager.GetLogger(typeof(Ambient)); - public override string ToString() { return string.Format("Ambient({0})", Cfg.ToString(-1)); } + public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } /// /// Configuration and wrappers @@ -50,18 +50,15 @@ namespace TBF.Rig.Modbus.Ambient.Comet public double MeasuredVal { get { return temperature; } } public string AltString { - get { + get + { return string.Format(ambientCfg.Format, Units.ConvertTo(TempUnit, temperature), Units.ConvertTo(PressureUnit, pressure), Units.ConvertTo(HumiUnit, humidity)); } } - - - readonly GenericDevices.IModbus modbus; - /// The state of the measurement - MsrmntState msrmntState; + readonly Common.Modbus modbus; /// /// Measured values when MsrmntState == MsrmntState.Valid @@ -87,41 +84,25 @@ namespace TBF.Rig.Modbus.Ambient.Comet { ambientCfg = cfg as AmbientCfg; - modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components); + modbus = TbfComponents.FindComponent(cfg.ParentName, components) as Common.Modbus; if (modbus == null) throw new Exception("Cannot find " + Name + " parent"); - temperature = DefaultTemperature; - pressure = DefaultPressure; - humidity = DefaultHumidity; - log.Warn(this.ToString()); } public void Initialize() { - if (ambientCfg.DebugLevel == DebugMode.Simulate) - { - msrmntTimeStamp = StateMachine.Time; - msrmntState = MsrmntState.Valid; - return; - } - - msrmntState = MsrmntState.Busy; + temperature = DefaultTemperature; + pressure = DefaultPressure; + humidity = DefaultHumidity; + msrmntTimeStamp = 0; ticketNumber = modbus.RegisterForPolling(); - - log.FatalFormat("Successfully initialized device {0}", ToString()); + log.FatalFormat("{0} - Device successfully initialized", Name); } /// Run this device public void RunDeviceBefore() { - if (ambientCfg.DebugLevel == DebugMode.Simulate || - ambientCfg.DebugLevel == DebugMode.FailureDuringOperation) - { - msrmntTimeStamp = StateMachine.Time; - return; - } - if (modbus.ReceivedTelegrams[ambientCfg.ModbusAddress].Count > 0) { byte[] telegram = modbus.ReceivedTelegrams[ambientCfg.ModbusAddress].Dequeue(); @@ -138,7 +119,6 @@ namespace TBF.Rig.Modbus.Ambient.Comet pressure = value / 10000.0; msrmntTimeStamp = StateMachine.Time; - msrmntState = MsrmntState.Valid; UpdateProcessData(temperature, pressure, humidity); @@ -149,9 +129,7 @@ namespace TBF.Rig.Modbus.Ambient.Comet public void RunDeviceAfter() { - if (ambientCfg.DebugLevel != Config.Entities.DebugMode.Simulate && - ambientCfg.DebugLevel != Config.Entities.DebugMode.FailureDuringOperation && - modbus.IsMyTurn(ticketNumber)) + if (modbus.IsMyTurn(ticketNumber)) { /// Read four registers: 0x31, 0x32, 0x33, 0x34 UInt16 regAddr = 0x0030; /// register addr. = 0x31 (Modbus!)