API read register functions for RS485 Daisy-Chain connections

59 views
Skip to first unread message

Linux programmer

unread,
Jun 2, 2020, 8:36:27 PM6/2/20
to libmodbus
Hi,

None of read register functions have an argument for RS485 ID for Daisy-Chain topology communication, how can I read / request registers from one particular RS485 ID?

Thank you.

Kind regards,

- j

Patrick Boettcher

unread,
Jun 3, 2020, 3:42:30 AM6/3/20
to Linux programmer, libm...@googlegroups.com
Hi,

if you mean slave-id, you need to use

modbus_set_slave(ctx, <ID>);

on an open context before reading or writing.

HTH,
--
Patrick.

Linux programmer

unread,
Jun 3, 2020, 7:03:14 AM6/3/20
to libmodbus


On Wednesday, June 3, 2020 at 5:42:30 PM UTC+10, Patrick Boettcher wrote:
Hi,

if you mean slave-id, you need to use

  modbus_set_slave(ctx, <ID>);

on an open context before reading or writing.

Correct, slave-id, so if I need to read 3 slave_id input registers, I need to call  modbus_set_slave(ctx, <ID>) three times before calling modbus_read_input_registers, right?

Thank you.

Felipe villamil

unread,
Jun 3, 2020, 7:17:06 AM6/3/20
to libm...@googlegroups.com
I don't know if I understand well your question but I will explain the two possible scenarios:

1.  you want to read three input registers of a device with slave id = SLAVE_ID.
  • set the slave id of your context by using modbus_set_slave(ctx, <SLAVE_ID>) just once
  • Use modbus_read_input_registers to read the three input registers you want to read
2.  you want to read an input register called slave_id, actualy containing the id of the device, for three different devices.
  • set the slave id of your context by using modbus_set_slave(ctx, <SLAVE_ID_first_device>)
  • Use modbus_read_input_registers to read the input register you want to read
  • Repeat both steps above for the rest of the devices
cheers,

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Cette discussion peut être lue sur le Web à l'adresse https://groups.google.com/d/msgid/libmodbus/e8ce183a-dd52-47a3-8d8a-595d67776666%40googlegroups.com.


--
Andrés Felipe Villamil
Ingénieur Institut Mines Nantes Atlantique
(Ex. Écoles des Mines de Nantes)

Linux programmer

unread,
Jun 6, 2020, 2:28:16 AM6/6/20
to libmodbus
Thanks Felipe, so if there is 32 slaves, before running scenario (2), it should read all slave IDs by broadcast:

rc = modbus_set_slave(context, MODBUS_BROADCAST_ADDRESS);

rc = modbus_read_input_registers(context, idAddress, 32, slaveIdList);

How cab I be sure all slaves responded before I run scenario (2) to poll each slave's register?

Thank you.

Kind regards,

- j

On Wednesday, June 3, 2020 at 9:17:06 PM UTC+10, Felipe villamil wrote:
I don't know if I understand well your question but I will explain the two possible scenarios:

1.  you want to read three input registers of a device with slave id = SLAVE_ID.
  • set the slave id of your context by using modbus_set_slave(ctx, <SLAVE_ID>) just once
  • Use modbus_read_input_registers to read the three input registers you want to read
2.  you want to read an input register called slave_id, actualy containing the id of the device, for three different devices.
  • set the slave id of your context by using modbus_set_slave(ctx, <SLAVE_ID_first_device>)
  • Use modbus_read_input_registers to read the input register you want to read
  • Repeat both steps above for the rest of the devices
cheers,
UT_REGISTERS_ADDRESS
On Wed, Jun 3, 2020 at 1:03 PM Linux programmer <jupit...@gmail.com> wrote:


On Wednesday, June 3, 2020 at 5:42:30 PM UTC+10, Patrick Boettcher wrote:
Hi,

if you mean slave-id, you need to use

  modbus_set_slave(ctx, <ID>);

on an open context before reading or writing.

Correct, slave-id, so if I need to read 3 slave_id input registers, I need to call  modbus_set_slave(ctx, <ID>) three times before calling modbus_read_input_registers, right?

Thank you.

HTH,
--
Patrick.

On Tue, 2 Jun 2020 17:36:27 -0700 (PDT)
Linux programmer <jupit...@gmail.com> wrote:

> Hi,
>
> None of read register functions have an argument for RS485 ID for
> Daisy-Chain topology communication, how can I read / request
> registers from one particular RS485 ID?
>
> Thank you.
>
> Kind regards,
>
> - j
>

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libm...@googlegroups.com.

Felipe villamil

unread,
Jun 6, 2020, 9:19:36 AM6/6/20
to libm...@googlegroups.com
Hey! As far as I know a Modbus RTU slave should not reply to a broadcast message. In other words, broadcast messages are specifically used for writing purposes. Here you can find a thread about the subject


Cheers,


Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Cette discussion peut être lue sur le Web à l'adresse https://groups.google.com/d/msgid/libmodbus/11d73d34-5db5-4f0a-978d-fbb523ea71f5o%40googlegroups.com.

Chuck Watson

unread,
Jun 6, 2020, 10:43:24 AM6/6/20
to libm...@googlegroups.com

If you think about it, the slaves can’t reply to a broadcast. The MODBUS protocol is master-slave only.

 

How do all the slaves reply to the broadcast at the same time? They would need much more smarts than the protocol provides. They would need some kind of collision avoidance.

 

Furthermore, the master would need to know where these unsolicited replies are coming from.

 

