Main Content

write

R2026b

Write data to Modbus server

Description

write(modbusClientObj,target,address,values) writes data to the specified target register and address on the Modbus server connected to the client specified by modbusClientObj. You can write to coils or holding registers.

example

write(modbusClientObj,target,address,values,serverId) additionally specifies serverId to send the write command to a specific server.

example

write(modbusClientObj,"holdingregs",address,values,precision) additionally specifies precision to determine how the register data is interpreted. Use only for holding registers.

example

write(modbusClientObj,"holdingregs",address,values,serverId,precision) specifies both the server address and the register data format. Use only for holding registers.

example

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 the values to 4 coils starting at address 8289.

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 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 values to 5 coils starting at address 8289 on server ID 3.

write(m,"coils",8289,[1 1 0 1 0],3)

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 using uint32 precision.

write(m,"holdingregs",49153,2000,"uint32")

You can write to coils or holding registers and also specify optional serverId and precision parameters. List both parameters 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

Modbus client, specified either as an icomm.interface.modbus.tcpip.Modbus or icomm.interface.modbus.serialrtu.Modbus object, based on the transport layer used for communication. You can create the client using the modbus function.

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

Example: write(m,"coils",8289,[1 1 0 1]) writes to four coils starting at address 8289.

Data Types: char

Starting address to write to, specified as a positive scalar. Use 1-based addressing because the function subtracts 1 from the specified address.

Example: write(m,"coils",5200,[1 1 0 1 1 0]) writes to six coils starting at address 5200.

Data Types: double

Array of values to write, specified as a double or array of doubles. 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. The maximum number of values in the array depends on the specified precision because each value can occupy one or more contiguous Modbus registers.

Example: write(m,"coils",8289,[0 1 0 1]) writes to four coils starting at address 8289.

Data Types: double

Address of the server to send the write command to, specified as a nonnegative scalar. If you do not specify a serverId, the function uses the default as 1. Valid values are 0-255, with 0 being the broadcast address.

Note

The serverId refers to the unit identifier for Modbus TCP.

Example: write(m,"coils",1,[1 1 1 1 0 0 0 0],3); writes to eight coils starting at address 1 from server ID 3.

Data Types: double

Data format of the register being written to on the Modbus server, specified as a character vector or string. 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.

Example: write(m,"holdingregs",2,[100 200 300 500],"uint32"); writes to four holding registers starting at address 2 using a precision of 'uint32'.

Data Types: char

Extended Capabilities

expand all

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

Version History

Introduced in R2017a

expand all