0

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

James Z
  • 12,209
  • 10
  • 24
  • 44

0 Answers0