Main Content

writeRead

R2026b

Write and read holding registers in single Modbus transaction

Description

moddata = writeRead(modbusClientObj,writeAddress,values,readAddress,readCount) writes and reads data from holding registers on a Modbus server connected to the client specified by modbusClientObj. The function writes values to registers starting at writeAddress, then reads from registers starting at readAddress in a single Modbus transaction. readCount specifies the number of consecutive registers to read.

The write operation is always performed before the read. Performing both operations in a single transaction avoids the risk of register values changing between separate write and read calls, and reduces communication overhead. The write and read address ranges must each be contiguous but are specified independently and need not overlap.

example

moddata = writeRead(modbusClientObj,writeAddress,values,writePrecision,readAddress,readCount,readPrecision) additionally specifies writePrecision and readPrecision to determine how the register data is interpreted for the write and read operations.

example

moddata = writeRead(___,serverId) additionally specifies serverId to send the command to a specific server.

example

Examples

collapse all

The writeRead function performs a combination of one write operation and one read operation on groups of holding registers in a single Modbus transaction. The write operation is always performed before the read. The range of addresses to read must be contiguous, and the range of addresses to write must be contiguous, but each is specified independently and may or may not overlap.

Write 2 holding registers starting at address 300, and read 4 holding registers starting at address 17250.

moddata = writeRead(m,300,[500 1000],17250,4)
moddata = 

   35647   48923   50873   60892

If the operation is successful, it returns an array of doubles, each representing a 16-bit register value, where the first value in the vector corresponds to the register value at the address specified in readAddress.

You can also use a variable for the values to write.

values = [500 1000];
moddata = writeRead(m,300,values,17250,4)
moddata =  

   35647   48923   50873   60892

Use the serverId argument to specify the address of the server to send the command to.

Write 3 holding registers starting at address 400, and read 4 holding registers starting at address 52008 from server ID 6.

moddata = writeRead(m,400,[1024 512 680],52008,4,6)
moddata =  

   38629   24735   29456   39470

Use the writePrecision and readPrecision arguments to specify the data format of the register being read from or written to on the Modbus server.

Write 3 holding registers starting at address 500, and read 6 holding registers starting at address 52008 from server ID 6. Specify a writePrecision of 'uint64' and a readPrecision of 'uint32'.

moddata = writeRead(m,500,[1024 512 680],"uint64",52008,6,"uint32",6)
moddata =  

   38629   24735   29456   39470   33434   29484

Input Arguments

collapse all

Modbus client, specified either as an icomm.interface.modbus.tcpip.Modbus or icomm.interface.modbus.serialrtu.Modbus object, based on the transport layer used for communication. You can create the client using the modbus function.

Starting address to write to, specified as a nonnegative scalar. Use 1-based addressing because the function subtracts 1 from the specified address.

Example: writeRead(m,501,[1024 512],11250,4) writes to two holding registers starting at address 501 and reads from four holding registers starting at address 11250.

Data Types: double

Array of values to write, specified as a double or array of doubles. Each value must be in the range of the specified writePrecision. For the default uint16, valid values are 0–65535.

Example: writeRead(m,200,[10 20 30 40 50],1001,2) writes five values to holding registers starting at address 200.

Data Types: double

Starting address of the holding registers to read, specified as a positive scalar. Use 1-based addressing because the function subtracts 1 from the specified address.

Example: writeRead(m,100,[500],8000,3) reads three holding registers starting at address 8000.

Data Types: double

Number of holding registers to read, specified as a positive scalar. The maximum number of values depends on the specified precision because each value can occupy one or more contiguous Modbus registers.

Example: writeRead(m,100,[500],8000,6) reads six holding registers starting at address 8000.

Data Types: double

Address of the server to send the command to, specified as a nonnegative scalar. If you do not specify a serverId, the function uses the default as 1. Valid values are 0–255, with 0 being the broadcast address.

Example: writeRead(m,400,[1024 512 680],52008,4,6) writes three holding registers starting at address 400 and reads four holding registers starting at address 52008 from server ID 6.

Data Types: double

Data format of the holding register being written to on the Modbus server, specified as a character vector or string. Valid values are 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'single', and 'double'. This argument is optional, and the default is 'uint16'. If you specify writePrecision, you must also specify readPrecision.

Note that writePrecision does not refer to the return type, which is always 'double'. It specifies how to interpret the register data.

Example: writeRead(m,400,[1024 512 680],"uint64",52008,4,"uint32",6) writes three holding registers starting at address 400 and reads four holding registers starting at address 52008 from server ID 6 with a writePrecision of 'uint64'.

Data Types: char

Data format of the holding register being read from on the Modbus server, specified as a character vector or string. Valid values are 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'single', and 'double'. This argument is optional, and the default is 'uint16'. If you specify readPrecision, you must also specify writePrecision.

Note that readPrecision does not refer to the return type, which is always 'double'. It specifies how to interpret the register data.

Example: writeRead(m,400,[1024 512 680],"uint64",52008,4,"uint32",6) writes three holding registers starting at address 400 and reads four holding registers starting at address 52008 from server ID 6 with a readPrecision of 'uint32'.

Data Types: char

Output Arguments

collapse all

Read data values, returned as a double or array of doubles.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017a

expand all