Ambient cleanup, ver. 2.18.1120

This commit is contained in:
Milan Hanajik
2019-01-23 08:26:53 +01:00
parent c6e1d672e5
commit 9bed5e6682
2 changed files with 89 additions and 101 deletions
+87 -99
View File
@@ -27,16 +27,13 @@ namespace TBF.BenchControl.Ambient.Comet
/// <summary>The state of the measurement</summary> /// <summary>The state of the measurement</summary>
MsrmntState msrmntState; MsrmntState msrmntState;
int msrmntTimeStamp;
///
/// Measured values when MsrmntState == MsrmntState.Valid /// Measured values when MsrmntState == MsrmntState.Valid
///
float temperature; /// [°C] float temperature; /// [°C]
float pressure; /// [bar] float pressure; /// [bar]
float humidity; /// [R%] float humidity; /// [R%]
/// <summary>Measurement time stamp when MsrmntState == MsrmntState.Valid</summary>
int msrmntTimeStamp;
public Ambient() public Ambient()
{ {
@@ -64,6 +61,7 @@ namespace TBF.BenchControl.Ambient.Comet
temperature = 20.0f; temperature = 20.0f;
humidity = 40.0f; humidity = 40.0f;
pressure = 1.0f; pressure = 1.0f;
msrmntTimeStamp = StateMachine.Time; msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid; msrmntState = MsrmntState.Valid;
return; return;
@@ -73,7 +71,9 @@ namespace TBF.BenchControl.Ambient.Comet
serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits); serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits);
serialPort.Handshake = ambientCfg.Handshake; serialPort.Handshake = ambientCfg.Handshake;
serialPort.Open(); serialPort.Open();
msrmntState = MsrmntState.Busy;
msrmntTimeStamp = 0;
msrmntState = MsrmntState.Busy;
log.FatalFormat("Successfully initialized device {0}", ToString()); log.FatalFormat("Successfully initialized device {0}", ToString());
} }
@@ -81,114 +81,102 @@ namespace TBF.BenchControl.Ambient.Comet
/// <summary>Run this device</summary> /// <summary>Run this device</summary>
public void RunDeviceBefore() public void RunDeviceBefore()
{ {
if (ambientCfg.DebugLevel == DebugMode.Simulate || if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null))
ambientCfg.DebugLevel == DebugMode.FailureDuringOperation) {
{ ///
msrmntTimeStamp = StateMachine.Time; /// Read response
return; ///
} try
{
int receivedBytes = serialPort.BytesToRead;
if (serialPort == null) if (receivedBytes >= 13)
{ {
log.ErrorFormat("{0} - RunDeviceBefore() - serial port closed", Name); byte[] byteArr = new byte[receivedBytes];
return; for (int i = 0; i < receivedBytes; i++) byteArr[i] = (byte)serialPort.ReadByte();
} log.Debug(Telegram.LogTelegram("Received ", byteArr));
try if ((receivedBytes == 13) && (byteArr[0] == 1) && (byteArr[1] == 3) && (byteArr[2] == 8) && Telegram.VerifyTelegramCRC(byteArr))
{ {
int receivedBytes = serialPort.BytesToRead; int value = (int)byteArr[3] * 256 + (int)byteArr[4];
temperature = (float)value / 10.0f;
if (receivedBytes >= 13) value = (int)byteArr[5] * 256 + (int)byteArr[6];
{ humidity = (float)value / 10.0f;
byte[] byteArr = new byte[receivedBytes];
for (int i = 0; i < receivedBytes; i++) byteArr[i] = (byte)serialPort.ReadByte();
log.Debug(Telegram.LogTelegram("Received ", byteArr));
if ((receivedBytes == 13) && (byteArr[0] == 1) && (byteArr[1] == 3) && (byteArr[2] == 8) && Telegram.VerifyTelegramCRC(byteArr)) value = (int)byteArr[9] * 256 + (int)byteArr[10];
{ pressure = (float)value / 10000.0f;
int value = (int)byteArr[3] * 256 + (int)byteArr[4];
temperature = (float)value / 10.0f;
value = (int)byteArr[5] * 256 + (int)byteArr[6]; msrmntTimeStamp = StateMachine.Time;
humidity = (float)value / 10.0f; msrmntState = MsrmntState.Valid;
log.InfoFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure);
value = (int)byteArr[9] * 256 + (int)byteArr[10]; }
pressure = (float)value / 10000.0f;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
log.InfoFormat("Ambient: temperature = {0:F1} C, humidity = {1:F1} %, pressure = {2:F0} mbar", temperature, humidity, 1000 * pressure);
} }
} }
} catch (Exception e)
catch (Exception e) {
{ DebugLevel = DebugMode.FailureDuringOperation;
DebugLevel = DebugMode.FailureDuringOperation; //serialPort.Close(); /// ?
serialPort = null; serialPort = null;
log.FatalFormat("Ambient: Serail port read failure : {0}", e.Message); log.FatalFormat("Ambient: Serial port read failure : {0}", e.Message);
if (e.InnerException != null) if (e.InnerException != null)
{ {
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message); log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
} }
} }
}
} }
/// <summary>Run this device</summary> /// <summary>Run this device</summary>
public void RunDeviceAfter() public void RunDeviceAfter()
{ {
if (ambientCfg.DebugLevel == DebugMode.Simulate || if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null))
ambientCfg.DebugLevel == DebugMode.FailureDuringOperation) {
{ if ((StateMachine.Time % 10) == 0)
return; {
} ///
/// Send request once per 10 seconds
///
try
{
/// Read four registers: 0x31, 0x32, 0x33, 0x34
UInt16 regAddr = 0x0030; /// register addr. = 0x31 (Modbus!)
UInt16 count = 4;
if (serialPort == null) /// Prepare data to be sent
{ byte[] msg = new byte[8];
log.ErrorFormat("{0} - RunDeviceAfter() - serial port closed", Name); msg[0] = 1; /// (byte)ambientCfg.BusAddress;
return; msg[1] = 3; /// CMD = Read registers
} msg[2] = (byte)(regAddr >> 8); /// Control Word Hi
msg[3] = (byte)(regAddr & 0xFF); /// Control Word Lo
msg[4] = (byte)(count >> 8); /// Serial Com Reference Hi
msg[5] = (byte)(count & 0xFF); /// Serial Com Reference Lo
Telegram.UpdateTelegramCRC(msg);
try log.Debug(Telegram.LogTelegram("Sending ", msg));
{ serialPort.Write(msg, 0, msg.Length);
/// Each 10 seconds }
if ((StateMachine.Time % 10) == 0) catch (Exception e)
{ {
/// Read four registers: 0x31, 0x32, 0x33, 0x34 DebugLevel = DebugMode.FailureDuringOperation;
UInt16 regAddr = 0x0030; /// register addr. = 0x31 (Modbus!) //serialPort.Close(); /// ?
UInt16 count = 4; serialPort = null;
/// Prepare data to be sent log.FatalFormat("Ambient: Serail port read failure : {0}", e.Message);
byte[] msg = new byte[8]; if (e.InnerException != null)
msg[0] = 1; /// (byte)ambientCfg.BusAddress; {
msg[1] = 3; /// CMD = Read registers log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
msg[2] = (byte)(regAddr >> 8); /// Control Word Hi }
msg[3] = (byte)(regAddr & 0xFF); /// Control Word Lo }
msg[4] = (byte)(count >> 8); /// Serial Com Reference Hi }
msg[5] = (byte)(count & 0xFF); /// Serial Com Reference Lo }
Telegram.UpdateTelegramCRC(msg);
log.Debug(Telegram.LogTelegram("Sending ", msg));
serialPort.Write(msg, 0, msg.Length);
}
}
catch (Exception e)
{
DebugLevel = DebugMode.FailureDuringOperation;
serialPort = null;
log.FatalFormat("Ambient: Serail port read failure : {0}", e.Message);
if (e.InnerException != null)
{
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
}
}
} }
/// <summary>Stop this device</summary> /// <summary>Stop this device</summary>
public void StopDevice() public void StopDevice()
{ {
if (serialPort != null) if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null))
{ {
serialPort.Close(); serialPort.Close();
serialPort = null; serialPort = null;
@@ -206,18 +194,18 @@ namespace TBF.BenchControl.Ambient.Comet
public IOperation ReadAmbientOp(FloatBox temperature, FloatBox pressure, FloatBox humidity) public IOperation ReadAmbientOp(FloatBox temperature, FloatBox pressure, FloatBox humidity)
{ {
this.tempBox = temperature; this.tempBox = temperature;
this.pressureBox = pressure; this.pressureBox = pressure;
this.humiBox = humidity; this.humiBox = humidity;
return this; return this;
} }
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() public void Start()
{ {
if (tempBox != null) tempBox.Val = temperature; if (tempBox != null) tempBox.Val = temperature;
if (pressureBox != null) pressureBox.Val = pressure; if (pressureBox != null) pressureBox.Val = pressure;
if (humiBox != null) humiBox.Val = humidity; if (humiBox != null) humiBox.Val = humidity;
} }
/// <summary>Run this operation</summary> /// <summary>Run this operation</summary>
@@ -226,9 +214,9 @@ namespace TBF.BenchControl.Ambient.Comet
/// </returns> /// </returns>
public Event Run() public Event Run()
{ {
if (tempBox != null) tempBox.Val = temperature; if (tempBox != null) tempBox.Val = temperature;
if (pressureBox != null) pressureBox.Val = pressure; if (pressureBox != null) pressureBox.Val = pressure;
if (humiBox != null) humiBox.Val = humidity; if (humiBox != null) humiBox.Val = humidity;
return Event.AmbientDone; return Event.AmbientDone;
} }
+2 -2
View File
@@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// Build Number // Build Number
// Revision // Revision
// //
[assembly: AssemblyVersion("2.18.1119.0")] [assembly: AssemblyVersion("2.18.1120.0")]
[assembly: AssemblyFileVersion("2.18.1119.0")] [assembly: AssemblyFileVersion("2.18.1120.0")]