| 1 | # modbus_write_and_read_registers |
| 2 | |
| 3 | ## Name |
| 4 | |
| 5 | modbus_write_and_read_registers - write and read many registers in a single transaction |
| 6 | |
| 7 | ## Synopsis |
| 8 | |
| 9 | ```c |
| 10 | int modbus_write_and_read_registers( |
| 11 | modbus_t *ctx, |
| 12 | int write_addr, int write_nb, const uint16_t *src, |
| 13 | int read_addr, int read_nb, uint16_t *dest |
| 14 | ); |
| 15 | ``` |
| 16 | |
| 17 | ## Description |
| 18 | |
| 19 | The *modbus_write_and_read_registers()* function shall write the content of the |
| 20 | `write_nb` holding registers from the array 'src' to the address `write_addr` of |
| 21 | the remote device then shall read the content of the `read_nb` holding registers |
| 22 | to the address `read_addr` of the remote device. The result of reading is stored |
| 23 | in `dest` array as word values (16 bits). |
| 24 | |
| 25 | You must take care to allocate enough memory to store the results in `dest` |
| 26 | (at least `nb * sizeof(uint16_t)`). |
| 27 | |
| 28 | The function uses the Modbus function code 0x17 (write/read registers). |
| 29 | |
| 30 | ## Return value |
| 31 | |
| 32 | The function shall return the number of read registers if successful. Otherwise |
| 33 | it shall return -1 and set errno. |
| 34 | |
| 35 | ## Errors |
| 36 | |
| 37 | - *EINVAL*, the context is NULL, `src` or `dest` is NULL, or `write_nb` / `read_nb` is less than 1. |
| 38 | - *EMBXILVAL*, too many registers requested, Too many registers to write |
| 39 | |
| 40 | ## See also |
| 41 | |
| 42 | - [modbus_read_registers](modbus_read_registers.md) |
| 43 | - [modbus_write_register](modbus_write_register.md) |
| 44 | - [modbus_write_registers](modbus_write_registers.md) |