2016-10-07 09:59:13 +00:00
|
|
|
|
///
|
2017-09-29 11:20:23 +00:00
|
|
|
|
/// Copyright (c) 2016-2017 Sensus Metering Systems
|
2016-10-07 09:59:13 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using log4net;
|
|
|
|
|
|
using TBF.BenchControl.Generic;
|
|
|
|
|
|
using TBF.Boxes;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TBF.BenchControl.Modbus.PressureMeter.Meret
|
|
|
|
|
|
{
|
|
|
|
|
|
public class PressureMeter : ComponentBase, GenericDevices.IPressureMeter, IDevice
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(PressureMeter));
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
2016-10-07 09:59:13 +00:00
|
|
|
|
|
2016-10-18 13:57:54 +00:00
|
|
|
|
readonly PressureMeterCfg pressureMtrCfg;
|
2016-10-07 09:59:13 +00:00
|
|
|
|
readonly GenericDevices.IModbus modbus;
|
|
|
|
|
|
|
2016-10-18 13:57:54 +00:00
|
|
|
|
private float receivedPressure;
|
2016-10-07 09:59:13 +00:00
|
|
|
|
|
|
|
|
|
|
const ushort WatchdogBitMask = 0x0010;
|
|
|
|
|
|
|
2021-04-07 07:57:42 +00:00
|
|
|
|
|
|
|
|
|
|
public PressureMeter() { }
|
2016-10-07 09:59:13 +00:00
|
|
|
|
|
|
|
|
|
|
public PressureMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
|
|
|
|
: base(cfg)
|
|
|
|
|
|
{
|
2016-10-18 13:57:54 +00:00
|
|
|
|
pressureMtrCfg = cfg as PressureMeterCfg;
|
2016-10-07 09:59:13 +00:00
|
|
|
|
|
|
|
|
|
|
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
|
|
|
|
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Initialize this device</summary>
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override void Initialize()
|
2016-10-07 09:59:13 +00:00
|
|
|
|
{
|
2021-04-07 07:57:42 +00:00
|
|
|
|
log.FatalFormat("{0} initialized: {1}", Name, this);
|
2016-10-07 09:59:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the water pressure
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>Pressure in mBar</returns>
|
|
|
|
|
|
public float ReadPressure()
|
|
|
|
|
|
{
|
2019-03-04 08:19:40 +00:00
|
|
|
|
double rawPressure = Config.Units.ConvertFrom(Config.Unit.kPa, receivedPressure);
|
|
|
|
|
|
double correctedPressure = Config.Formulas.CorrectedValue(rawPressure, Corrections);
|
|
|
|
|
|
return (float)correctedPressure;
|
2016-10-07 09:59:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: PressureDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="pressure">Reference to a variable for the pressure in Bar</param>
|
|
|
|
|
|
/// <returns>ReadPressureOp instance reference casted to IOperaton</returns>
|
|
|
|
|
|
public IOperation ReadPressureOp(ref FloatBox pressure)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ReadPressureOp(this, ref pressure);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: pressureDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="pressure">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 pressure, Event pressureDone)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ReadPressureOp(this, ref pressure, pressureDone);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceBefore()
|
|
|
|
|
|
{
|
2017-09-29 11:20:23 +00:00
|
|
|
|
if (pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.Simulate ||
|
|
|
|
|
|
pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.FailureDuringOperation)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Count > 0)
|
2016-10-18 13:57:54 +00:00
|
|
|
|
{
|
|
|
|
|
|
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 = System.BitConverter.ToSingle(telegram, 3);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-07 07:57:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceAfter()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.Simulate ||
|
|
|
|
|
|
pressureMtrCfg.DebugLevel == Config.Entities.DebugMode.FailureDuringOperation)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2017-09-29 11:20:23 +00:00
|
|
|
|
|
|
|
|
|
|
if ((StateMachine.Time % 3) == (pressureMtrCfg.ModbusAddress % 3)) /// Each 3 seconds
|
|
|
|
|
|
{
|
|
|
|
|
|
const byte Function = 4; /// Read input registers
|
|
|
|
|
|
const ushort Address = 0; /// Pressure
|
|
|
|
|
|
modbus.SendMessage(pressureMtrCfg.ModbusAddress, Function, Address, 2);
|
|
|
|
|
|
}
|
2016-10-07 09:59:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-05 18:26:07 +00:00
|
|
|
|
public void StopDevice() { }
|
|
|
|
|
|
public void StopDevice2() { }
|
2016-10-07 09:59:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|