Main Content

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

Writing 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 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, and it is 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)

Writing 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 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, and it is 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 2 registers, and for 'uint64', 'int64' and 'double', each value corresponds to 4 registers. For 'int16' and 'uint16', each value is from a single 16-bit register.

This example writes 3 values, starting at address 29473 and converting to single precision.

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