writeSingleCoil method
Write single coil (FC 05).
Writes a single coil to either ON or OFF.
Parameters:
slaveId: The slave device ID (0-247, 0 for broadcast)address: Coil addressisOn:truefor ON (0xFF00),falsefor OFF (0x0000)
Example:
await client.writeSingleCoil(1, 0, true); // Turn on coil 0
Implementation
@override
Future<void> writeSingleCoil(int slaveId, int address, bool isOn) async {
if (slaveId > _addressMax) {
throw ArgumentError(
'modbus: slaveId \'$slaveId\' must be between \'$addressBroadCast\' and \'$_addressMax\'');
}
final value = isOn ? 0xFF00 : 0x0000;
final response = await send(
slaveId,
ProtocolDataUnit(
funcCodeWriteSingleCoil,
uint16ToBytes([address, value]),
),
);
if (response.data.length != 4) {
throw Exception(
'modbus: response data size \'${response.data.length}\' does not match expected \'4\'');
}
final rspAddress = bytesToUint16(response.data.sublist(0, 2))[0];
if (rspAddress != address) {
throw Exception(
'modbus: response address \'$rspAddress\' does not match request \'$address\'');
}
final rspValue = bytesToUint16(response.data.sublist(2, 4))[0];
if (rspValue != value) {
throw Exception(
'modbus: response value \'$rspValue\' does not match request \'$value\'');
}
}