Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read unsigned int 32 bit #654

Open
LucaDavolio opened this issue Sep 6, 2022 · 2 comments
Open

How to read unsigned int 32 bit #654

LucaDavolio opened this issue Sep 6, 2022 · 2 comments

Comments

@LucaDavolio
Copy link

Hi,
I tried to read a 32 bit word and, as I imagined, it gave me a wrong value.
Is there a way to read 32 bit unsigned integers?
Thanks.

@jfsimon1981
Copy link

Hi,

Check how the device manufacturer implemented it, and reconstruct the 32bit word at your application level from the UINT16 which i believe is the highest bits Modbus supports.

Manufacturer will define how they send data such as signed values, or others. Though for your case you stick the two parts together and reinterpret as 32bit.

Code snippet just to give you an idea

uint32_t data_out;
data_out = (((uint32_t)register_buffer_32b[i] << 16) | uint32_t)register_buffer_32b[i+1]));

@modem-man-gmx
Copy link

my very simplified way of reading 32bit is:

uint16_t Buffer[2];
modbus_read_registers( mbctx, RegAddress, 2, &Buffer[0] );
uint32_t i32Value = MODBUS_GET_INT32_FROM_INT16( Buffer, 0 );

I would not do the bit fiddling by hand, it's already in the libmodbus, have 3.1.6, but am sure it's since longer time there.

I'm waiting a bit for modbus_get_uint32_abcd(), modbus_get_uint32_dcba(), modbus_get_uint32_cdab(), and the like, similar to float. But to be honest, I never got my hands on a real device which needs the MSB/LSB flipping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants