Write and Read Multiple Holding Registers

The writeRead function is used to perform 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 may or may not 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 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, and it is 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 address specified in readAddress.

This example writes 2 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 array of values in the function syntax, as shown above. The same example could be written this way, using a variable for the 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

What some devices refer to as a slaveID property, may work as a serverID property in the MODBUS interface. For some manufacturers a slave ID is sometimes referred to as a server ID. 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 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 2 registers, and for 'uint64', 'int64' and 'double', each value corresponds to 4 registers. For 'int16' and 'uint16', each value is from a single 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 or 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 2 holding registers starting at address 919, and writes 3 holding registers starting at address 719, formatting read and write for single precision data registers.

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