Main Content

write

Perform a write operation to the connected Modbus server

Description

example

write(m,target,address,values) writes data to Modbus object m to target type target at the starting address address using the values to write values. You can write to coils or holding registers.

example

write(m,target,address,values,serverId) additionally specifies serverId, which is the address of the server to send the write command to. serverId can be used for coils or holding registers.

write(m,'holdingregs',address,values,'precision') additionally specifies precision, which is the data format of the register being written. precision can be used only for holding registers.

write(m,'holdingregs',address,values,serverId,'precision') additionally specifies serverId and data format precision. You can both arguments together when the write target is holding registers.

Examples

collapse all

If the write target is coils, the function writes a contiguous sequence of 1–1968 coils to either on or off in a remote device. A coil is a single output bit. A value of 1 indicates the coil is on and a value of 0 means it is off.

Write to 4 coils, starting at address 8289. The address parameter is the starting address of the coils to write to, and it is a double. The values parameter is an array of values to write.

write(m,'coils',8289,[1 1 0 1])

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

values = [1 1 0 1];
write(m,'coils',8289,values)

If the write target is holding registers, the function writes a block of 1–123 contiguous registers in a remote device. Values whose representation is greater than 16 bits are stored in consecutive register addresses.

Set the register at address 49153 to 2000.

write(m,'holdingregs',49153,2000)

You can write to coils or holding registers and also specify the optional parameter for server ID, and you can specify precision for holding registers. You can set either option by itself or set both the serverId option and the precision option together. Both options should be listed after the required arguments.

Write 3 values, starting at address 29473, at Server ID 2, converting to single precision.

write(m,'holdingregs',29473,[928.1 50.3 24.4],2,'single')

Input Arguments

collapse all

Target area to write to, specified as a character vector or string. You can perform a Modbus write operation on two types of targets: coils and holding registers, so you must set the target type as either 'coils' or 'holdingregs'. Target must be the first argument after the object name.

This example writes to 4 coils starting at address 8289.

Example: write(m,'coils',8289,[1 1 0 1])

Data Types: char

Starting address to write to, specified as a double. Address must be the second argument after the object name.

This example writes to 6 coils starting at address 5200.

Example: write(m,'coils',5200,[1 1 0 1 1 0])

Data Types: double

Array of values to write, specified as a double or array of doubles. values must be the third argument after the object name. If the target is coils, valid values are 0 and 1. If the target is holding registers, valid values must be in the range of the specified precision. You can include the array of values in the syntax, as shown here, or use a variable for the values.

This example writes to 4 coils starting at address 8289.

Example: write(m,'coils',8289,[0 1 0 1])

Data Types: double

Address of the server to send the write command to, specified as a double. Server ID must be specified after the object name, target, address, and values. If you do not specify a serverId, the default of 1 is used. Valid values are 0-255, with 0 being the broadcast address.

Note

The serverId refers to unit identifier for Modbus TCP.

This example writes 8 coils starting at address 1 from server ID 3.

Example: write(m,'coils',1,[1 1 1 1 0 0 0 0],3);

Data Types: double

Data format of the register being written to on the Modbus server, specified as a character vector or string. Precision must be specified after the object name, target, 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 precision does not refer to the return type, which is always 'double'. It specifies how to interpret the register data.

This example writes to 4 holding registers starting at address 2 using a precision of 'uint32'.

Example: write(m,'holdingregs',2,[100 200 300 500],'uint32');

Data Types: char

Extended Capabilities

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

Version History

Introduced in R2017a

expand all