Write Data to a Modbus Server
Types of Data You Can Write to over Modbus
The write
function performs write operations to two
types of target addressable areas:
Coils
Holding registers
Each of the two areas can accept a write request to a single address or a contiguous
address range. When you perform the write operation, you must specify the target type
(target
), the starting address (address
), and the
values to write (values
). You can also optionally specify the address of
the server (serverId
) and the data format
(precision
).
Write Coils over Modbus
If the write target is coils, the function writes a contiguous sequence of 1–1968 coils
to either on or off (1 or 0) 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.
The syntax to write to coils is:
write(obj,'coils',address,values)
The obj
parameter is the name of the Modbus® object. The following examples assume you have created a Modbus object, m
. For information on creating the object, see
Create a Modbus Connection.
The address
parameter is the starting address of the coils to write
to, specified as a double. The values
parameter is an array of values to
write. For a target of coils, valid values are 0
and
1
.
This example writes 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)
Write Holding Registers over Modbus
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.
The syntax to write to holding registers is:
write(obj,'holdingregs',address,values)
The obj
parameter is the name of the Modbus object. The following examples assume you have created a Modbus object, m
. For information on creating the object, see
Create a Modbus Connection.
The address
parameter is the starting address of the holding
registers to write to, specified as a double. The values
parameter is an
array of values to write. For a target of holding registers, valid values must be in the
range of the specified precision.
This example sets the register at address 49153 to 2000.
write(m,'holdingregs',49153,2000)
Precision Option
The 'precision'
argument specifies the data format of the register
being written to on the Modbus server. Valid values are 'uint16'
,
'int16'
, 'uint32'
, 'int32'
,
'uint64'
, 'int64'
, 'single'
, and
'double'
. This argument is optional, and the default is
'uint16'
.
The values passed in to be written are converted to register values based on the
specified precision. For precision values 'int32'
,
'uint32'
, and 'single'
, each value corresponds to
two registers, and for 'uint64'
, 'int64'
and
'double'
, each value corresponds to four registers. For
'int16'
and 'uint16'
, each value is from one 16-bit
register.
This example writes 3 values as single
precision, starting at address
29473.
write(m,'holdingregs',29473,[928.1 50.3 24.4],'single')