Reputation: 346
I have a Linux machine, On Modbus RTU(over rs485) multiple devices are connected to this Linux machine. I do not know the addresses of connected devices. I want to get a list of connected devices. Is there any way to get all connected devices?
I am using libmodbus.
here is code to read to one device with a known address.
modbus_t *modbusCtxPtr=NULL;
int modbus_id=9; // Modbus address
modbusCtxPtr = modbus_new_rtu(TTYDEVICE, BAUDRATE, 'N', 8, STOPBITS);
if (modbusCtxPtr == NULL)
{
errorAndLogLogger(TTY_ERROR, "ERROR - Unable to create the libmodbus context.");
return;
}
if (modbus_set_slave(modbusCtxPtr, modbus_id) == -1)
errorAndLogLogger(TTY_ERROR, "ERROR - Error in setting slave id.");
if (modbus_connect(modbusCtxPtr) == -1)
errorAndLogLogger(TTY_ERROR, "ERROR - Modbus Connection failed.");
/* Here, I called read register over Modbus, function. */
Upvotes: 2
Views: 3580
Reputation: 312
Modbus avoids collisions by only letting the slaves talk if they've first been addressed by the master. That means that there's no way to broadcast a message or passively listen like you would with Ethernet. You can poll each address like you and @Marker said. Just pay attention to the error code and set the timeout to 1 or 2 sec.
The best bet would be to trace out the serial link and see what all devices are on the network. You'd know how many slaves to expect when polling, and you'd the manufacturer and model info. With that info you can get the manual and know what each register is for. If the device has a keypad style interface, you might could also find out the slave address.
Upvotes: 3