writeMultipleCoils method
Write multiple coils (FC 15).
Forces each coil in a sequence to either ON or OFF.
Parameters:
slaveId: The slave device ID (0-247, 0 for broadcast)address: Starting address of coilsquantity: Number of coils to write (1-1968)value: Byte array with coil values (bit-packed)
Example:
final values = Uint8List.fromList([0xFF, 0x00]); // First 8 ON, next 8 OFF
await client.writeMultipleCoils(1, 0, 16, values);
Implementation
@override
Future<void> writeMultipleCoils(
int slaveId, int address, int quantity, Uint8List value) async {
if (slaveId > _addressMax) {
throw ArgumentError(
'modbus: slaveId \'$slaveId\' must be between \'$addressBroadCast\' and \'$_addressMax\'');
}
if (quantity < writeBitsQuantityMin || quantity > writeBitsQuantityMax) {
throw ArgumentError(
'modbus: quantity \'$quantity\' must be between \'$writeBitsQuantityMin\' and \'$writeBitsQuantityMax\'');
}
if (value.length * 8 < quantity) {
throw ArgumentError(
'modbus: value bits size \'${value.length * 8}\' does not greater or equal to quantity \'$quantity\'');
}
final response = await send(
slaveId,
ProtocolDataUnit(
funcCodeWriteMultipleCoils,
pduDataBlockSuffix(value, [address, quantity]),
),
);
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 rspQuantity = bytesToUint16(response.data.sublist(2, 4))[0];
if (rspQuantity != quantity) {
throw Exception(
'modbus: response quantity \'$rspQuantity\' does not match request \'$quantity\'');
}
}