Add methods to set serial parameters and control lines in UsbSerialPort

Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
Tony
2026-06-28 06:36:09 +08:00
parent 1638a5c388
commit 2d7fc43ea2
2 changed files with 127 additions and 0 deletions
+44
View File
@@ -32,6 +32,50 @@ class UsbSerialPort {
return written ?? 0;
}
/// Updates serial line parameters for an already-open port.
Future<void> setParameters({
required int baudRate,
int dataBits = 8,
UsbSerialParity parity = UsbSerialParity.none,
UsbSerialStopBits stopBits = UsbSerialStopBits.one,
}) async {
await _methods.invokeMethod<void>('setParameters', {
'portId': _portId,
'baudRate': baudRate,
'dataBits': dataBits,
'parity': parity.index,
'stopBits': FlutterUsbSerial._stopBitsIndex(stopBits),
});
}
/// Sets the DTR modem control line.
Future<void> setDTR(bool value) async {
await _methods.invokeMethod<void>('setDTR', {
'portId': _portId,
'value': value,
});
}
/// Sets the RTS modem control line.
Future<void> setRTS(bool value) async {
await _methods.invokeMethod<void>('setRTS', {
'portId': _portId,
'value': value,
});
}
/// Purges USB serial driver buffers when the driver supports it.
Future<void> purgeHwBuffers({
bool purgeWriteBuffers = true,
bool purgeReadBuffers = true,
}) async {
await _methods.invokeMethod<void>('purgeHwBuffers', {
'portId': _portId,
'purgeWriteBuffers': purgeWriteBuffers,
'purgeReadBuffers': purgeReadBuffers,
});
}
/// Close the port and release native resources.
Future<void> close() async {
await _methods.invokeMethod<void>('close', {'portId': _portId});