Contenido principal

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

To write up to 1968 contiguous coils in the remote server, set the write target to "coils". A coil is a single-bit read/write value. A value of 1 indicates on and 0 indicates 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)

To write up to 1968 contiguous coils in the remote server, set the write target to "coils". A coil is a single-bit read/write value. A value of 1 indicates on and 0 indicates off.

Write values to 5 coils starting at address 8289 on server ID 3.

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

To write up to 123 contiguous holding registers in the remote server, set the write target to "holdingregs". A holding register is a 16-bit read/write register. 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. The precision argument specifies how the values are converted to register data before writing. A precision of uint32 means each value is written as an unsigned 32-bit integer spanning two consecutive registers.

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

You can write to either target type and also specify optional serverId and precision 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. Create the client using the modbus function.

Target area to write to, specified as a character vector or string. Valid values are:

  • "coils" — Single-bit read/write values. A value of 1 indicates on and 0 indicates off.

  • "holdingregs" — 16-bit read/write registers.

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

Data Types: char | string

Starting address for write operation, 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

Values to write, specified as a double or array of double values. 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

Server identifier for write operation, specified as a nonnegative scalar. The server identifier corresponds to the unit identifier in Modbus TCP and the server address in Modbus Serial RTU. If you do not specify a serverId, the function uses the default value of 1. Valid values are 0-255, with 0 being the broadcast address.

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

precision specifies how the values are converted to register data before writing.

Example: write(m,"holdingregs",2,[100 200 300 500],"uint32"); writes four values to holding registers starting at address 2, converting each value to a uint32 value spanning two consecutive registers.

Data Types: char | string

Extended Capabilities

expand all

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

Version History

Introduced in R2017a

expand all