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>
MsrmntState msrmntState;
int msrmntTimeStamp;
///
/// Measured values when MsrmntState == MsrmntState.Valid
///
float temperature; /// [°C]
float pressure; /// [bar]
float humidity; /// [R%]
/// <summary>Measurement time stamp when MsrmntState == MsrmntState.Valid</summary>
int msrmntTimeStamp;
public Ambient()
{
@@ -64,6 +61,7 @@ namespace TBF.BenchControl.Ambient.Comet
temperature = 20.0f;
humidity = 40.0f;
pressure = 1.0f;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
return;
@@ -73,7 +71,9 @@ namespace TBF.BenchControl.Ambient.Comet
serialPort = new SerialPort(comPortName, ambientCfg.BaudRate, ambientCfg.Parity, ambientCfg.DataBits, ambientCfg.StopBits);
serialPort.Handshake = ambientCfg.Handshake;
serialPort.Open();
msrmntState = MsrmntState.Busy;
msrmntTimeStamp = 0;
msrmntState = MsrmntState.Busy;
log.FatalFormat("Successfully initialized device {0}", ToString());
}
@@ -81,114 +81,102 @@ namespace TBF.BenchControl.Ambient.Comet
/// <summary>Run this device</summary>
public void RunDeviceBefore()
{
if (ambientCfg.DebugLevel == DebugMode.Simulate ||
ambientCfg.DebugLevel == DebugMode.FailureDuringOperation)
{
msrmntTimeStamp = StateMachine.Time;
return;
}
if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null))
{
///
/// Read response
///
try
{
int receivedBytes = serialPort.BytesToRead;
if (serialPort == null)
{
log.ErrorFormat("{0} - RunDeviceBefore() - serial port closed", Name);
return;
}
if (receivedBytes >= 13)
{
byte[] byteArr = new byte[receivedBytes];
for (int i = 0; i < receivedBytes; i++) byteArr[i] = (byte)serialPort.ReadByte();
log.Debug(Telegram.LogTelegram("Received ", byteArr));
try
{
int receivedBytes = serialPort.BytesToRead;
if ((receivedBytes == 13) && (byteArr[0] == 1) && (byteArr[1] == 3) && (byteArr[2] == 8) && Telegram.VerifyTelegramCRC(byteArr))
{
int value = (int)byteArr[3] * 256 + (int)byteArr[4];
temperature = (float)value / 10.0f;
if (receivedBytes >= 13)
{
byte[] byteArr = new byte[receivedBytes];
for (int i = 0; i < receivedBytes; i++) byteArr[i] = (byte)serialPort.ReadByte();
log.Debug(Telegram.LogTelegram("Received ", byteArr));
value = (int)byteArr[5] * 256 + (int)byteArr[6];
humidity = (float)value / 10.0f;
if ((receivedBytes == 13) && (byteArr[0] == 1) && (byteArr[1] == 3) && (byteArr[2] == 8) && Telegram.VerifyTelegramCRC(byteArr))
{
int value = (int)byteArr[3] * 256 + (int)byteArr[4];
temperature = (float)value / 10.0f;
value = (int)byteArr[9] * 256 + (int)byteArr[10];
pressure = (float)value / 10000.0f;
value = (int)byteArr[5] * 256 + (int)byteArr[6];
humidity = (float)value / 10.0f;
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);
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)
{
DebugLevel = DebugMode.FailureDuringOperation;
serialPort = null;
}
catch (Exception e)
{
DebugLevel = DebugMode.FailureDuringOperation;
//serialPort.Close(); /// ?
serialPort = null;
log.FatalFormat("Ambient: Serail port read failure : {0}", e.Message);
if (e.InnerException != null)
{
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
}
}
log.FatalFormat("Ambient: Serial port read failure : {0}", e.Message);
if (e.InnerException != null)
{
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
}
}
}
}
/// <summary>Run this device</summary>
public void RunDeviceAfter()
{
if (ambientCfg.DebugLevel == DebugMode.Simulate ||
ambientCfg.DebugLevel == DebugMode.FailureDuringOperation)
{
return;
}
if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null))
{
if ((StateMachine.Time % 10) == 0)
{
///
/// 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)
{
log.ErrorFormat("{0} - RunDeviceAfter() - serial port closed", Name);
return;
}
/// Prepare data to be sent
byte[] msg = new byte[8];
msg[0] = 1; /// (byte)ambientCfg.BusAddress;
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
{
/// Each 10 seconds
if ((StateMachine.Time % 10) == 0)
{
/// Read four registers: 0x31, 0x32, 0x33, 0x34
UInt16 regAddr = 0x0030; /// register addr. = 0x31 (Modbus!)
UInt16 count = 4;
log.Debug(Telegram.LogTelegram("Sending ", msg));
serialPort.Write(msg, 0, msg.Length);
}
catch (Exception e)
{
DebugLevel = DebugMode.FailureDuringOperation;
//serialPort.Close(); /// ?
serialPort = null;
/// Prepare data to be sent
byte[] msg = new byte[8];
msg[0] = 1; /// (byte)ambientCfg.BusAddress;
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);
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);
}
}
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>
public void StopDevice()
{
if (serialPort != null)
if ((ambientCfg.DebugLevel == DebugMode.Normal) && (serialPort != null))
{
serialPort.Close();
serialPort = null;
@@ -206,18 +194,18 @@ namespace TBF.BenchControl.Ambient.Comet
public IOperation ReadAmbientOp(FloatBox temperature, FloatBox pressure, FloatBox humidity)
{
this.tempBox = temperature;
this.tempBox = temperature;
this.pressureBox = pressure;
this.humiBox = humidity;
this.humiBox = humidity;
return this;
}
/// <summary>Start this operation</summary>
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;
}
/// <summary>Run this operation</summary>
@@ -226,9 +214,9 @@ namespace TBF.BenchControl.Ambient.Comet
/// </returns>
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;
}
+2 -2
View File
@@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// Build Number
// Revision
//
[assembly: AssemblyVersion("2.18.1119.0")]
[assembly: AssemblyFileVersion("2.18.1119.0")]
[assembly: AssemblyVersion("2.18.1120.0")]
[assembly: AssemblyFileVersion("2.18.1120.0")]