Brad Bishop | 0f291cc | 2019-09-01 15:16:57 -0400 | [diff] [blame^] | 1 | From f1eb4bc7ccb09cd8d19ab641ee37637f8c34d16d Mon Sep 17 00:00:00 2001 |
| 2 | From: i-ky <gl.ivanovsky@gmail.com> |
| 3 | Date: Tue, 10 Jul 2018 15:58:45 +0300 |
| 4 | Subject: [PATCH] Fixed MODBUS_GET_* macros in case of negative values |
| 5 | |
| 6 | 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. |
| 7 | --- |
| 8 | src/modbus.h | 10 +++++----- |
| 9 | 1 file changed, 5 insertions(+), 5 deletions(-) |
| 10 | |
| 11 | diff --git a/src/modbus.h b/src/modbus.h |
| 12 | index f6e9a5f5..c63f5ceb 100644 |
| 13 | --- a/src/modbus.h |
| 14 | +++ b/src/modbus.h |
| 15 | @@ -245,12 +245,12 @@ MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, |
| 16 | #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF) |
| 17 | #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF) |
| 18 | #define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \ |
| 19 | - (((int64_t)tab_int16[(index) ] << 48) + \ |
| 20 | - ((int64_t)tab_int16[(index) + 1] << 32) + \ |
| 21 | - ((int64_t)tab_int16[(index) + 2] << 16) + \ |
| 22 | + (((int64_t)tab_int16[(index) ] << 48) | \ |
| 23 | + ((int64_t)tab_int16[(index) + 1] << 32) | \ |
| 24 | + ((int64_t)tab_int16[(index) + 2] << 16) | \ |
| 25 | (int64_t)tab_int16[(index) + 3]) |
| 26 | -#define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1]) |
| 27 | -#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1]) |
| 28 | +#define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) | tab_int16[(index) + 1]) |
| 29 | +#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) | tab_int8[(index) + 1]) |
| 30 | #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \ |
| 31 | do { \ |
| 32 | tab_int8[(index)] = (value) >> 8; \ |