diff --git a/doc/libmodbus.txt b/doc/libmodbus.txt index f8597d0..6bcdf06 100644 --- a/doc/libmodbus.txt +++ b/doc/libmodbus.txt @@ -143,12 +143,18 @@ Macros for data manipulation:: - MODBUS_GET_HIGH_BYTE(data), extracts the high byte from a byte - MODBUS_GET_LOW_BYTE(data), extracts the low byte from a byte +- MODBUS_GET_INT64_FROM_INT16(tab_int16, index), builds an int64 from the four + first int16 starting at tab_int16[index] - MODBUS_GET_INT32_FROM_INT16(tab_int16, index), builds an int32 from the two first int16 starting at tab_int16[index] - MODBUS_GET_INT16_FROM_INT8(tab_int8, index), builds an int16 from the two first int8 starting at tab_int8[index] - MODBUS_SET_INT16_TO_INT8(tab_int8, index, value), set an int16 value into the two first bytes starting at tab_int8[index] +- MODBUS_SET_INT32_TO_INT16(tab_int16, index, value), set an int32 value into + the two first int16 starting at tab_int16[index] +- MODBUS_SET_INT64_TO_INT16(tab_int16, index, value), set an int64 value into + the four first int16 starting at tab_int16[index] Handling of bits and bytes:: linkmb:modbus_set_bits_from_byte[3] diff --git a/src/modbus.h b/src/modbus.h index 4381aad..1207de3 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -230,6 +230,11 @@ MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF) #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF) +#define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \ + (((int64_t)tab_int16[(index) ] << 48) + \ + ((int64_t)tab_int16[(index) + 1] << 32) + \ + ((int64_t)tab_int16[(index) + 2] << 16) + \ + (int64_t)tab_int16[(index) + 3]) #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1]) #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1]) #define MODBUS_SET_INT16_TO_INT8(tab_int8, index, value) \ @@ -237,6 +242,18 @@ MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, tab_int8[(index)] = (value) >> 8; \ tab_int8[(index) + 1] = (value) & 0xFF; \ } while (0) +#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \ + do { \ + tab_int16[(index) ] = (value) >> 16; \ + tab_int16[(index) + 1] = (value); \ + } while (0) +#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \ + do { \ + tab_int16[(index) ] = (value) >> 48; \ + tab_int16[(index) + 1] = (value) >> 32; \ + tab_int16[(index) + 2] = (value) >> 16; \ + tab_int16[(index) + 3] = (value); \ + } while (0) MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value); MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits,