I'm having trouble receiving bytes using the libmodbus 3.1.4 package provided by Raspberry PI OS. I have also compiled the latest git sources with the same result.
As a modbus master I always and only receive bytes with value 0x00, weather any data is transmitted by a slave or not, or weather the slave is connected or not.
Example usage: (Error handling omitted)
auto ctx = modbus_new_rtu("/dev/ttyS0", 115200, 'N', 8, 1);
rc = modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP);
rc = modbus_rtu_set_custom_rts(ctx, do_rts);
modbus_set_slave(ctx, 5);
modbus_set_debug(ctx, 1);
modbus_set_response_timeout(ctx, 1, 0);
rc = modbus_connect(ctx);
modbus_flush(ctx);
// Write some registers
std::array<uint16_t, 11> drs;
drs.fill(0);
drs[4] = 1;
drs[5] = 1;
drs[6] = 1;
// auto val = dist(mt);
std::cout << fmt::format("Sending Float Value: {}\n", v);
modbus_set_float_abcd(v, drs.data() + 7);
rc = modbus_write_registers(ctx, 0, 11, drs.data());
if (rc < 0) {
std::cout << "Modbus Write Registers Error: " << modbus_strerror(errno) << "\n";
}
modbus_close(ctx);
modbus_free(ctx);
Example Debug output:
Opening /dev/ttyS0 at 115200 bauds (N, 8, 1)
Bytes flushed (0) // I call flush after connect just to be sure...
Sending Float Value: 530
[05][10][00][00][00][0B][16][00][00][00][00][00][00][00][00][00][01][00][01][00][01][04][44][00][80][00][00][00][00][6F][6F]
Sending request using RTS signal
Commanding RTS PIN to: 1
Commanding RTS PIN to: 0
Waiting for a confirmation...
<00><00><00><00><00>
ERROR CRC received 0x0 != CRC calculated 0x71C0
Modbus Write Registers Error: Invalid CRC
The data is written to the slave correctly.
On both an oscilloscope and a logic analyzer I can see the correct response being sent by the slave however the value received by libmodbus are always 0.
If I disconnect the slave I get the exact same output. In that case I would expect to get a timeout or a connection reset error.
I should also mention that a quick test using pyserial shows that the UART can and does receive bytes as sent,
I've used libmodbus in various linux projects for a number of years now and this is the first time I've ever had an issue. I have made many implementations of modbus master and slaves on raspberry PI devices, but this is my first try with the compute module.
Any advice is greatly appreciated.
Hardware: Raspberry PI Compute Module 3 on custom board.
Software: Linux raspberrypi 5.4.72-v7+ #1356, Libmodbus 3.1.4
Thanks,
Allan Irvine