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:
obj parameter is the name of the MODBUS
object. The examples assume you have created a MODBUS object,
For information on creating the object, see Create a MODBUS Connection.
writeAddress is the starting address
of the holding registers to write to, and it is a double. The
is an array of values to write. The first value in the vector is written
writeAddress. Each value must be in the
readAddress is the starting address of
the holding registers to read, and
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
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
serverId argument specifies the address
of the server to send the read command to. Valid values are
0 being the broadcast address. This argument
is optional, and the default is
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
The syntax to specify server ID is:
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
specify the data format of the register being read from or written
to on the MODBUS server. Valid values are
'double'. This argument is optional, and the
The values passed in to be written are converted to register
values based on the specified precision. For precision values
'single', each value corresponds to 2 registers,
each value corresponds to 4 registers. For
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:
If you want to use the
as well, it goes after the
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
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')