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

Multiple slaves on the same port. #740

Open phu251099 opened 3 months ago

phu251099 commented 3 months ago

Currently I am facing the following problem:

I have 3 slaves with 3 IDs 1, 2, 3 connected to 1 modbus rtu COM port.

I don't know how to read and write data to 3 devices.

When using the following functions, communication with only 1 address is possible: ctx = modbus_new_rtu("/dev/ttyS3", 115200, 'N', 8, 1); modbus_set_slave(ctx, 1); modbus_read_registers(ctx, 0, 2, tab_reg);

mhei commented 3 months ago

What about using?

ctx = modbus_new_rtu("/dev/ttyS3", 115200, 'N', 8, 1);
modbus_set_slave(ctx, 1);
modbus_read_registers(ctx, 0, 2, tab_reg);
...
modbus_set_slave(ctx, 2);
modbus_read_registers(ctx, 0, 2, tab_reg);
...
modbus_set_slave(ctx, 3);
modbus_read_registers(ctx, 0, 2, tab_reg);
...