Abdul-Mateen
Abdul-Mateen

Reputation: 71

How can I fix empty values returned when using flutter_libserialport package to read serial input on Windows?

I'm developing a flutter destop app which has to read serial input from an esp32 board. I'm using the flutter_libserialport package. Reads are empty. When I use a port reader to stream, I get only [0]s and when I use the port.read(buffer), I just get a bunch of zeroes in the returned value [0,0,0,...].

It's my first time using the library and I haven't been able to find any proper documentation on it. This might be a configuration issue but I'm not sure how to do this configuration.

The only way I'm able to get it to work is to first read from the port with the Arduino IDE's serial monitor or a python script using the pyserial library; close the connection there and then read the port in my app.

Code sample:

  void startListening() {
    if(_selected?.isOpen == true){
      print("selection was open. Closed successful = ${_selected?.close()}");
    }
    _selected?.config = SerialPortConfig()..baudRate = 9600;

    if(_selected?.open(mode: 3) != true){

      print("=============> Something went wrong");
      print(_selected == null ? "selected is null": SerialPort.lastError);

    } else {

      // final data = await _selected?.read(128, timeout: 200);
      // print("=======> data");
      // print(String.fromCharCodes(data!));
      // _selected?.close();

      final reader = SerialPortReader(_selected!);
      reader.stream.listen((event) {

        print("============> onData: ${_selected?.name}");
        print("$event => ${String.fromCharCodes(event)}");

      }, onError: (error, stackTrace){

        print("error: $error");
        print("stackTrace: $stackTrace");

      }, onDone: (){
        _selected!.close();
      },);
    }
  }

sample output:

enter image description here

Upvotes: 0

Views: 701

Answers (2)

Abdul-Mateen
Abdul-Mateen

Reputation: 71

I wasn't able to solve this problem using the flutter_libserialport itself.

I used the flython package to open and close the port with a Python script, and then open the port again with the flutter_libserialport afterward.

Upvotes: 0

littlebear333
littlebear333

Reputation: 760

Before you set config, you should open your serial port first. Like this

    void startListening() {
    if(_selected?.isOpen == true){
      print("selection was open. Closed successful = ${_selected?.close()}");
    }
   

    if(_selected?.open(mode: 3) != true){

      print("=============> Something went wrong");
      print(_selected == null ? "selected is null": SerialPort.lastError);

    } else {
        _selected?.config = SerialPortConfig()..baudRate = 9600;
      // final data = await _selected?.read(128, timeout: 200);
      // print("=======> data");
      // print(String.fromCharCodes(data!));
      // _selected?.close();

      final reader = SerialPortReader(_selected!);
      reader.stream.listen((event) {

        print("============> onData: ${_selected?.name}");
        print("$event => ${String.fromCharCodes(event)}");

      }, onError: (error, stackTrace){

        print("error: $error");
        print("stackTrace: $stackTrace");

      }, onDone: (){
        _selected!.close();
      },);
    }
  }

Or,the config will not work.

Upvotes: 0

Related Questions