Program is protected against Ambient sensor serial port failure, version 1.5.120.

This commit is contained in:
Milan Hanajik 2015-09-22 11:28:48 +02:00
parent b3dc49e8b7
commit f203c56e8b
4 changed files with 133 additions and 86 deletions

View File

@ -80,63 +80,96 @@ namespace TBF.BenchControl.Ambient.Comet
/// <summary>Run this device</summary>
public void RunDeviceBefore()
{
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate)
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate ||
ambientCfg.DebugLevel == Entities.DebugMode.FailureDuringOperation)
{
msrmntTimeStamp = StateMachine.Time;
return;
}
int receivedBytes = serialPort.BytesToRead;
if (receivedBytes >= 13)
try
{
byte[] byteArr = new byte[receivedBytes];
for (int i = 0; i < receivedBytes; i++) byteArr[i] = (byte)serialPort.ReadByte();
log.Debug( Telegram.LogTelegram("Received ", byteArr));
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 / 10.0f;
value = (int)byteArr[5] * 256 + (int)byteArr[6];
humidity = (float)value / 10.0f;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
log.InfoFormat("Ambient: temperature = {0} C, humidity = {1} %, pressure = {2} hPa", temperature.ToString("F1"), humidity.ToString("F1"), pressure.ToString("F0"));
}
value = (int)byteArr[9] * 256 + (int)byteArr[10];
pressure = (float)value / 10.0f;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
log.InfoFormat("Ambient: temperature = {0} C, humidity = {1} %, pressure = {2} hPa", temperature.ToString("F1"), humidity.ToString("F1"), pressure.ToString("F0"));
}
}
}
}
catch (Exception e)
{
DebugLevel = Entities.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>Run this device</summary>
public void RunDeviceAfter()
{
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate) return;
/// 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;
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate ||
ambientCfg.DebugLevel == Entities.DebugMode.FailureDuringOperation)
{
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);
}
/// 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 = Entities.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>

View File

@ -82,58 +82,73 @@ namespace TBF.BenchControl.Ambient.Greco
/// <summary>Run this device</summary>
public void RunDeviceBefore()
{
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate)
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate ||
ambientCfg.DebugLevel == Entities.DebugMode.FailureDuringOperation)
{
msrmntTimeStamp = StateMachine.Time;
return;
}
measurementBuilder.Append(serialPort.ReadExisting());
int len = measurementBuilder.Length;
///
/// Device sends each 20 sec one line of ASCII text with data in the following format:
/// (temp) 6 characters, two decimal digits, with leading spaces
/// TAB 1 character
/// (humi) 6 characters, two decimal digits, with leading spaces
/// TAB 1 character
/// (pressure) 6 characters, one decimal digits, with leading spaces is LT 1000
/// CR + LF 2 characters
/// Example:
/// 23.42 38.08 1061.1
/// 23.39 38.04 1060.8
/// 23.39 38.08 1061.4
///
if (len >= 22)
try
{
string measurement = measurementBuilder.ToString();
measurementBuilder.Clear();
measurementBuilder.Append(serialPort.ReadExisting());
int len = measurementBuilder.Length;
float t = 0;
float h = 0;
float p = 0;
if (float.TryParse(measurement.Substring(len - 22, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
CultureInfo.InvariantCulture, out t) &&
measurement[len - 16] == '\t' &&
float.TryParse(measurement.Substring(len - 15, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
CultureInfo.InvariantCulture, out h) &&
measurement[len - 9] == '\t' &&
float.TryParse(measurement.Substring(len - 8, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
CultureInfo.InvariantCulture, out p) &&
measurement[len - 2] == '\r' &&
measurement[len - 1] == '\n')
///
/// Device sends each 20 sec one line of ASCII text with data in the following format:
/// (temp) 6 characters, two decimal digits, with leading spaces
/// TAB 1 character
/// (humi) 6 characters, two decimal digits, with leading spaces
/// TAB 1 character
/// (pressure) 6 characters, one decimal digits, with leading spaces is LT 1000
/// CR + LF 2 characters
/// Example:
/// 23.42 38.08 1061.1
/// 23.39 38.04 1060.8
/// 23.39 38.08 1061.4
///
if (len >= 22)
{
temperature = t;
humidity = h;
pressure = p;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
string measurement = measurementBuilder.ToString();
measurementBuilder.Clear();
log.InfoFormat("Ambient: temperature = {0} C, humidity = {1} %, pressure = {2} hPa", temperature.ToString("F1"), humidity.ToString("F1"), pressure.ToString("F0"));
}
else
float t = 0;
float h = 0;
float p = 0;
if (float.TryParse(measurement.Substring(len - 22, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
CultureInfo.InvariantCulture, out t) &&
measurement[len - 16] == '\t' &&
float.TryParse(measurement.Substring(len - 15, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
CultureInfo.InvariantCulture, out h) &&
measurement[len - 9] == '\t' &&
float.TryParse(measurement.Substring(len - 8, 6), NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingWhite,
CultureInfo.InvariantCulture, out p) &&
measurement[len - 2] == '\r' &&
measurement[len - 1] == '\n')
{
temperature = t;
humidity = h;
pressure = p;
msrmntTimeStamp = StateMachine.Time;
msrmntState = MsrmntState.Valid;
log.InfoFormat("Ambient: temperature = {0} C, humidity = {1} %, pressure = {2} hPa", temperature.ToString("F1"), humidity.ToString("F1"), pressure.ToString("F0"));
}
else
{
log.Warn("Invalid string received: " + measurement);
}
}
}
catch (Exception e)
{
DebugLevel = Entities.DebugMode.FailureDuringOperation;
serialPort = null;
log.FatalFormat("Ambient: Serail port read failure : {0}", e.Message);
if (e.InnerException != null)
{
log.Warn("Invalid string received: " + measurement);
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
}
}
}

View File

@ -313,12 +313,11 @@ namespace TBF.BenchControl.WaterMeters.iPerl
{
DebugLevel = Entities.DebugMode.FailureDuringOperation;
log.FatalFormat("Redrawing results failed : {0}", e.Message);
log.FatalFormat("Opto-data serial port failure : {0}", e.Message);
if (e.InnerException != null)
{
log.FatalFormat("InnerMessage : {0}", e.InnerException.Message);
}
log.FatalFormat("StackTrace : {0}{1}", Environment.NewLine, e.StackTrace);
}
}
else if (DebugLevel == Entities.DebugMode.FailureDuringOperation)

View File

@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.5.119.1")]
[assembly: AssemblyFileVersion("1.5.119.1")]
[assembly: AssemblyVersion("1.5.120.1")]
[assembly: AssemblyFileVersion("1.5.120.1")]