Main Content

Write and Read Multiple Holding Registers

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 and the range of addresses to write must be contiguous, but each is specified independently and they can overlap.

The syntax for the write-read operation to holding registers is:

writeRead(obj,writeAddress,values,readAddress,readCount)

The obj parameter is the name of the Modbus object. The following examples assume you have created a Modbus object, m. For information on creating the object, see Create a Modbus Connection.

The writeAddress is the starting address of the holding registers to write to, specified as a double. The values parameter is an array of values to write. The first value in the vector is written to the writeAddress. Each value must be in the range 0–65535.

The readAddress is the starting address of the holding registers to read, and readCount is the number of registers to read.

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.

This example writes two holding registers starting at address 601, and reads 4 holding registers starting at address 19250.

writeRead(m,601,[1024 512],19250,4)
ans = 

   27640   60013   51918   62881

You can optionally create variables for the values to be written, instead of including the literal array of values in the function syntax. The same example could be written this way, using a variable for the write values:

values = [1024 512];
writeRead(m,601,values,19250,4)
ans = 

   27640   60013   51918   62881

Server ID Option

The serverId argument specifies the address of the server to send the read command to. Valid values are 0–247, with 0 being the broadcast address. This argument is optional, and the default is 1.

Note

If your device uses a slaveID property, it might work to use it as the serverID property with the writeRead command as described here.

The syntax to specify a server ID is:

writeRead(obj,writeAddress,values,readAddress,readCount,serverId)

This example writes 3 holding registers starting at address 400, and reads 4 holding registers starting at address 52008, from server ID 6.

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

   38629   84735   29456   39470

Precision Option

The 'writePrecision' and 'readPrecision' arguments specify the data format of the register being read from or written to on the Modbus server. Valid values are 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'single', and 'double'. This argument is optional, and the default is 'uint16'.

The values passed in to be written are converted to register values based on the specified precision. For precision values 'int32', 'uint32', and 'single', each value corresponds to two registers, and for 'uint64', 'int64' and 'double', each value corresponds to four registers. For 'int16' and 'uint16', each value is from one 16-bit register.

Note that precision specifies how to interpret or convert the register data, not the return type of the read operation. The data returned is always of type double.

The syntax for designating the write and read precision is:

writeRead(obj,writeAddress,values,writePrecision,readAddress,readCount,readPrecision)

If you want to use the serverId argument as well, it goes after the readPrecision.

This example writes 3 holding registers starting at address 400 and reads 4 holding registers starting at address 52008, from server ID 6. It also specifies a writePrecision of 'uint64' and a readPrecision of 'uint32'.

writeRead(m,400,[1024 512 680],'uint64',52008,4,'uint32',6)
ans = 

   38629   84735   29456   39470

This example reads two holding registers starting at address 919, and writes 3 holding registers starting at address 719, formatting the read and write values for single precision data registers.

  values = [1.14 5.9 11.27];
  writeRead(m,719,values,'single',919,2,'single')