-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SerialPortReader re-subscription #4
Comments
libserialport opens serial ports in exclusive mode, but does not disable the exlusive mode when closed. This means, you'll get an error "Device or resource busy" (EBUSY) when trying to re-open a serial port. I have proposed a patch to fix that a good while ago but unfortunately there's been zero activity there: sigrokproject/libserialport#5 |
It's a bit clumsy, but technically, you could now pause & close on error, and then re-open & resume: class MySerialPortReader {
final SerialPortReader _reader;
StreamSubscription<Uint8List> _subscription;
MySerialPortReader(SerialPort port) : _reader = SerialPortReader(port) {
_subscription = _reader.stream.listen(_receiveData, onError: _handleError);
}
void _receiveData(Uint8List data) {
print('==> $data');
}
void _handleError(Object error) {
print(error);
_subscription.pause();
_reader.port.close();
_keepRetryingAsAnExample();
}
void _keepRetryingAsAnExample() {
Timer.periodic(const Duration(seconds: 5), (timer) {
if (_reader.port.openReadWrite()) {
_subscription.resume();
timer.cancel();
}
});
}
} |
How can I substitute the stream in
SerialPortReader
after re-plug the USB and the stream observers don't have to re-subscript the stream again?Originally posted by @fynxiu in #3 (comment)
The text was updated successfully, but these errors were encountered: