stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.29k stars 1.71k forks source link

Why not merge modbus_flush() directly into functions such as modbus_write(read)_xxx() function? #732

Open yanzhang920817 opened 6 months ago

yanzhang920817 commented 6 months ago

I encountered a problem: I created a TCP server with a port of 60000, which broadcasts the data sent to it and can interact with a modbus RTU slave. When I use libmodbus When API creates a modbus RTU over TCP master station A to connect to port 60000, modbus communication is normal. However, when I use modbus pull software to connect to port 60000 using modbus RTU over TCP, there will be a situation where both master station A and modbus pull data are scrambled. Ultimately, it is determined that this is because either master station A or modbus pull received data in their input cache that does not belong to them, If a modbus_flush() is added before modbus_write(read)_xxx(), this problem can be solved. Then the problem arises.

for example:

modbus_flush(ctx);
if (-1 == modbus_write_registers(ctx, addr, count, buf)) {
}

Why can't this modbus_flush() be directly encapsulated into modbus_write(read)_xxx()? So, every time we go to write or read, the data is always clean?