Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

writeRead

Perform a write then read operation on groups of holding registers in a single MODBUS transaction

Syntax

writeRead(m,writeAddress,values,readAddress,readCount)
writeRead(m,writeAddress,values,readAddress,readCount,serverId)
writeRead(m,writeAddress,values,writePrecision,readAddress,readCount,readPrecision)

Description

example

writeRead(m,writeAddress,values,readAddress,readCount) writes data to MODBUS object m at the starting address writeAddress using the values to write values, and then reads data at the starting address readAddress using the number of values to read readCount.

This 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.

example

writeRead(m,writeAddress,values,readAddress,readCount,serverId) additionally uses the serverId as the address of the server to send the command to.

example

writeRead(m,writeAddress,values,writePrecision,readAddress,readCount,readPrecision) adds optional precisions for the write and read operations. The writePrecision and readPrecision arguments specify the data format of the register being read from or written to on the MODBUS server.

Examples

collapse all

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 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.

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

   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 optionally create variables for the values to be written, instead of including the array of values in the function syntax. The example could be written this way, using a variable for the values:

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

   35647   58923   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.

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

   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'.

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

   38629   24735   29456   39470   33434   29484

Input Arguments

collapse all

Starting address to write to, specified as a double. writeAddress must be the first argument after the object name. This example writes 2 holding registers starting at address 501 and reads 4 holding registers starting at address 11250. The writeAddress is 501.

Example: writeRead(m,501,[1024 512],11250,4)

Data Types: double

Array of values to write, specified as a double or array of doubles. Values must be the second argument after the object name. Each value must be in the range 0–65535. This example writes 2 holding registers starting at address 501 and reads 4 holding registers starting at address 11250. The values are [1024 512].

Example: writeRead(m,501,[1024 512],11250,4)

Data Types: double

Starting address of the holding registers to read, specified as a double. readAddress must be the third argument after the object name. This example writes 2 holding registers starting at address 501 and reads 4 holding registers starting at address 11250. The readAddress is 11250.

Example: writeRead(m,501,[1024 512],11250,4)

Data Types: double

Number of holding registers to read, specified as a double. readCount must be the fourth argument after the object name. This example writes 2 holding registers starting at address 501 and reads 4 holding registers starting at address 11250. The readCount is 4.

Example: writeRead(m,501,[1024 512],11250,4)

Data Types: double

Address of the server to send the command to, specified as a double. Server ID must be specified after the object name, write address, values, read address, and read count. If you do not specify a serverId, the default of 1 is used. Valid values are 0–247, with 0 being the broadcast address. This example writes 3 holding registers starting at address 400 and reads 4 holding registers starting at address 52008 from server ID 6.

Example: writeRead(m,400,[1024 512 680],52008,4,6)

Data Types: double

Data format of the holding register being written to on the MODBUS server, specified as a character vector or string. writePrecision must be specified after the write address and values. Valid values are 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'single', and 'double'. This argument is optional, and the default is 'uint16'.

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

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'.

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

Data Types: char

Data format of the holding register being read from on the MODBUS server, specified as a character vector or string. readPrecision must be specified after the read address, and read count. Valid values are 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'single', and 'double'. This argument is optional, and the default is 'uint16'.

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

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 readPrecision of 'uint32'.

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

Data Types: char

Introduced in R2017a

Was this topic helpful?