Skip to content

Commit 6ee7b3f

Browse files
Only load each wide integer once in MODBUS_SET_INT*_TO_INT*() macros
If you have a union like this as part of the read data: union { uint8_t serial[6]; uint16_t serial_raw[3]; }; and do the obvious for(size_t i = 0; i < sizeof(rdng.serial_raw) / sizeof(*rdng.serial_raw); ++i) { MODBUS_SET_INT16_TO_INT8(rdng.serial, i*2, rdng.serial_raw[i]); } then because serial_raw[i] is updated through serial[i] at first, then loaded again, you end up with rdng.serial[i*2]=rdng.serial[i*2+1] for all i, which is both confusing and wrong; instead, you must do for(size_t i = 0; i < sizeof(rdng.serial_raw) / sizeof(*rdng.serial_raw); ++i) { uint16_t r = rdng.serial_raw[i]; MODBUS_SET_INT16_TO_INT8(rdng.serial, i*2, rdng.serial_raw[i]); } but there's really no reason to require this, and this patch lets you use them directly
1 parent b25629b commit 6ee7b3f

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/modbus.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,25 @@ MODBUS_API int modbus_disable_quirks(modbus_t *ctx, unsigned int quirks_mask);
283283
(((int32_t) tab_int16[(index)] << 16) | (int32_t) tab_int16[(index) + 1])
284284
#define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) \
285285
(((int16_t) tab_int8[(index)] << 8) | (int16_t) tab_int8[(index) + 1])
286-
#define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
287-
do { \
288-
((int8_t *) (tab_int8))[(index)] = (int8_t) ((value) >> 8); \
289-
((int8_t *) (tab_int8))[(index) + 1] = (int8_t) (value); \
286+
#define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \
287+
do { \
288+
uint16_t _val = (value); \
289+
((int8_t *) (tab_int8))[(index)] = (int8_t) (_val >> 8); \
290+
((int8_t *) (tab_int8))[(index) + 1] = (int8_t) _val; \
290291
} while (0)
291-
#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \
292-
do { \
293-
((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 16); \
294-
((int16_t *) (tab_int16))[(index) + 1] = (int16_t) (value); \
292+
#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \
293+
do { \
294+
uint32_t _val = (value); \
295+
((int16_t *) (tab_int16))[(index)] = (int16_t) (_val >> 16); \
296+
((int16_t *) (tab_int16))[(index) + 1] = (int16_t) _val; \
295297
} while (0)
296-
#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \
297-
do { \
298-
((int16_t *) (tab_int16))[(index)] = (int16_t) ((value) >> 48); \
299-
((int16_t *) (tab_int16))[(index) + 1] = (int16_t) ((value) >> 32); \
300-
((int16_t *) (tab_int16))[(index) + 2] = (int16_t) ((value) >> 16); \
301-
((int16_t *) (tab_int16))[(index) + 3] = (int16_t) (value); \
298+
#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \
299+
do { \
300+
uint64_t _val = (value); \
301+
((int16_t *) (tab_int16))[(index)] = (int16_t) (_val >> 48); \
302+
((int16_t *) (tab_int16))[(index) + 1] = (int16_t) (_val >> 32); \
303+
((int16_t *) (tab_int16))[(index) + 2] = (int16_t) (_val >> 16); \
304+
((int16_t *) (tab_int16))[(index) + 3] = (int16_t) _val; \
302305
} while (0)
303306

304307
MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value);

0 commit comments

Comments
 (0)