readDiscreteInputs method
Read discrete input status (FC 02).
Reads from 1 to 2000 contiguous discrete inputs in a remote device.
Parameters:
slaveId: The slave device ID (1-247)address: Starting address of discrete inputsquantity: Number of inputs to read (1-2000)
Returns: Byte array with input status (bit-packed)
Implementation
@override
Future<Uint8List> readDiscreteInputs(
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(
funcCodeReadDiscreteInputs,
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));
}