Modbus write task
Write command channel values to the coils and holding registers of a Modbus server.
A Modbus write task writes the values of command channels to the coils and holding registers of one Modbus server. For example, a button on a schematic can start a pump through a coil of a PLC. Modbus write tasks have no state channels.
Each command writes only the addresses of the channels in the command. Channels at adjacent addresses go in one request. The Driver writes coils with function code 15 and holding registers with function code 16. For a device that supports only function codes 05 and 06, set Write codes of the server to Single. The Driver then writes each register on its own, so the two halves of a 32-bit value can reach the device at different times. A write goes before reads that wait for the same connection.
To write one bit of a holding register, the Driver reads the register, changes the bit, and writes the register back. If the device changes another bit of the register between the read and the write, the write removes that change. For a device that supports function code 22, select Mask write (FC22) on the server. The Driver then changes the bit with one request, and the other bits stay as the device has them.
Before you start, you need a connected server.
Create a Modbus write task
When you create command channels with a client library, give each one its own index channel. A writer that holds several commands on one index must write all of them in every frame.
Console
Python
TypeScript
- In the devices toolbar, right-click the server and select Create write task.
- Click Add channel. Select the type, then enter the address. For a holding register, select the Data type. For a holding register bit, enter the Bit.
- Click (Start).
The Console creates a command channel and an index channel for each output. For the
server Pump skid PLC, a coil at address 3 gives Pump_skid_PLC_coil_output_3_cmd and
Pump_skid_PLC_coil_output_3_cmd_time. To use a different name, right-click the channel
and select Rename before you start the task.
Python does not create channels. Create them first.
import synnax as sy
from synnax import modbus
dev = client.devices.retrieve(name="Pump skid PLC")
cmd_time = client.channels.create(
name="pump_cmd_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
pump_cmd = client.channels.create(
name="pump_cmd", index=cmd_time.key, data_type=sy.DataType.UINT8
)
task = modbus.WriteTask(
name="Pump control",
device=dev.key,
channels=[modbus.CoilWriteChannel(address=3, channel=pump_cmd.key)],
)
client.tasks.configure(task)
task.start()TypeScript does not create channels. Create them first.
import { modbus } from "@synnaxlabs/client";
const [dev] = await client.devices.retrieve({
where: { name: { eq: "Pump skid PLC" } },
});
const cmdTime = await client.channels.create({
name: "pump_cmd_time",
isIndex: true,
dataType: "timestamp",
});
const pumpCmd = await client.channels.create({
name: "pump_cmd",
index: cmdTime.key,
dataType: "uint8",
});
const task = await client.tasks.create({
name: "Pump control",
rack: dev.rack,
type: "modbus_write",
config: modbus.writeConfigZ.parse({
device: dev.key,
channels: [{ type: "coil", address: 3, channel: pumpCmd.key }],
}),
});
await task.start();The task has the options Data saving and Auto start. See
task options. Python WriteTask has no
data_saving_disabled argument.
Channel reference
Each channel has an address, from 0 to 65535, and a channel, the command channel.
Each table shows the Console label of a setting, with its Python field under it.
The Console also accepts a six-digit Modicon address, such as 400001 for holding register 0. The first digit is the table: 0 for coils and 4 for holding registers. The Console shows the Modicon form next to each address.
Troubleshooting
“Illegal function”
The device does not support function code 15 or 16. Set Write codes of the server to Single.
The connection dropped
The Driver reconnects and sends the command again at once, up to the Retries of the server. If that fails, the task shows the warning “Failed to write commands” and keeps running. The Driver reconnects at the next command.
For other problems, see Connect a Modbus server and Troubleshoot hardware.