Commit f1eb4bc7ccb09cd8d19ab641ee37637f8c34d16d

Authored by i-ky
Committed by Stéphane Raimbault
1 parent 820e15fa

Fixed MODBUS_GET_* macros in case of negative values

In case resulting value should be negative it is incorrect to use '+' operator to construct it from pieces, because highest bytes will result in negative number after bitwise shift while others will stay positive. Replacing addition with '|' should solve the issue.
Showing 1 changed file with 5 additions and 5 deletions
src/modbus.h
@@ -245,12 +245,12 @@ MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, @@ -245,12 +245,12 @@ MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
245 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF) 245 #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF)
246 #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF) 246 #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF)
247 #define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \ 247 #define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \
248 - (((int64_t)tab_int16[(index) ] << 48) + \  
249 - ((int64_t)tab_int16[(index) + 1] << 32) + \  
250 - ((int64_t)tab_int16[(index) + 2] << 16) + \ 248 + (((int64_t)tab_int16[(index) ] << 48) | \
  249 + ((int64_t)tab_int16[(index) + 1] << 32) | \
  250 + ((int64_t)tab_int16[(index) + 2] << 16) | \
251 (int64_t)tab_int16[(index) + 3]) 251 (int64_t)tab_int16[(index) + 3])
252 -#define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1])  
253 -#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1]) 252 +#define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) | tab_int16[(index) + 1])
  253 +#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) | tab_int8[(index) + 1])
254 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \ 254 #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
255 do { \ 255 do { \
256 tab_int8[(index)] = (value) >> 8; \ 256 tab_int8[(index)] = (value) >> 8; \