open method
Opens the underlying port using config. Idempotent.
Implementation
@override
Future<void> open(String portName, SerialConfig config) async {
if (_isClosed) {
throw StateError('LibserialportSerialTransport already closed');
}
if (_isOpen) return;
final namePtr = portName.toNativeUtf8();
final portHandle = calloc<Pointer<Void>>();
try {
_check(_bindings.spGetPortByName(namePtr, portHandle),
'sp_get_port_by_name($portName)');
_port = portHandle.value;
_check(_bindings.spOpen(_port!, SpMode.readWrite), 'sp_open');
_check(_bindings.spSetBaudrate(_port!, config.baudRate),
'sp_set_baudrate(${config.baudRate})');
_check(_bindings.spSetBits(_port!, config.dataBits),
'sp_set_bits(${config.dataBits})');
_check(_bindings.spSetParity(_port!, _parityToNative(config.parity)),
'sp_set_parity');
_check(_bindings.spSetStopBits(_port!, config.stopBits),
'sp_set_stopbits(${config.stopBits})');
_check(
_bindings.spSetFlowControl(
_port!, _flowControlToNative(config.flowControl),
),
'sp_set_flowcontrol',
);
_readBuf = malloc<Uint8>(readBufferSize);
_isOpen = true;
_pollTimer = Timer.periodic(pollInterval, (_) => _drainOne());
} finally {
malloc.free(namePtr);
calloc.free(portHandle);
}
}