1

I have used the libmodbus to build a project to connect to my device by modbus-tcp. The project run. But Unit ID is wrong. My ID slave is 0xFE. However it is fixed as 0xFF whenever I send a modbus-tcp package. I had tried to change it by function modbus_set_slave but it did not work. How can I change it?

Libmodbus: https://github.com/stephane/libmodbus

"modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);"

It's clear that I cannot put Unit ID in above function to read value a register. enter image description here

Bell Blue
  • 11
  • 2
  • Have you tried [`modbus_set_slave`](https://github.com/stephane/libmodbus/blob/master/doc/modbus_set_slave.txt) - The docs are a little confusing but the library [appears to use that](https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp.c#L116) as the Unit Identifier. – Brits Jul 14 '22 at 00:12
  • Yes, I tried it. Although I set 'req[6] = 254;', I does not work – Bell Blue Jul 14 '22 at 02:04
  • So you edited the library? (probably easier if you include a full example). It does appear that the [max slaveID is 247](https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp.c#L80) (with `0xFF` also allowed). This is technically correct for Modbus RTU (section 2.2 in the spec - 248-255 are reserved) but I can't see anything in the Modbus TCP spec on this. [This issue](https://github.com/stephane/libmodbus/issues/153) is somewhat relevant. – Brits Jul 14 '22 at 03:21
  • I have to edit the library because my device have ID = 254. I have no choice. I noticed the max slaveID, and I detected also that modbus_set_slave in the example is only used for Modbus RTU. Do you have any ideas that is useful in my case? – Bell Blue Jul 14 '22 at 07:25
  • If you have created your own version of the library then you should be able to send whatever unit ID you want to (alternatively there are alternative libraries). Have you verified that the device does respond when you send it a request with the right unit ID? – Brits Jul 14 '22 at 08:17
  • I observe on wireshark some modbus packages, which are exchange between my device and a tool. There is no doubt: the ID=254 – Bell Blue Jul 14 '22 at 08:51
  • OK - It looks like your options are modify the library, use a different library, or submit an issue and wait for the libmodbus devs to change this (if they agree to do so). If you have modified the library and it's still not working you will need to provide details of your changes (based upon a quick look I think it should only take one change [here](https://github.com/stephane/libmodbus/blob/master/src/modbus-tcp.c#L80)). – Brits Jul 14 '22 at 09:21
  • 2
    Thanks for your advices. I will let you know once the result is out – Bell Blue Jul 15 '22 at 01:37

1 Answers1

0

Actually function modbus_set_slave is the answer. The reason why it did not work is that I made a mistake with the link. Moreover, I can chang the ID unit by fixing it in other function because the library is available to modify.

Bell Blue
  • 11
  • 2