readCoils method
Read coil status (FC 01).
Reads from 1 to 2000 contiguous coils in a remote device.
Parameters:
slaveId: The slave device ID (1-247)address: Starting address of coilsquantity: Number of coils to read (1-2000)
Returns: Byte array with coil status (bit-packed)
Example:
final coils = await client.readCoils(1, 0, 10);
final isCoil0On = (coils[0] & 0x01) != 0;
Implementation
@override
Future<Uint8List> readCoils(int slaveId, int address, int quantity) async {
if (slaveId < _addressMin || slaveId > _addressMax) {
throw ArgumentError(
'modbus: slaveId \'$slaveId\' must be between \'$_addressMin\' and \'$_addressMax\'');
}
if (quantity < readBitsQuantityMin || quantity > readBitsQuantityMax) {
throw ArgumentError(
'modbus: quantity \'$quantity\' must be between \'$readBitsQuantityMin\' and \'$readBitsQuantityMax\'');
}
final response = await send(
slaveId,
ProtocolDataUnit(
funcCodeReadCoils,
uint16ToBytes([address, quantity]),
),
);
if (response.data.length - 1 != response.data[0]) {
throw Exception(
'modbus: response byte size \'${response.data.length - 1}\' does not match count \'${response.data[0]}\'');
}
if (response.data[0] != (quantity + 7) ~/ 8) {
throw Exception(
'modbus: response byte size \'${response.data[0]}\' does not match quantity to bytes \'${(quantity + 7) ~/ 8}\'');
}
return Uint8List.fromList(response.data.sublist(1));
}