Skip to content

Commit

Permalink
Change 32bit reordering for little endian
Browse files Browse the repository at this point in the history
Trying to read a 32 bit (float) value from a certain PLC via modbus yielded the words switched. This fixes the problem in my case, but configurability may be needed since the modbus protocol doesn't define how 32 bit values should be ordered.
  • Loading branch information
snickl committed Jan 28, 2012
1 parent 081695d commit c7782b3
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,13 @@ static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
union
{
uint8_t b[4];
uint16_t s[2];
float f;
} conv;

#if BYTE_ORDER == LITTLE_ENDIAN
/* little endian */
conv.b[0] = lo & 0x00ff;
conv.b[1] = (lo >> 8) & 0x00ff;
conv.b[2] = hi & 0x00ff;
conv.b[3] = (hi >> 8) & 0x00ff;
conv.s[0] = hi;
conv.s[1] = lo;
#else
conv.b[3] = lo & 0x00ff;
conv.b[2] = (lo >> 8) & 0x00ff;
Expand Down

0 comments on commit c7782b3

Please sign in to comment.