/// /// Copyright (c) 2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using log4net; using TBF.BenchControl.Generic; using TBF.Boxes; namespace TBF.BenchControl.Modbus.QuidoRS { public class QuidoRS : ComponentBase, IDevice { private static readonly ILog log = LogManager.GetLogger(typeof(QuidoRS)); public override string ToString() { return string.Format("PressureMeter({0})", Cfg.ToString(1)); } readonly QuidoRSCfg quidoRSCfg; readonly GenericDevices.IModbus modbus; private ushort currentOutputs; const ushort WatchdogBitMask = 0x0010; public QuidoRS() { } public QuidoRS(QuidoRSCfg cfg, IList components) : base(cfg) { quidoRSCfg = cfg; modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components); if (modbus == null) throw new Exception("Cannot find " + Name + " parent"); /// Connect to handler //ModbusAddress.MeretRS485Address[Idx0] = ModbusAddress; log.Debug(this.ToString()); } /// Initialize this device public void Initialize() { } /// Run this device public void RunDeviceBefore() { } /// Run this device public void RunDeviceAfter() { currentOutputs = (UInt16)((Int32)currentOutputs ^ (Int32)WatchdogBitMask); SendOutputs(currentOutputs); } /// Stop this device public void StopDevice() { } /// /// Set outputs of QuidoRS board /// /// output value public void SetOutputs(ushort outputs) { currentOutputs = (UInt16)((Int32)outputs | ((Int32)currentOutputs & (Int32)WatchdogBitMask)); SendOutputs(currentOutputs); } private void SendOutputs(ushort outputs) { byte[] msg = new byte[11]; msg[0] = quidoRSCfg.ModbusAddress; msg[1] = (byte)Function.ForceMultipleCoils; msg[2] = 0; /// Data Hi msg[3] = 0; /// Data Lo msg[4] = 0; /// Nr. of coils Hi msg[5] = 16; /// Nr. of coils Lo msg[6] = 2; /// Byte count msg[7] = (byte)(outputs & 0xFF); /// Output data Lo msg[8] = (byte)(outputs >> 8); /// Output data Hi modbus.SendMessage(msg); } /// /// Events: SetOpututsDone, Error /// /// Reference to a variable for the pressure in Bar /// SetOutputsOp instance reference casted to IOperaton public IOperation SetOutputsOp(ushort outValue) { return new SetOutputsOp(this, outValue); } /// /// Events: pressureDone, Error /// /// Reference to a variable for the pressure in Bar /// Event returned when SetOutput is done /// SetOutputsOp instance reference casted to IOperaton public IOperation ReadPressureOp(ushort outValue, Event setOutputsDone) { return new SetOutputsOp(this, outValue, setOutputsDone); } } }