Modbus.Common.Modbus.SendMessage(byte deviceAddress, byte function, ...) function added.

This commit is contained in:
Milan Hanajik
2016-10-18 11:31:17 +02:00
parent 9e90d3a93f
commit 14de767c52
@@ -71,11 +71,12 @@ namespace TBF.BenchControl.Modbus.Common
/// <summary>
/// Send a modbus message.
/// This function calculates and overwrites the last 2 bytes
/// of the message - CRC as specified in Modbus specification.
/// 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, CRC bytes dont have to be set</param>
/// <param name="message">Message incl CRC fields, CRC bytes dont have to be set</param>
public void SendMessage(byte[] message)
{
if (modbusCommonCfg.DebugLevel == DebugMode.Simulate) return;
@@ -85,6 +86,36 @@ namespace TBF.BenchControl.Modbus.Common
serialPort.Write(message, 0, message.Length);
}
/// <summary>
/// Send a structured modbus message.
/// </summary>
/// <param name="modbusAddress">Device address (1..255) or 0 = broadcast</param>
/// <param name="function">Function (0..127)</param>
/// <param name="dataAddress">Address of data to be transferred (0..65535)</param>
/// <param name="dataCount">Count of data bytes to be transferred (0..65535)</param>
public void SendMessage(byte modbusAddress, byte function, ushort dataAddress, ushort dataCount)
{
if (modbusCommonCfg.DebugLevel == DebugMode.Simulate) return;
byte[] message = new byte[8]
{
modbusAddress,
function,
(byte)(dataAddress / 256),
(byte)(dataAddress % 256),
(byte)(dataCount / 256),
(byte)(dataCount % 256),
0,
0,
};
Telegram.UpdateTelegramCRC(message);
log.Debug(Telegram.LogTelegram("Sending message ", message));
serialPort.Write(message, 0, message.Length);
}
void Worker()
{
while (!stopWorkerThread)