I've been trying to figure out the problem related to Modbus/RTU communication. Receiving on Arduino end does not work well. Seldom it's able to receive the message from Modbus slave device but usually not.
Background:
Arduino is directly wired to ST485CN chip (almost same as MAX485 - RO output is normally LOW whereas it's HIGH on MAX ship). RS485 bus connected to device. I have tried also ready made RS485 "adapter" for Arduino (MAX chip) and result is even worse.
Modbus RTU master: ModbusRTUMaster, SimpleModbusMaster and arduino-modbus-rtu-master. 2nd one being the best, 3rd second best and first not able to receive a single message.
I have tried 9600baud 8N1 and 19200 8N1. Both resulting the same result.
Oscilloscope shows proper message and it's able to decode the message from RS485 bus and also from Arduino pin (tried Serial 1, 2, 3 and SoftwareSerial which didn't work at all - device didn't respond to request) i.e. RS485 chip in between is working fine and output (from RO) is valid and good looking (nice pulse train without overshoots or undershoots).
Slave ID:2, Function call: 3, Address: 40001, Length: 10.
Device responds valid message. This has been verified by the manufacturer as well as USB<>RS485 converter with qmodbus application. Device responds to every query.
Modbus RTU master libraries (except simple one) has been tested on pure test SW without anything else that would cause any distraction. Result exactly the same. In real environment I'm using FreeRTOS.
I've been using Arduino for quite some time for several projects. Simple and pretty large. So, I know few things but this one I cannot figure out.
There's very odd thing when I tried ModbusRTUMaster with additional debug variables. It is able to receive data but typically it is corrupt. Sometimes the slave ID is 2, sometimes 160. Function code sometimes 3, sometimes 32. Length of the response (inc CRC) should be 25 but it is 26 or 27 - alternating.
it's like Serial port has timing issue or something similar..
Any idea what could be the problem and more importantly how to solve this?
Apparently this is related to library itself as well as RS485 chip combination. Retested with ready made RS485 converter for Arduino (with MAX485 chip) and SimpleModbusMaster and now it's producing proper result without failing. SimpleModbusMaster is very old library but works well compared to more recent one. Don't know really the reason why but I don't have time to start debugging this any longer. Proper debugging possibilities are anyway missing.