---
name: "modbus_write_and_read_registers"
section: "3"
platform: "linux"
package: "libmodbus-dev"
version: "3.1.6-2.1"
release: "Debian 12 (Bookworm)"
platforms: ["linux"]
url: "https://new.carta.tech/modbus_write_and_read_registers"
---

# modbus\_write\_and\_read\_registers(3)

write and read many registers in a single transaction

## NAME

modbus\_write\_and\_read\_registers - write and read many registers in a single transaction

## SYNOPSIS

**int modbus\_write\_and\_read\_registers(modbus\_t \****ctx***, int** *write\_addr***, int** *write\_nb***, const uint16\_t \****src***, int** *read\_addr***, int** *read\_nb***, const uint16\_t \****dest***);**

## DESCRIPTION

The **modbus\_write\_and\_read\_registers()** function shall write the content of the *write\_nb* holding registers from the array *src* to the address *write\_addr* of the remote device then shall read the content of the *read\_nb* holding registers to the address *read\_addr* of the remote device. The result of reading is stored in *dest* array as word values (16 bits).

You must take care to allocate enough memory to store the results in *dest* (at least *nb* \* sizeof(uint16\_t)).

The function uses the Modbus function code 0x17 (write/read registers).

## RETURN VALUE

The function shall return the number of read registers if successful. Otherwise it shall return -1 and set errno.

## ERRORS

**EMBMDATA**

Too many registers requested, Too many registers to write

## SEE ALSO

[**modbus\_read\_registers**(3)](/modbus_read_registers/3.md) [**modbus\_write\_register**(3)](/modbus_write_register/3.md) [**modbus\_write\_registers**(3)](/modbus_write_registers/3.md)

## AUTHORS

The libmodbus documentation was written by Stéphane Raimbault \<stephane.raimbault@gmail.com\>
