Commit 1776a3503c4b75b049fb7ca446a962a9f142286a
1 parent
8a7e2446
Add some more macros for data manipulation
Signed-off-by: Michael Heimpold <mhei@heimpold.de>
Showing
2 changed files
with
23 additions
and
0 deletions
doc/libmodbus.txt
| ... | ... | @@ -143,12 +143,18 @@ Macros for data manipulation:: |
| 143 | 143 | |
| 144 | 144 | - MODBUS_GET_HIGH_BYTE(data), extracts the high byte from a byte |
| 145 | 145 | - MODBUS_GET_LOW_BYTE(data), extracts the low byte from a byte |
| 146 | +- MODBUS_GET_INT64_FROM_INT16(tab_int16, index), builds an int64 from the four | |
| 147 | + first int16 starting at tab_int16[index] | |
| 146 | 148 | - MODBUS_GET_INT32_FROM_INT16(tab_int16, index), builds an int32 from the two |
| 147 | 149 | first int16 starting at tab_int16[index] |
| 148 | 150 | - MODBUS_GET_INT16_FROM_INT8(tab_int8, index), builds an int16 from the two |
| 149 | 151 | first int8 starting at tab_int8[index] |
| 150 | 152 | - MODBUS_SET_INT16_TO_INT8(tab_int8, index, value), set an int16 value into |
| 151 | 153 | the two first bytes starting at tab_int8[index] |
| 154 | +- MODBUS_SET_INT32_TO_INT16(tab_int16, index, value), set an int32 value into | |
| 155 | + the two first int16 starting at tab_int16[index] | |
| 156 | +- MODBUS_SET_INT64_TO_INT16(tab_int16, index, value), set an int64 value into | |
| 157 | + the four first int16 starting at tab_int16[index] | |
| 152 | 158 | |
| 153 | 159 | Handling of bits and bytes:: |
| 154 | 160 | linkmb:modbus_set_bits_from_byte[3] | ... | ... |
src/modbus.h
| ... | ... | @@ -230,6 +230,11 @@ MODBUS_API int modbus_reply_exception(modbus_t *ctx, const uint8_t *req, |
| 230 | 230 | |
| 231 | 231 | #define MODBUS_GET_HIGH_BYTE(data) (((data) >> 8) & 0xFF) |
| 232 | 232 | #define MODBUS_GET_LOW_BYTE(data) ((data) & 0xFF) |
| 233 | +#define MODBUS_GET_INT64_FROM_INT16(tab_int16, index) \ | |
| 234 | + (((int64_t)tab_int16[(index) ] << 48) + \ | |
| 235 | + ((int64_t)tab_int16[(index) + 1] << 32) + \ | |
| 236 | + ((int64_t)tab_int16[(index) + 2] << 16) + \ | |
| 237 | + (int64_t)tab_int16[(index) + 3]) | |
| 233 | 238 | #define MODBUS_GET_INT32_FROM_INT16(tab_int16, index) ((tab_int16[(index)] << 16) + tab_int16[(index) + 1]) |
| 234 | 239 | #define MODBUS_GET_INT16_FROM_INT8(tab_int8, index) ((tab_int8[(index)] << 8) + tab_int8[(index) + 1]) |
| 235 | 240 | #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, |
| 237 | 242 | tab_int8[(index)] = (value) >> 8; \ |
| 238 | 243 | tab_int8[(index) + 1] = (value) & 0xFF; \ |
| 239 | 244 | } while (0) |
| 245 | +#define MODBUS_SET_INT32_TO_INT16(tab_int16, index, value) \ | |
| 246 | + do { \ | |
| 247 | + tab_int16[(index) ] = (value) >> 16; \ | |
| 248 | + tab_int16[(index) + 1] = (value); \ | |
| 249 | + } while (0) | |
| 250 | +#define MODBUS_SET_INT64_TO_INT16(tab_int16, index, value) \ | |
| 251 | + do { \ | |
| 252 | + tab_int16[(index) ] = (value) >> 48; \ | |
| 253 | + tab_int16[(index) + 1] = (value) >> 32; \ | |
| 254 | + tab_int16[(index) + 2] = (value) >> 16; \ | |
| 255 | + tab_int16[(index) + 3] = (value); \ | |
| 256 | + } while (0) | |
| 240 | 257 | |
| 241 | 258 | MODBUS_API void modbus_set_bits_from_byte(uint8_t *dest, int idx, const uint8_t value); |
| 242 | 259 | MODBUS_API void modbus_set_bits_from_bytes(uint8_t *dest, int idx, unsigned int nb_bits, | ... | ... |