diff --git a/AMWD.Protocols.Modbus.Serial/ModbusRtuProxy.cs b/AMWD.Protocols.Modbus.Serial/ModbusRtuProxy.cs index 3670006..3070dc4 100644 --- a/AMWD.Protocols.Modbus.Serial/ModbusRtuProxy.cs +++ b/AMWD.Protocols.Modbus.Serial/ModbusRtuProxy.cs @@ -738,8 +738,9 @@ namespace AMWD.Protocols.Modbus.Serial byte[] objBytes = GetDeviceObject((byte)i, deviceInfo); - // We need to split the response if it would exceed the max ADU size - if (responseBytes.Count + bodyBytes.Count + objBytes.Length > RtuProtocol.MAX_ADU_LENGTH) + // We need to split the response if it would exceed the max ADU size. + // 2 bytes of CRC have to be added. + if (responseBytes.Count + bodyBytes.Count + objBytes.Length + 2 > RtuProtocol.MAX_ADU_LENGTH) { bodyBytes[3] = 0xFF; bodyBytes[4] = (byte)i; @@ -831,9 +832,8 @@ namespace AMWD.Protocols.Modbus.Serial default: { - if (deviceIdentification.ExtendedObjects.ContainsKey(objectId)) + if (deviceIdentification.ExtendedObjects.TryGetValue(objectId, out byte[] bytes)) { - byte[] bytes = deviceIdentification.ExtendedObjects[objectId]; result.Add((byte)bytes.Length); result.AddRange(bytes); } diff --git a/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs b/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs index cc7a3b3..99373f2 100644 --- a/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs +++ b/AMWD.Protocols.Modbus.Tests/Serial/ModbusRtuProxyTest.cs @@ -1050,14 +1050,21 @@ namespace AMWD.Protocols.Modbus.Tests.Serial _requestBytesQueue.Enqueue([.. request, .. RtuProtocol.CRC16(request)]); byte[] expectedResponse = [ 1, 43, 14, 3, - 3, 255, 224, 103, + 3, 255, 223, 102, 0, 10, 86, 101, 110, 100, 111, 114, 78, 97, 109, 101, 1, 11, 80, 114, 111, 100, 117, 99, 116, 67, 111, 100, 101, 2, 18, 77, 97, 106, 111, 114, 77, 105, 110, 111, 114, 82, 101, 118, 105, 115, 105, 111, 110, 3, 0, 4, 0, 5, 0, 6, 0, 128, 0, 129, 0, 130, 0, 131, 3, 11, 22, 33, - 132, 0, 133, 0, 134, 0, 135, 0, 136, 0, 137, 0, 138, 0, 139, 0, 140, 0, 141, 0, 142, 0, 143, 0, 144, 0, 145, 0, 146, 0, 147, 0, 148, 0, 149, 0, 150, 0, 151, 0, 152, 0, 153, 0, 154, 0, 155, 0, 156, 0, 157, 0, 158, 0, 159, 0, 160, 0, 161, 0, 162, 0, 163, 0, 164, 0, 165, 0, 166, 0, 167, 0, 168, 0, 169, 0, 170, 0, 171, 0, 172, 0, 173, 0, 174, 0, 175, 0, 176, 0, 177, 0, 178, 0, 179, 0, 180, 0, 181, 0, 182, 0, 183, 0, 184, 0, 185, 0, 186, 0, 187, 0, 188, 0, 189, 0, 190, 0, 191, 0, 192, 0, 193, 0, 194, 0, 195, 0, 196, 0, 197, 0, 198, 0, 199, 0, 200, 0, 201, 0, 202, 0, 203, 0, 204, 0, 205, 0, 206, 0, 207, 0, 208, 0, 209, 0, 210, 0, 211, 0, 212, 0, 213, 0, 214, 0, 215, 0, 216, 0, 217, 0, 218, 0, 219, 0, 220, 0, 221, 0, 222, 0, 223, 0]; + 132, 0, 133, 0, 134, 0, 135, 0, 136, 0, 137, 0, 138, 0, 139, 0, 140, 0, 141, 0, 142, 0, 143, 0, + 144, 0, 145, 0, 146, 0, 147, 0, 148, 0, 149, 0, 150, 0, 151, 0, 152, 0, 153, 0, 154, 0, 155, 0, + 156, 0, 157, 0, 158, 0, 159, 0, 160, 0, 161, 0, 162, 0, 163, 0, 164, 0, 165, 0, 166, 0, 167, 0, + 168, 0, 169, 0, 170, 0, 171, 0, 172, 0, 173, 0, 174, 0, 175, 0, 176, 0, 177, 0, 178, 0, 179, 0, + 180, 0, 181, 0, 182, 0, 183, 0, 184, 0, 185, 0, 186, 0, 187, 0, 188, 0, 189, 0, 190, 0, 191, 0, + 192, 0, 193, 0, 194, 0, 195, 0, 196, 0, 197, 0, 198, 0, 199, 0, 200, 0, 201, 0, 202, 0, 203, 0, + 204, 0, 205, 0, 206, 0, 207, 0, 208, 0, 209, 0, 210, 0, 211, 0, 212, 0, 213, 0, 214, 0, 215, 0, + 216, 0, 217, 0, 218, 0, 219, 0, 220, 0, 221, 0, 222, 0]; using var proxy = GetProxy(); await proxy.StartAsync(); @@ -1072,7 +1079,7 @@ namespace AMWD.Protocols.Modbus.Tests.Serial _serialPortMock.Verify(m => m.Close(), Times.Once); _serialPortMock.Verify(m => m.Open(), Times.Once); _serialPortMock.Verify(m => m.Read(It.IsAny(), 0, RtuProtocol.MAX_ADU_LENGTH), Times.Once); - _serialPortMock.Verify(m => m.Write(It.IsAny(), 0, 258), Times.Once); + _serialPortMock.Verify(m => m.Write(It.IsAny(), 0, 256), Times.Once); _serialPortMock.VerifyAdd(m => m.DataReceived += It.IsAny(), Times.Once); _serialPortMock.VerifyRemove(m => m.DataReceived -= It.IsAny(), Times.Once);