0

Hello to all programmers out there,

I think I have a basic understanding problem about the libmodbus library and I've been looking for a reasonable answer for a while but couldn't find one. Hence my question here in the forum:

Using the libmodbus library, I have written 2 applications (1 client and 1 server application) which also run on 2 separate ARM processors. Both processors are connected to each other via Ethernet and use the TCP/IP protocol to exchange data.

The Modbus client writes an integer value to a register on the Modbus server using the modbus_write_register() command. Now I would like to read out this value on the Modbus server itself. That means, I would like to have the transmitted integer value available again on the Modbus server itself.

Using the libmodbus library, what is the correct approach for this? Which libmodbus functions do I have to use for this?

I have already tried to read the value on the Modbus server using the modbus_read_registers() command and as specified in this example https://libmodbus.org/olddocs/v3.1.7/modbus_read_registers, but I only get error messages .

Is this even the right approach, can the server actually read its own registers?

Thank you all for your replies and as always: Happy Coding :) Lars

  • 1
    Either approach should be possible (but probably simpler to read from the [modbus_mapping_t](https://libmodbus.org/reference/modbus_mapping_new/)). Unfortunately without seeing your code it's difficult to help (and "I only get error messages" is less useful than full error messages). Perhaps [this test](https://github.com/stephane/libmodbus/blob/master/tests/bandwidth-server-many-up.c) might provide a starting point (`mb_mapping` will be updated by the call to [`modbus_reply`](https://libmodbus.org/reference/modbus_reply/)). – Brits May 29 '23 at 04:00
  • Hi Brits, you are right, I didn't add the error message, which is "Error reading register values: Connection timed out". – Lars Lindner May 30 '23 at 08:14
  • Sorry, would need source to go with that (currently there is not enough info to say much more). Also please note that the example I linked supports multiple simultaneous connections but, a lot of samples only support a single connection (which may be your issue). – Brits May 30 '23 at 09:54
  • Thank you again Brit for your help. My approach now is to read the values on the server directly via the modbus_mapping_t structure instead of using the modbus_read_registers() function. I think, the modbus_read_registers() function is more like to use it on the client, but not on the server! – Lars Lindner May 30 '23 at 15:22
  • "modbus_read_registers() function is more like to use it on the client" correct - however it's also possible for the server to connect to itself (a bit inefficient but sometimes useful; particularly for testing but it also keeps your server logic simple). When using `modbus_mapping_t` do be careful re thread safety (you might be reading while `modbus_reply` is updating the structure). – Brits May 30 '23 at 20:48

0 Answers0