Search code examples
cmodbus

libmodbus: modbus_mapping_new() not working correctly?


I have written my own Modbus Slave based on the example (unit-test-server.c) from the libmodbus library (v3.1.4). However, it's not working because of a problem that I don't know how to solve: the modbus_mapping_new() function seems to be allocating memory for the wrong data?!

This is what I'm doing, and according to the documentation this should allocated memory for 7 holding registers:

mb_mapping = modbus_mapping_new(0, 0, 7, 0);

What's actually happening is this:

enter image description here

printf("NB_BITS = %d\nSTART_BITS = %d\nNB_INPUT_BITS = %d\nSTART_INPUT_BITS = %d\nNB_INPUT_REGISTERS = %d\nSTART_INPUT_REGISTERS = %d\nNB_REGISTERS = %d\nSTART_REGISTERS = %d\n", mb_mapping->nb_bits, mb_mapping->start_bits, mb_mapping->nb_input_bits, mb_mapping->start_input_bits, mb_mapping->nb_input_registers, mb_mapping->start_input_registers, mb_mapping->nb_registers, mb_mapping->start_registers); // DEBUG OUTPUT

^^ These are all the values that can be set in the modbus_mapping_t structure.

If there is anybody familiar with the libmodbus library here: Am I misunderstanding the documentation or what is going on here?

Right now I'm getting a SegFault because modbus_mapping_new() hasn't allocated memory for the 7 holding registers I'm writing to.


Solution

  • I just run my program on my MacBook and everything works perfectly fine! modbus_mapping_new() allocates memory for the registers (500 in this case) and Modbus Masters are retrieving the correct values from my Slave.

    enter image description here

    I guess that means that the code has a problem with the CPU architecture of the Nvidia Jetson TX2 that I run my code on originally?! How do I solve this such that it can run correctly on the Jetson too?

    Oddly enough I had the exact same code run perfectly fine on the Jetson once too, some months ago. I have absolutely no idea what might have changed ...

    [Edit]

    I finally figured it out: Somehow the libmodbus versions seem to have got mixed up. While the LIBMODBUS_VERSION_STRING variable contained "3.1.4", the latest version, there was also libmodbus 3.0.6 installed via the Ubuntu packet sources. I purged the latter from the system and reinstalled libmodbus 3.1.4 from the official git repository. Now everything works fine.