debevv / nanoMODBUS

A compact MODBUS RTU/TCP C library for embedded/microcontrollers
MIT License
234 stars 47 forks source link

Access to "Unit Identifier" byte in NMBS_TRANSPORT_TCP mode is impossible #30

Closed arhiv6 closed 1 year ago

arhiv6 commented 1 year ago

I wanted to use the library for creation modbus TCP to RTU gateway. But in the library impossible get access to "Unit identifier" byte. In function recv_msg_header() I see reciveing this byte: nmbs->msg.unit_id (this string), but I can't access this field from outside the library.

arhiv6 commented 1 year ago

I find only one variant: I can use sometsing like as

nmbs_set_platform_arg(&nmbs, (void *) &nmbs);

before calling nmbs_server_poll(&nmbs) and

nmbs_t *nmbs = (nmbs_t *) arg; uint8_t unit_id = nmbs->msg.unit_id;

in my server request callbacks. But the documentation says:

All struct members are to be considered private, it is not advisable to read/write them directly.

It is only way?

debevv commented 1 year ago

This was a bit of an oversight, it doesn't make sense to not expose the RTU unit ID in server callbacks. Can you try this branch https://github.com/debevv/nanoMODBUS/tree/unit_id_callback and check if it's enough?

arhiv6 commented 1 year ago

Yes, that's enough.

debevv commented 1 year ago

Fixed in cbd4fa69fbc8be6d7a57a224f75f830ce16dd159