From cbca546da8a27288adb4854970574814205e741a Mon Sep 17 00:00:00 2001 From: pieterg Date: Thu, 20 Apr 2023 14:49:48 +0200 Subject: [PATCH 1/3] AbstractModbusMessage: make MessageFrame virtual so it can be overridden by classes which need to override ProtocolDataUnit --- Modbus/Message/AbstractModbusMessage.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modbus/Message/AbstractModbusMessage.cs b/Modbus/Message/AbstractModbusMessage.cs index cf689ab..4c96960 100644 --- a/Modbus/Message/AbstractModbusMessage.cs +++ b/Modbus/Message/AbstractModbusMessage.cs @@ -39,7 +39,7 @@ public byte SlaveAddress set => MessageImpl.SlaveAddress = value; } - public byte[] MessageFrame => + public virtual byte[] MessageFrame => MessageImpl.MessageFrame; public virtual byte[] ProtocolDataUnit => @@ -62,4 +62,4 @@ public void Initialize(byte[] frame) } protected abstract void InitializeUnique(byte[] frame); -} \ No newline at end of file +} From 833690f359c8ea965385a8a5c0752fae8a4a392f Mon Sep 17 00:00:00 2001 From: pieterg Date: Thu, 20 Apr 2023 14:55:05 +0200 Subject: [PATCH 2/3] ReadWriteMultipleRegistersRequest: override MessageFrame When using the base class MessageFrame implementation, it will call the base class ProtocolDataUnit, resulting in an invalid/incomplete message frame. By overriding MessageFrame, our overridden ProtocolDataUnit will be called instead. --- .../Message/ReadWriteMultipleRegistersRequest.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Modbus/Message/ReadWriteMultipleRegistersRequest.cs b/Modbus/Message/ReadWriteMultipleRegistersRequest.cs index 5de8923..c0afb71 100644 --- a/Modbus/Message/ReadWriteMultipleRegistersRequest.cs +++ b/Modbus/Message/ReadWriteMultipleRegistersRequest.cs @@ -28,6 +28,20 @@ public ReadWriteMultipleRegistersRequest( writeData); } + public override byte[] MessageFrame + { + get + { + byte[]? pdu = ProtocolDataUnit; + MemoryStream frame = new(1 + pdu.Length); + + frame.WriteByte(SlaveAddress); + frame.Write(pdu, 0, pdu.Length); + + return frame.ToArray(); + } + } + public override byte[] ProtocolDataUnit { get @@ -86,4 +100,4 @@ protected override void InitializeUnique(byte[] frame) ReadRequest = ModbusMessageFactory.CreateModbusMessage(readFrame); WriteRequest = ModbusMessageFactory.CreateModbusMessage(writeFrame); } -} \ No newline at end of file +} From 41ecd05075090b20da181ae8ef6d0663536f5097 Mon Sep 17 00:00:00 2001 From: pieterg Date: Thu, 20 Apr 2023 14:58:33 +0200 Subject: [PATCH 3/3] ModbusRtuTransport: fix handling ReadWriteMultipleRegisters response ReadWriteMultipleRegisters response is the same as a ReadHoldingRegisters response --- Modbus/IO/ModbusRtuTransport.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modbus/IO/ModbusRtuTransport.cs b/Modbus/IO/ModbusRtuTransport.cs index 9955783..8563d89 100644 --- a/Modbus/IO/ModbusRtuTransport.cs +++ b/Modbus/IO/ModbusRtuTransport.cs @@ -66,7 +66,8 @@ public static int ResponseBytesToRead(byte[] frameStart) Modbus.ReadCoils or Modbus.ReadInputs or Modbus.ReadHoldingRegisters or - Modbus.ReadInputRegisters + Modbus.ReadInputRegisters or + Modbus.ReadWriteMultipleRegisters => frameStart[2] + 1, Modbus.WriteSingleCoil or @@ -134,4 +135,4 @@ internal override byte[] ReadRequest() return frame; } -} \ No newline at end of file +}