/// /// Copyright (c) 2016-2020 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using SchematicDrawing; using TBF.BenchControl.Generic; using TBF.Boxes; namespace TBF.BenchControl.Modbus.PressureMeter.Meret { public class PressureMeter : ComponentBase, GenericDevices.IPressureMeter, IDevice, IDrawingItCmpntWithMeasuredVal { 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; public IDrawingItem DrawingItem { get { return pressureMtrCfg as IDrawingItem; } } readonly GenericDevices.IModbus modbus; double receivedPressure; public double MeasuredVal { get { return receivedPressure; } } public string AltString { get { return string.Empty; } } public PressureMeter() { } public PressureMeter(Generic.IComponentCfg cfg, IList 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.Warn(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((byte)pressureMtrCfg.ModbusAddress, Function, Address, 2); } } public void RunDeviceAfter() { } public void StopDevice() { } public void StopDevice2() { } /// /// Returns the water pressure /// /// Pressure in mBar public double ReadPressure() { if (Cfg.DebugLevel == Config.Entities.DebugMode.Normal) { return Config.Formulas.CorrectedValue(receivedPressure, Corrections); } else if (Cfg.DebugLevel == Config.Entities.DebugMode.Simulate) { return 1.0; } else { return 0; } } /// /// Events: PressureDone, Error /// /// Reference to a variable for the pressure in Bar /// ReadPressureOp instance reference casted to IOperaton public IOperation ReadPressureOp(ref DoubleBox pressureBox) { return new ReadPressureOp(this, ref pressureBox); } /// /// Events: pressureDone, Error /// /// Reference to a variable for the pressure in Bar /// Event returned when measurement done /// ReadPressureOp instance reference casted to IOperaton public IOperation ReadPressureOp(ref DoubleBox pressureBox, Event pressureDone) { return new ReadPressureOp(this, ref pressureBox, pressureDone); } } }