4-20ma / ModbusMaster

Enlighten your Arduino to be a Modbus master
http://4-20ma.io/ModbusMaster/
Apache License 2.0
587 stars 350 forks source link

Last byte missing from master request #142

Open t6Jay opened 4 years ago

t6Jay commented 4 years ago

I could not get the modbus slave to respond to read requests, after I modified the cpp file, it works.

I was requesting read of 35 holding registers from address 256 like so; result=node.readHoldingRegisters(0x0100, 35); But the slave was not responding (modbus timeout)

Using a modbus slave simulator I could see the message was; 0103 0100 0023 05 (hex) so it's 7 bytes long and I think it should be 8 bytes, It looks like the CRC is missing a byte.

I found in the ModbusMaster.cpp the code responsible to send the message;

for (i = 0; i < u8ModbusADUSize; i++) { _serial->write(u8ModbusADU[i]); }

It looks like the u8ModbusADUSize is calculated correctly, so I modified the code to;

for (i = 0; i < u8ModbusADUSize+1; i++) { _serial->write(u8ModbusADU[i]); }

And now I can see 8 bytes going out, and my modbus slave now responds correctly!

To complicate the problem, I have used this library before, on a Leonardo, and it worked without having to modify the cpp file. I think the library version was the same, but cannot be sure.

I'd like to either understand why I had a problem with the library, or if there really is a bug, help to get it fixed (i find this unlikely as I'm a C++ and arduino novice).