124 lines
4.1 KiB
C#
124 lines
4.1 KiB
C#
///
|
|
/// Copyright (c) 2016-2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.Modbus.Meret.PressureMeter
|
|
{
|
|
public class PressureMeter : ComponentBase, GenericDevices.IPressureMeter, IDevice
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(PressureMeter));
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(1)); }
|
|
|
|
readonly PressureMeterCfg pressureMtrCfg;
|
|
readonly GenericDevices.IModbus modbus;
|
|
double receivedPressure;
|
|
|
|
public PressureMeter()
|
|
{
|
|
}
|
|
|
|
public PressureMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
pressureMtrCfg = cfg as PressureMeterCfg;
|
|
|
|
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
///
|
|
/// IDevice interface
|
|
///
|
|
public void Initialize() { }
|
|
|
|
public void RunDeviceBefore()
|
|
{
|
|
if (pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.Simulate ||
|
|
pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.FailureDuringOperation)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Count > 0)
|
|
{
|
|
byte[] telegram = modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Dequeue();
|
|
if (telegram.Length == 9 && telegram[1] == 4 && telegram[2] == 4)
|
|
{
|
|
/// Swap byte order
|
|
byte t1 = telegram[3];
|
|
byte t2 = telegram[4];
|
|
byte t3 = telegram[5];
|
|
byte t4 = telegram[6];
|
|
telegram[3] = t4;
|
|
telegram[4] = t3;
|
|
telegram[5] = t2;
|
|
telegram[6] = t1;
|
|
|
|
receivedPressure = Config.Units.ConvertFrom(Config.Unit.kPa, System.BitConverter.ToSingle(telegram, 3));
|
|
log.WarnFormat("Pressure: {0}={1}bar", Name, receivedPressure.ToString("F3"));
|
|
}
|
|
}
|
|
|
|
if ((StateMachine.Time % Constants.ModbusPollPeriod) == (pressureMtrCfg.ModbusAddress % Constants.ModbusPollPeriod)) /// Each 'ModbusDevices' seconds
|
|
{
|
|
const byte Function = 4; /// Read input registers
|
|
const ushort Address = 0; /// Pressure
|
|
modbus.SendMessage(pressureMtrCfg.ModbusAddress, Function, Address, 2);
|
|
}
|
|
}
|
|
|
|
public void RunDeviceAfter() { }
|
|
public void StopDevice() { }
|
|
public void StopDevice2() { }
|
|
|
|
|
|
/// <summary>
|
|
/// Returns the water pressure
|
|
/// </summary>
|
|
/// <returns>Pressure in mBar</returns>
|
|
public float ReadPressure()
|
|
{
|
|
if (Cfg.DebugLevel == Config.Entities.DebugMode.Normal)
|
|
{
|
|
return Convert.ToSingle(Config.Formulas.CorrectedValue(receivedPressure, Corrections));
|
|
}
|
|
else if (Cfg.DebugLevel == Config.Entities.DebugMode.Simulate)
|
|
{
|
|
return 1.0F;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: PressureDone, Error
|
|
/// </summary>
|
|
/// <param name="pressureBox">Reference to a variable for the pressure in Bar</param>
|
|
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadPressureOp(ref FloatBox pressureBox)
|
|
{
|
|
return new ReadPressureOp(this, ref pressureBox);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: pressureDone, Error
|
|
/// </summary>
|
|
/// <param name="pressureBox">Reference to a variable for the pressure in Bar</param>
|
|
/// <param name="pressureDone">Event returned when measurement done</param>
|
|
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadPressureOp(ref FloatBox pressureBox, Event pressureDone)
|
|
{
|
|
return new ReadPressureOp(this, ref pressureBox, pressureDone);
|
|
}
|
|
}
|
|
}
|