What I have seen a broadcast used for is to tell all slaves to capture a reading value, or multiple values, at the same point in time and save the captured values to a register. Then the master requests the captured value from the register at a later time using the standard master-slave protocol.

Linux programmer

unread,
Jun 6, 2020, 9:28:34 PM6/6/20
to libmodbus


On Sunday, June 7, 2020 at 12:43:24 AM UTC+10, Chuck Watson wrote:

If you think about it, the slaves can’t reply to a broadcast. The MODBUS protocol is master-slave only.

 

Fair enough, but how can the master to get the salve id to run modbus_set_slave(context, slaveId) in the first place? Let's say that the slaveId (rs485Id) is installed in an input register address 0x401 for all 32 salve devices (devices could be changed randomly), if the master calls modbus_read_input_registers(context, 0x401, 1, slaveIdList) without calling modbus_set_slave(context, slaveId) (no slaveId predefined), how does the slaves respond the master request?



--

Andrés Felipe Villamil

Ingénieur Institut Mines Nantes Atlantique

(Ex. Écoles des Mines de Nantes)

 

--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".

Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libm...@googlegroups.com.

Chuck Watson

unread,
Jun 7, 2020, 11:58:08 AM6/7/20
to libm...@googlegroups.com

The master needs to know the address’ for all the slaves. Normally this is configured into the master at commissioning time by the technician.

 

The other way is you can run a routine to check each MODBUS address (1-255) for an active slave device. You can reduce this number by restricting the slave address range.

 

Frequently though this is not a preferred method though because it takes time. You need to send a poll out and wait for the slave to respond, or not, before moving on to the next possible device address. The other issue is how do you know who is responding to address 23 for example? And if you know who is at address 23, you are back to the master knowing all devices on the network.

 

Somebody has to configure all the slaves with a unique address. It does not do much good to put the address into a MODBUS register on the slave because you cant read the address unless you know the address!!

Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Cette discussion peut être lue sur le Web à l'adresse https://groups.google.com/d/msgid/libmodbus/7d94d763-aad1-4910-b81d-98c23b4fd375o%40googlegroups.com.

Linux programmer

unread,
Jul 9, 2020, 3:14:00 AM7/9/20
to libmodbus
Hi Chuck,


On Monday, June 8, 2020 at 1:58:08 AM UTC+10, Chuck Watson wrote:

The master needs to know the address’ for all the slaves. Normally this is configured into the master at commissioning time by the technician.

 

Let's say I am the technician, how can I configure multiple same modbus devices to daisy chain slave connections? Where is the slave address to assign each device?

The other way is you can run a routine to check each MODBUS address (1-255) for an active slave device. You can reduce this number by restricting the slave address range.


 I can run my laptop as master to connect to a modbus device as one slave, I can directly request to an address, I got a response. Now, if I connect to two modbus devices in a dasiy chain slave connections, because both modbus devices are defined the same register addresses, if I pull input register 0x100 and I have a response, which one sent the response?

 

Frequently though this is not a preferred method though because it takes time. You need to send a poll out and wait for the slave to respond, or not, before moving on to the next possible device address. The other issue is how do you know who is responding to address 23 for example? And if you know who is at address 23, you are back to the master knowing all devices on the network.


As above, all my slaves are defined the same register map, how can I distinguish them?

 

Somebody has to configure all the slaves with a unique address. It does not do much good to put the address into a MODBUS register on the slave because you cant read the address unless you know the address!!


Understood, are there a document how to configure the daisy chain slave id?

Thank you very much.




Richard Ash

unread,
Jul 12, 2020, 12:23:41 PM7/12/20
to Linux programmer, libm...@googlegroups.com
On Thu, 9 Jul 2020 00:14:00 -0700 (PDT)
Linux programmer <jupit...@gmail.com> wrote:

> Hi Chuck,
>
>
> On Monday, June 8, 2020 at 1:58:08 AM UTC+10, Chuck Watson wrote:
> >
> > The master needs to know the address’ for all the slaves. Normally
> > this is configured into the master at commissioning time by the
> > technician.
> >
> Let's say I am the technician, how can I configure multiple same
> modbus devices to daisy chain slave connections? Where is the slave
> address to assign each device?

You have to decide what address each modbus device will use. So you
decide that the temperature sensor modbus device for input water is
address 2, outlet water will be address 3, and the air sensor modbus
device will use address 3.

Then you have to consult the documentation for each modbus device to
find out how to set it's address. It might have a DIP switch, or some
vendor-provided software for modifying settings. But they will all have
a field for address, which you set correctly - at the same time as you
select the correct baud rate, parity and stop bits settings.

> The other way is you can run a routine to check each MODBUS address
> (1-255)
> > for an active slave device. You can reduce this number by
> > restricting the slave address range.
> >
>
> I can run my laptop as master to connect to a modbus device as one
> slave, I can directly request to an address, I got a response. Now,
> if I connect to two modbus devices in a dasiy chain slave
> connections, because both modbus devices are defined the same
> register addresses, if I pull input register 0x100 and I have a
> response, which one sent the response?

It depends what calls to modbus_set_slave() you have made, as this sets
the modbus device address.
https://libmodbus.org/docs/v3.1.6/modbus_set_slave.html

The response will come from whatever device you most recently set with
this call.

> As above, all my slaves are defined the same register map, how can I
> distinguish them?
By the modbus device address you set on them, see above. The device
addresses must be unique for every device connected to the same
physical network.

Richard
Reply all
Reply to author
Forward
0 new messages