Initialization and logging of several TBF.Rig.Modbus components.

This commit is contained in:
Milan Hanajik
2022-04-26 16:51:23 +02:00
parent fd35f20501
commit fc81344857
7 changed files with 161 additions and 132 deletions
+20 -14
View File
@@ -199,15 +199,15 @@ namespace TBF.Rig.Modbus.Common
if (bytesCount > 0)
{
bytesCount = serialPort.Read(buffer, 0, Math.Min(bytesCount, BUFFER_SIZE));
}
///
string st = string.Format("{0}", Name, Telegram.LogTelegram("Received data ", buffer, 0, bytesCount));
Debug.WriteLine(st);
log.Debug(st);
///
for (int i = 0; i < bytesCount && rcvdBytesCount < BUFFER_SIZE; i++, rcvdBytesCount++)
{
rcvdData[rcvdBytesCount] = buffer[i];
///
for (int i = 0; i < bytesCount && rcvdBytesCount < BUFFER_SIZE; i++, rcvdBytesCount++)
{
rcvdData[rcvdBytesCount] = buffer[i];
}
///
string st = string.Format("[{0}] received {1}", Name, Telegram.LogTelegram("", buffer, 0, bytesCount));
Debug.WriteLine(st);
log.Debug(st);
}
}
@@ -236,8 +236,11 @@ namespace TBF.Rig.Modbus.Common
/// Enqueue
receivedTelegrams[0].Enqueue(receivedTelegram);
string s = string.Format("{0} queue size={1}", Telegram.LogTelegram(string.Format("Received message from {0} ", componentNames[0]),
receivedTelegram), receivedTelegrams[0].Count);
string s = string.Format("[{0}] received message from [{1}] {2} queue size={3}",
Name,
componentNames[0],
Telegram.LogTelegram("", receivedTelegram),
receivedTelegrams[0].Count);
Debug.WriteLine(s);
log.Debug(s);
offset += 28;
@@ -260,8 +263,11 @@ namespace TBF.Rig.Modbus.Common
int deviceAddress = receivedTelegram[0];
receivedTelegrams[deviceAddress].Enqueue(receivedTelegram);
string s = string.Format("{0} queue size={1}", Telegram.LogTelegram(string.Format("Received message from {0} ", componentNames[deviceAddress]),
receivedTelegram), receivedTelegrams[deviceAddress].Count);
string s = string.Format("[{0}] received message from [{1}] {2} queue size={3}",
Name,
componentNames[deviceAddress],
Telegram.LogTelegram("", receivedTelegram),
receivedTelegrams[deviceAddress].Count);
Debug.WriteLine(s);
log.Debug(s);
offset += candidateLen;
@@ -386,7 +392,7 @@ namespace TBF.Rig.Modbus.Common
serialPort.Write(message, 0, message.Length);
}
string s = Telegram.LogTelegram(string.Format("{0} - Sending message to {1} ", Name, meno), message);
string s = Telegram.LogTelegram(string.Format("[{0}] sending message to [{1}] ", Name, meno), message);
Debug.WriteLine(s);
log.Debug(s);
}
@@ -44,17 +44,24 @@ namespace TBF.Rig.Modbus.PressureMeter.Meret
///
public override void Initialize()
{
modbus = TbfComponents.FindComponent(pressureMtrCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
if (DebugLevel == DebugMode.Normal)
{
modbus = TbfComponents.FindComponent(pressureMtrCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
modbus.ComponentNames[pressureMtrCfg.ModbusAddress] = Name;
ticketNumber = modbus.RegisterForPolling();
log.FatalFormat("{0} initialized: {1}", Name, this);
modbus.ComponentNames[pressureMtrCfg.ModbusAddress] = Name;
ticketNumber = modbus.RegisterForPolling();
log.FatalFormat("{0} initialized: {1}", Name, this);
}
else
{
log.FatalFormat("{0} simulated: {1}", Name, this);
}
}
public void RunDeviceBefore()
{
if (modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Count > 0)
if (DebugLevel == DebugMode.Normal && modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Count > 0)
{
byte[] telegram = modbus.ReceivedTelegrams[pressureMtrCfg.ModbusAddress].Dequeue();
if (telegram.Length == 9 && telegram[1] == 4 && telegram[2] == 4)
@@ -77,7 +84,7 @@ namespace TBF.Rig.Modbus.PressureMeter.Meret
public void RunDeviceAfter()
{
if (modbus.IsMyTurn(ticketNumber))
if (DebugLevel == DebugMode.Normal && modbus.IsMyTurn(ticketNumber))
{
const byte Function = 4; /// Read input registers
const ushort Address = 0; /// Pressure
@@ -95,11 +102,11 @@ namespace TBF.Rig.Modbus.PressureMeter.Meret
/// <returns>Pressure in mBar</returns>
public double ReadPressure()
{
if (Cfg.DebugLevel == DebugMode.Normal)
if (DebugLevel == DebugMode.Normal)
{
return MeasurementCorrection.CorrectedValue(receivedPressure, Corrections);
}
else if (Cfg.DebugLevel == DebugMode.Simulate)
else if (DebugLevel == DebugMode.Simulate)
{
return 1.0;
}
@@ -121,8 +121,7 @@ namespace TBF.Rig.Modbus.PressureMeter.Meret
case 4: return MsrdValLimLo.ToString();
case 5: return MsrdValLimHi.ToString();
default:
return string.Format("{0}(1) {2}[{3},{4}] addr={5}", Name,
(string.IsNullOrEmpty(ParentName) ? "-" : ParentName), Shape, X, Y, ModbusAddress);
return string.Format("{0}({1}) address={2}", Name, string.IsNullOrEmpty(ParentName) ? "-" : ParentName, ModbusAddress);
}
}
+39 -36
View File
@@ -118,8 +118,8 @@ namespace TBF.Rig.Modbus.PumpFM.DanfossVLT
}
}
readonly Common.Modbus modbus;
readonly IControlBoard controlBoard;
Common.Modbus modbus;
IControlBoard controlBoard;
int bitNr; /// 0 .. 63
public UInt128 Mask; /// derived from bitPosition in the constructor
@@ -136,43 +136,48 @@ namespace TBF.Rig.Modbus.PumpFM.DanfossVLT
: base(cfg)
{
pumpCfg = cfg as PumpCfg;
modbus = TbfComponents.FindComponent(cfg.ParentName, components) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
controlBoard = null;
foreach (var name in new string[] { "CB", "UniCB" })
{
if ((controlBoard = TbfComponents.FindComponent(name, components) as IControlBoard) != null)
break;
}
if (controlBoard == null) throw new Exception("Cannot find a control board");
log.Warn(this.ToString());
}
public override void Initialize()
{
if (DebugLevel == DebugMode.Normal || DebugLevel == DebugMode.DetectedOn)
{
modbus = TbfComponents.FindComponent(pumpCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
controlBoard = null;
foreach (var name in new string[] { "CB", "UniCB" })
{
if ((controlBoard = TbfComponents.FindComponent(name) as IControlBoard) != null)
break;
}
if (controlBoard == null) throw new Exception("Cannot find a control board");
bitNr = pumpCfg.BitNr;
Mask = (((UInt128)1) << bitNr);
modbus.ComponentNames[pumpCfg.ModbusAddress] = Name;
TBF.UiBridge.Bridge.SetpointChangeHandler += delegate(object sndr, TBF.UiBridge.SetpointChangeArgs args)
{
if (args.Name == Name)
{
if (args.Increase) IncreaseSetpoint(); else DecreaseSetpoint();
}
};
log.FatalFormat("{0} initialized: {1}", Name, this);
}
else
{
log.FatalFormat("{0} simulated: {1}", Name, this);
}
}
///
/// IDevice interface
///
public override void Initialize()
{
bitNr = pumpCfg.BitNr;
Mask = (((UInt128)1) << bitNr);
modbus.ComponentNames[pumpCfg.ModbusAddress] = Name;
TBF.UiBridge.Bridge.SetpointChangeHandler += delegate(object sndr, TBF.UiBridge.SetpointChangeArgs args)
{
if (args.Name == Name)
{
if (args.Increase) IncreaseSetpoint(); else DecreaseSetpoint();
}
};
}
public void RunDeviceBefore()
{
if (pumpCfg.DebugLevel == DebugMode.Normal ||
pumpCfg.DebugLevel == DebugMode.DetectedOn)
if (DebugLevel == DebugMode.Normal || DebugLevel == DebugMode.DetectedOn)
{
if (modbus.ReceivedTelegrams[pumpCfg.ModbusAddress].Count > 0)
{
@@ -205,8 +210,7 @@ namespace TBF.Rig.Modbus.PumpFM.DanfossVLT
this.power = powerArg;
UInt16 reference = (UInt16)Math.Round(0x4000 * powerArg / 100.0);
if (pumpCfg.DebugLevel == DebugMode.Normal ||
pumpCfg.DebugLevel == DebugMode.DetectedOn)
if (DebugLevel == DebugMode.Normal || DebugLevel == DebugMode.DetectedOn)
{
byte[] msg = new byte[13];
msg[0] = (byte)pumpCfg.ModbusAddress;
@@ -242,8 +246,7 @@ namespace TBF.Rig.Modbus.PumpFM.DanfossVLT
this.power = 0;
UInt16 reference = 0;
if (pumpCfg.DebugLevel == DebugMode.Normal ||
pumpCfg.DebugLevel == DebugMode.DetectedOn)
if (DebugLevel == DebugMode.Normal || DebugLevel == DebugMode.DetectedOn)
{
byte[] msg = new byte[13];
msg[0] = (byte)pumpCfg.ModbusAddress;
+68 -57
View File
@@ -49,14 +49,21 @@ namespace TBF.Rig.Modbus.QuidoRS
public override void Initialize()
{
watchdogBitMask = (ushort)(quidoRSCfg.WatchdogEnabled ? (1 << quidoRSCfg.WatchdogBitNr) : 0);
if (DebugLevel == DebugMode.Normal)
{
watchdogBitMask = (ushort)(quidoRSCfg.WatchdogEnabled ? (1 << quidoRSCfg.WatchdogBitNr) : 0);
modbus = TbfComponents.FindComponent(quidoRSCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
modbus = TbfComponents.FindComponent(quidoRSCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
modbus.ComponentNames[quidoRSCfg.ModbusAddress] = Name;
if (quidoRSCfg.ReadInputs) ticketNumber = modbus.RegisterForPolling();
log.FatalFormat("Device initialized: {0}", this);
modbus.ComponentNames[quidoRSCfg.ModbusAddress] = Name;
if (quidoRSCfg.ReadInputs) ticketNumber = modbus.RegisterForPolling();
log.FatalFormat("{0} initialized: {1}", Name, this);
}
else
{
log.FatalFormat("{0} simulated: {1}", Name, this);
}
}
///
@@ -64,45 +71,46 @@ namespace TBF.Rig.Modbus.QuidoRS
///
public void RunDeviceBefore()
{
if (quidoRSCfg.DebugLevel == DebugMode.Simulate) return;
lock (this)
if (DebugLevel == DebugMode.Normal)
{
if (watchdogBitMask != 0 && (DateTime.Now - lastOutputsChange) > new TimeSpan(0, 0, 0, 0, 3000)) /// Toggle after 1.5 sec (resulting in toggle period 2 sec)
lock (this)
{
currentOutputs = (UInt16)((Int32)currentOutputs ^ (Int32)watchdogBitMask);
SendOutputs(currentOutputs);
}
}
if (modbus.ReceivedTelegrams[quidoRSCfg.ModbusAddress].Count > 0)
{
byte[] telegram = modbus.ReceivedTelegrams[quidoRSCfg.ModbusAddress].Dequeue();
if ((telegram.Length >= 8) && (telegram[1] == 0x11) && (telegram[2] == telegram.Length - 5))
{
int msgLen = telegram.Length - 7;
byte modbusAddress = telegram[3];
string msg = Encoding.ASCII.GetString(telegram, 5, msgLen);
string s = string.Format("Device detected: Address = {0}, Message = {1}", modbusAddress, msg);
Debug.WriteLine(s);
log.Warn(s);
if (watchdogBitMask != 0 && (DateTime.Now - lastOutputsChange) > new TimeSpan(0, 0, 0, 0, 3000)) /// Toggle after 1.5 sec (resulting in toggle period 2 sec)
{
currentOutputs = (UInt16)((Int32)currentOutputs ^ (Int32)watchdogBitMask);
SendOutputs(currentOutputs);
}
}
if ((telegram.Length == 6) && (telegram[1] == (byte)Function.ReadSingleInput) && (telegram[2] == telegram.Length - 5))
if (modbus.ReceivedTelegrams[quidoRSCfg.ModbusAddress].Count > 0)
{
byte inputs = telegram[3];
Inputs = (UInt32)inputs;
string s = string.Format("Inputs = 0x{0}", inputs.ToString("X2"));
Debug.WriteLine(s);
log.Warn(s);
byte[] telegram = modbus.ReceivedTelegrams[quidoRSCfg.ModbusAddress].Dequeue();
if ((telegram.Length >= 8) && (telegram[1] == 0x11) && (telegram[2] == telegram.Length - 5))
{
int msgLen = telegram.Length - 7;
byte modbusAddress = telegram[3];
string msg = Encoding.ASCII.GetString(telegram, 5, msgLen);
string s = string.Format("Device detected: Address = {0}, Message = {1}", modbusAddress, msg);
Debug.WriteLine(s);
log.Warn(s);
}
if ((telegram.Length == 6) && (telegram[1] == (byte)Function.ReadSingleInput) && (telegram[2] == telegram.Length - 5))
{
byte inputs = telegram[3];
Inputs = (UInt32)inputs;
string s = string.Format("Inputs = 0x{0}", inputs.ToString("X2"));
Debug.WriteLine(s);
log.Warn(s);
}
}
}
}
public void RunDeviceAfter()
{
if (quidoRSCfg.ReadInputs && modbus.IsMyTurn(ticketNumber))
if (DebugLevel == DebugMode.Normal && quidoRSCfg.ReadInputs && modbus.IsMyTurn(ticketNumber))
{
const ushort Address = 0; /// Discrete inputs
modbus.SendMessage((byte)quidoRSCfg.ModbusAddress, (byte)Function.ReadSingleInput, Address, 2, Name);
@@ -171,31 +179,34 @@ namespace TBF.Rig.Modbus.QuidoRS
private void SendOutputs(UInt32 outputs)
{
byte addr = quidoRSCfg.ModbusAddress;
byte cmd = (byte)Function.ForceMultipleCoils;
byte dataLo = (byte)(outputs & 0xFF);
byte data2 = (byte)(outputs >> 8);
byte data3 = (byte)(outputs >> 16);
byte dataHi = (byte)(outputs >> 24);
if (DebugLevel == DebugMode.Normal)
{
byte addr = quidoRSCfg.ModbusAddress;
byte cmd = (byte)Function.ForceMultipleCoils;
byte dataLo = (byte)(outputs & 0xFF);
byte data2 = (byte)(outputs >> 8);
byte data3 = (byte)(outputs >> 16);
byte dataHi = (byte)(outputs >> 24);
if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_4_4)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 4, 1, dataLo, 0, 0 }, Name);
}
else if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_8_8)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 8, 1, dataLo, 0, 0 }, Name);
}
else if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_2_16)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 16, 2, dataLo, data2, 0, 0 }, Name);
}
else if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_2_32)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 32, 4, dataLo, data2, data3, dataHi, 0, 0 }, Name);
}
if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_4_4)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 4, 1, dataLo, 0, 0 }, Name);
}
else if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_8_8)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 8, 1, dataLo, 0, 0 }, Name);
}
else if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_2_16)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 16, 2, dataLo, data2, 0, 0 }, Name);
}
else if (quidoRSCfg.Variant == QuidoVariant.QuidoRS_2_32)
{
modbus.SendMessage(new byte[] { addr, cmd, 0, 0, 0, 32, 4, dataLo, data2, data3, dataHi, 0, 0 }, Name);
}
lastOutputsChange = DateTime.Now;
lastOutputsChange = DateTime.Now;
}
}
+16 -12
View File
@@ -55,16 +55,23 @@ namespace TBF.Rig.Modbus.TempMeter.Groch
public override void Initialize()
{
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
if (Channel == 0)
if (DebugLevel == DebugMode.Normal)
{
modbus = TbfComponents.FindComponent(tempMtrCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
if (Channel == 0)
{
modbus = TbfComponents.FindComponent(tempMtrCfg.ParentName) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
modbus.ComponentNames[0] = Name;
ticketNumber = modbus.RegisterForPolling();
modbus.ComponentNames[0] = Name;
ticketNumber = modbus.RegisterForPolling();
}
log.FatalFormat("{0} initialized: {1}", Name, this);
}
else
{
log.FatalFormat("{0} simulated: {1}", Name, this);
}
log.FatalFormat("{0} initialized: {1}", Name, this);
}
@@ -101,7 +108,7 @@ namespace TBF.Rig.Modbus.TempMeter.Groch
public void RunDeviceBefore()
{
if (Channel == 0)
if (DebugLevel == DebugMode.Normal && Channel == 0)
{
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
///
@@ -145,10 +152,7 @@ namespace TBF.Rig.Modbus.TempMeter.Groch
{
/// Only TempMeter with Channel == 0 communicates via the parent Modbus component
if (Channel == 0 &&
tempMtrCfg.DebugLevel != DebugMode.Simulate &&
tempMtrCfg.DebugLevel != DebugMode.FailureDuringOperation &&
modbus.IsMyTurn(ticketNumber))
if (DebugLevel == DebugMode.Normal && Channel == 0 && modbus.IsMyTurn(ticketNumber))
{
modbus.SendMessageGroch(new byte[] { 0x20, 0x20, 0x20, 0x20, 0x10, 2, 1, 3, 6, 0x16 }, Name);
}
@@ -123,8 +123,7 @@ namespace TBF.Rig.Modbus.TempMeter.Meret
case 4: return MsrdValLimLo.ToString();
case 5: return MsrdValLimHi.ToString();
default:
return string.Format("{0}(1) {2}[{3},{4}] addr={5}", Name,
(string.IsNullOrEmpty(ParentName) ? "-" : ParentName), Shape, X, Y, ModbusAddress);
return string.Format("{0}({1}) address={2}", Name, string.IsNullOrEmpty(ParentName) ? "-" : ParentName, ModbusAddress);
}
}