Modbus RTU - high&low register

Hello,

I have problem understanding dual registers and how to get single value out in modbus. Can someone explain how can I get whole number from split register in high 16-bit and low 16-bit.

Thank you

Shifting and OR'ing should help you here. Maybe something like:

unsigned long temp;

temp = high16word;
temp = temp << 16;
temp = temp | low16word;

May I ask why unsigned long would be used here if registers are signed? Would not be more logical to used signed long?

That was just an example to show how to combine 2 16-bit words into a 32-bit long word. You should be able to use a signed long as well.

Ok I see, but if I use signed intiger, than I do not get the right response. When the response should be negative number I get -1 as response. Should I change anything in the code ifI use that with signed values?

Signed and unsugned int are just of interpretation of the same set of bits. The unsigned one has more general meaning and can be used for sending both.
The only thing that you should to do for transform one to another - it simple pointer cast from uint8 to int8, for example.

uint8_t a = 0xFF;                 // unsigned byte 0xFF is 255
int8_t* b = (int8_t*) &a;    // cast pointer b to address of the a as signed
printf("B = %d", *b );         // signed byte 0XFF is -1

Thank you guys. I managed to solve the problem and is working as it should. The original solution explanation from @markd833 works. I had minor bug in the code that was causing problem.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.