Modbus.TempMeter.Groch in progress

This commit is contained in:
Milan Hanajik
2021-03-14 21:31:26 +01:00
parent afa213f8d5
commit bdefc0b8fa
2 changed files with 84 additions and 4 deletions
+28 -1
View File
@@ -104,7 +104,34 @@ namespace TBF.Rig.Modbus.Common
{
if (serialPort == null) return;
Telegram.UpdateTelegramCRC(message);
Telegram.UpdateTelegramCRC(message);
if ((DateTime.Now - lastSerialPortWrite) > new TimeSpan(0, 0, 0, 0, 100) && initialRunDeviceCommComplete)
{
/// More then 100 ms since last 'send' --> do not enqueue the message
SendMessageNow(message);
}
else
{
telegramsToSend.Enqueue(message);
string s = Telegram.LogTelegram(string.Format("{0} - Enqueueing message ", Name), message);
Debug.WriteLine(s);
log.Debug(s);
}
}
/// <summary>
/// Send an arbitrary modbus message.
/// When the message length is N, however only bytes 1..N-2 have to be set.
/// The last two message bytes (CRC) may be uninitialized or zero.
/// They are calculated inside this function as required by Modbus specification.
/// </summary>
/// <param name="message">Message incl CRC fields, CRC bytes dont have to be set</param>
public void SendMessageGroch(byte[] message)
{
if (serialPort == null) return;
if ((DateTime.Now - lastSerialPortWrite) > new TimeSpan(0, 0, 0, 0, 100) && initialRunDeviceCommComplete)
{
+56 -3
View File
@@ -12,11 +12,14 @@ using TBF.Resources;
namespace TBF.Rig.Modbus.TempMeter.Groch
{
public class TempMeter : ComponentBase, GenericDevices.ITempMeter, GenericDevices.IHasCalendarEvents, SchematicDrawing.IDrawingItCmpntWithMeasuredVal
public class TempMeter : ComponentBase, GenericDevices.ITempMeter, IDevice, GenericDevices.IHasCalendarEvents,
SchematicDrawing.IDrawingItCmpntWithMeasuredVal
{
private static readonly ILog log = LogManager.GetLogger(typeof(TempMeter));
public override string ToString() { return string.Format("TempMeter({0})", Cfg.ToString(-1)); }
public const int GrochAddress = 3;
readonly TempMeterCfg tempMtrCfg;
///
int Idx0 { get { return tempMtrCfg.Idx0; } } /// 0..15
@@ -49,8 +52,6 @@ namespace TBF.Rig.Modbus.TempMeter.Groch
modbus = TbfComponents.FindComponent(cfg.ParentName, components) as Common.Modbus;
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
log.Warn(this.ToString());
}
@@ -86,6 +87,58 @@ namespace TBF.Rig.Modbus.TempMeter.Groch
return calendarEvents;
}
public void Initialize() { }
public void RunDeviceBefore()
{
if (tempMtrCfg.DebugLevel == Config.Entities.DebugMode.Simulate ||
tempMtrCfg.DebugLevel == Config.Entities.DebugMode.FailureDuringOperation)
{
return;
}
if (modbus.ReceivedTelegrams[GrochAddress].Count > 0)
{
byte[] telegram = modbus.ReceivedTelegrams[GrochAddress].Dequeue();
if (telegram.Length == 28)
{
byte checksum = 0;
for (int i = 0; i < 27; i++) checksum += telegram[i];
if (telegram[0] == '\r' &&
telegram[1] == '\n' &&
telegram[2] == GrochAddress &&
telegram[3] == 28 &&
telegram[5] == 2 &&
telegram[26] == 3 &&
telegram[27] == checksum)
{
/// Swap byte order
int t1 = telegram[10] + 256 * telegram[11];
int t2 = telegram[12] + 256 * telegram[13];
int t3 = telegram[14] + 256 * telegram[15];
int t4 = telegram[16] + 256 * telegram[17];
int t5 = telegram[18] + 256 * telegram[19];
int t6 = telegram[20] + 256 * telegram[21];
int t7 = telegram[22] + 256 * telegram[23];
int t8 = telegram[24] + 256 * telegram[25];
//receivedTemp = System.BitConverter.ToSingle(telegram, 3);
//log.WarnFormat("Temperature: {0}={1}°C", Name, receivedTemp.ToString("F1"));
}
}
}
}
public void RunDeviceAfter()
{
if ((StateMachine.Time % Constants.ModbusPollPeriod) == (GrochAddress % Constants.ModbusPollPeriod)) /// Each 'ModbusDevices' seconds
{
byte[] message = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x10, 2, 1, 3, 6, 0x16 };
modbus.SendMessageGroch(message);
}
}
public void StopDevice() { }
public void StopDevice2() { }
public double ReadTemperature()
{
if (Cfg.DebugLevel == Config.Entities.DebugMode.Normal)