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

How to listen to serial port connectivity continuously #74

Open
hosseinvejdani opened this issue Apr 23, 2023 · 10 comments
Open

How to listen to serial port connectivity continuously #74

hosseinvejdani opened this issue Apr 23, 2023 · 10 comments
Labels

Comments

@hosseinvejdani
Copy link

How to listen to serial port connectivity continuously in Flutter Linux app with flutter_libserialport package?

@realrk95
Copy link

Listen to SerialPortEvents. Check what is the output. Typically it is Event type which can equate to bool indicating the connectivity status. Put it in the initState of the connection screen and listen to this through a changenotifier to switch states when the device disconnects/connects. Looks something like this:

SerialPortEvents.listen((status) {
    if (event == true)
        connection procedure;
    else disconnect procedure;
})

@hosseinvejdani
Copy link
Author

hosseinvejdani commented May 18, 2023

But there is no SerialPortEvents class defined in the flutter_libserialport package!

@mateen-demah
Copy link

@hosseinvejdani have you found a way to this?

@hosseinvejdani
Copy link
Author

@hosseinvejdani have you found a way to this?

No!

@ohdonpiano
Copy link

ohdonpiano commented Jun 19, 2023

something like this?
https://pub.dev/documentation/dart_serial_port/latest/dart_serial_port/SerialPortReader-class.html

an example:

void createReader(SerialPort serialPort) {
    final portReader = SerialPortReader(serialPort);
    readerSubscription = portReader.stream.listen((newData) {
      log("new data received from serial port: ${newData.length} bytes");
    });
  }

  void dispose() {
    readerSubscription?.cancel();
  }

@mateen-demah
Copy link

something like this? https://pub.dev/documentation/dart_serial_port/latest/dart_serial_port/SerialPortReader-class.html

an example:

void createReader(SerialPort serialPort) {
    final portReader = SerialPortReader(serialPort);
    readerSubscription = portReader.stream.listen((newData) {
      log("new data received from serial port: ${newData.length} bytes");
    });
  }

  void dispose() {
    readerSubscription?.cancel();
  }

Your example is for getting the data stream from the port. This issue is about getting the status of the connection on the port. for instance, if a USB device is suddenly disconnected, how do you detect that in the application?

@ohdonpiano
Copy link

I don't see this feature on the serial port directly

reading SerialPortReader code, I find this part:

_receiver = ReceivePort();
    _receiver!.listen((data) {
      if (data is SerialPortError) {
        _controller.addError(data);
      } else if (data is Uint8List) {
        _controller.add(data);
      }
    });

maybe you can try something like this:

readerSubscription = portReader.stream.listen((newData) {
      log("new data received from serial port: ${newData.length} bytes");
      commandReceived(newData);
    }, onError: (error) {
      if (error is SerialPortError) {
        log("received serial port error: $error");
        dispose();
      }
    });

I can't try it myself yet

@mateen-demah
Copy link

Alright.
Thanks @ohdonpiano , maybe I should take a look all the possible SerialPortErrors to find one I can look out for.

@oteriusus
Copy link

I don't see this feature on the serial port directly

reading SerialPortReader code, I find this part:

_receiver = ReceivePort();
    _receiver!.listen((data) {
      if (data is SerialPortError) {
        _controller.addError(data);
      } else if (data is Uint8List) {
        _controller.add(data);
      }
    });

maybe you can try something like this:

readerSubscription = portReader.stream.listen((newData) {
      log("new data received from serial port: ${newData.length} bytes");
      commandReceived(newData);
    }, onError: (error) {
      if (error is SerialPortError) {
        log("received serial port error: $error");
        dispose();
      }
    });

I can't try it myself yet

Thanks @ohdonpiano, it works for me!

@lucafabbri
Copy link
Collaborator

Can we close this one?

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

No branches or pull requests

6 participants