5

I have some modbus TCP code written under pymodbus 1.2 the relevent code was

result = modbus_client.read_holding_registers(40093, 3)

After updating to pymodbus 1.4.0 it wouldn't work until I cargo culted the new unit parameter into the function call (teh examples all had unit=1 in them):

result = modbus_client.read_holding_registers(40093, 3, unit=1)

what does the unit parameter in pymodbus read_ holding_registers() mean? I can't seem to find an explanation anywhere. The source says ":param unit: The slave unit this request is targeting", but I don't understand what this means, nor what selection other than 1 might be used.

Joshua Clayton
  • 1,669
  • 18
  • 29
  • This is the modbus slave ID that might be you have multiple chained modbus slave/server that each slaves has a unique `unit_ID`. – Benyamin Jafari Oct 12 '18 at 22:56

1 Answers1

6

The Modbus protocol was originally developed long before TCP/IP was popular (late 70s I think). It was used on serial connections mostly. Some serial hardware protocols like RS485 allow daisy-chaining. The modbus master (in your case Python) can poll many slaves on a single serial port. Only the slave that was requested will respond. The address of the slave is the Unit in this case. Once Modbus was adapted to TCP/IP, the protocol allowed this "unit address" to be used to create multiple slaves behind a single IP address. Most of the time, if using TCP/IP there is a single address of 1. On Wikipedia they refer to this as "Station address."

I'm not sure why you would need to include this in the call to the method since it is a kwarg that is defaulted to 1 anyway.

  • Thanks for the response. It looks like it is defaulted to 0 the latest version on github. That seems like a bug, at least for tcp. I'll need to look again tomorrow. – Joshua Clayton May 16 '18 at 05:47
  • I believe unit=0 is a valid address. If that is true this wouldn't be a bug. If your slave has a unit address of 1 you will need to use the unit kwarg. – Adam Solchenberger May 16 '18 at 12:32
  • I read that 0 means send to every slave on modbus RTU. I have no idea about TCP. It seems vestigial, but I have only been using modbus for a few months, so what do I know. – Joshua Clayton May 16 '18 at 17:02
  • 1
    @AdamSolchenberger `unit_ID` is same in modbustcp or modbusrtu, this is the `slave_ID` – Benyamin Jafari Oct 12 '18 at 22:52
  • I just recently ran across a case where the modbus tcp slave is itself a modbus rtu master, and exposes its own slaves to tcp, so the slave id is needed despite connecting to a single ip address. And with a concrete example, all is clear. – Joshua Clayton Oct 15 '18 at 15:38