Skip to content
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

Open
jpnurmi opened this issue Sep 26, 2020 · 2 comments
Open

SerialPortReader re-subscription #4

jpnurmi opened this issue Sep 26, 2020 · 2 comments

Comments

@jpnurmi
Copy link
Owner

jpnurmi commented Sep 26, 2020

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)

jpnurmi added a commit that referenced this issue Sep 26, 2020
This does not a solution to #4 as it does not handle re-subscribtion
or so, just helps to see what's going on.

Ref: #4
jpnurmi added a commit that referenced this issue Sep 26, 2020
This does not a solution to #4 as it does not handle re-subscribtion
or so, just helps to see what's going on.

Ref: #4
@jpnurmi
Copy link
Owner Author

jpnurmi commented Sep 26, 2020

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

jpnurmi added a commit that referenced this issue Sep 26, 2020
jpnurmi added a commit that referenced this issue Sep 26, 2020
@jpnurmi
Copy link
Owner Author

jpnurmi commented Sep 26, 2020

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();
      }
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant