188 lines
5.5 KiB
C#
188 lines
5.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO.Ports;
|
|
using System.Text;
|
|
using log4net;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.Greco
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public class Ambient : TestBenchSim, IComponent, IDevice, IOperation, GenericDevices.IAmbient
|
|
{
|
|
/// <summary>
|
|
/// Info: Successful measurement
|
|
/// Warn: Invalid serial response format
|
|
/// </summary>
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Ambient));
|
|
|
|
public static void ResetStaticProperties() { }
|
|
|
|
private readonly AmbientCfg ambientCfg;
|
|
public Generic.IComponentCfg Cfg { get { return ambientCfg; } }
|
|
|
|
public string Name { get { return ambientCfg.Name; } }
|
|
|
|
/// Private fields
|
|
SerialPort serialPort;
|
|
StringBuilder measurementBuilder;
|
|
|
|
/// <summary>The state of the measurement</summary>
|
|
MsrmntState msrmntState;
|
|
|
|
/// <summary>Measured temperature when MsrmntState == MsrmntState.Valid</summary>
|
|
float temperature;
|
|
|
|
/// <summary>Measured humidity when MsrmntState == MsrmntState.Valid</summary>
|
|
float humidity;
|
|
|
|
/// <summary>Measured pressure when MsrmntState == MsrmntState.Valid</summary>
|
|
float pressure;
|
|
|
|
/// <summary>Measurement time stamp when MsrmntState == MsrmntState.Valid</summary>
|
|
int msrmntTimeStamp;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public Ambient(AmbientCfg cfg, IList<Generic.IComponent> components)
|
|
{
|
|
if (cfg == null) throw new ArgumentNullException("cfg");
|
|
this.ambientCfg = cfg;
|
|
}
|
|
|
|
public new void Initialize()
|
|
{
|
|
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate)
|
|
{
|
|
temperature = 20.0f;
|
|
humidity = 40.0f;
|
|
pressure = 1.0f;
|
|
msrmntTimeStamp = StateMachine.CurrentTime;
|
|
msrmntState = BenchControl.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();
|
|
measurementBuilder = new StringBuilder(40);
|
|
msrmntState = MsrmntState.Busy;
|
|
}
|
|
|
|
/// <summary>Run this device</summary>
|
|
public void RunDeviceBefore()
|
|
{
|
|
if (ambientCfg.DebugLevel == Entities.DebugMode.Simulate)
|
|
{
|
|
msrmntTimeStamp = StateMachine.CurrentTime;
|
|
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)
|
|
{
|
|
string measurement = measurementBuilder.ToString();
|
|
measurementBuilder.Clear();
|
|
|
|
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.CurrentTime;
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
log.InfoFormat("Msrmnt OK: time = {0}, temp={1}, humi={2}, pres={3}", msrmntTimeStamp, t, h, p);
|
|
}
|
|
else
|
|
{
|
|
log.Warn("Invalid string received: " + measurement);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Run this device</summary>
|
|
public void RunDeviceAfter()
|
|
{
|
|
}
|
|
|
|
/// <summary>Stop this device</summary>
|
|
public void StopDevice()
|
|
{
|
|
if (serialPort != null) serialPort.Close();
|
|
}
|
|
|
|
///
|
|
/// Boxes for the operation result
|
|
///
|
|
FloatBox tempBox;
|
|
FloatBox pressureBox;
|
|
FloatBox humiBox;
|
|
|
|
public IOperation ReadAmbientOp(FloatBox temperature, FloatBox pressure, FloatBox humidity)
|
|
{
|
|
this.tempBox = temperature;
|
|
this.pressureBox = pressure;
|
|
this.humiBox = humidity;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>
|
|
/// Event.FlowInDone or Event.FlowOutDone
|
|
/// </returns>
|
|
public Event Run()
|
|
{
|
|
tempBox.Val = temperature;
|
|
pressureBox.Val = pressure;
|
|
humiBox.Val = humidity;
|
|
return Event.AmbientDone;
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
public void Stop()
|
|
{
|
|
}
|
|
}
|
|
}
|