jpnurmi / flutter_libserialport

Serial Port for Flutter
https://pub.dev/packages/flutter_libserialport
MIT License
139 stars 80 forks source link

How to listen to serial port connectivity continuously #74

Open hosseinvejdani opened 1 year ago

hosseinvejdani commented 1 year ago

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

realrk95 commented 1 year ago

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 commented 1 year ago

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

mateen-demah commented 11 months ago

@hosseinvejdani have you found a way to this?

hosseinvejdani commented 11 months ago

@hosseinvejdani have you found a way to this?

No!

ohdonpiano commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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

oteriusus commented 5 months ago

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 commented 2 months ago

Can we close this one?