jpnurmi / flutter_libserialport

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

my code only gets the expected data after starting and closing putty, it seems it the serial port object does not use the configs even setting them #87

Open geoffrey-oongo opened 8 months ago

geoffrey-oongo commented 8 months ago

void getDataFromLibSerial( LibSerial.SerialPort? port, LibSerial.SerialPortReader? reader) async { try { if (reader != null) { Stream upcomingData = reader.stream.map((event) { print(event); var data = ascii.decode(event); print(data);

      try {
        if (data.contains('S') &&
            data.contains('+') &&
            data.contains('kg')) {
          cleanedData = data
              .split('\n')[0]
              .split("+")[1]
              .replaceAll(',kg', '')
              .trim();

          var parsedWeight = num.tryParse(cleanedData);
          print(parsedWeight);
          if (parsedWeight != null) {
            cleanWeight = parsedWeight.toString();
            widget.func(cleanWeight ?? "", context);
          }
        }
      } catch (e) {}
      print(cleanWeight);

      return cleanWeight ?? "";
    });

    upcomingData.listen((event) {
      if (event.isNotEmpty) {
        setState(() {});
      }
    });
  }
} catch (e) {
  print(e);
}

}

void didChangeDependencies() async { final connectionProvider = Provider.of(context, listen: false).connection;

showWeight = true;

if (connectionProvider != null) {
  if (connectionProvider.type == 'Serial') {
    try {
      String COM_PORT = connectionProvider.COM_PORT ?? "COM3";
      port = LibSerial.SerialPort(COM_PORT);

      if (port == null) {
        return;
      }
      LibSerial.SerialPortConfig config = LibSerial.SerialPortConfig();
      config.bits = 8;
      config.baudRate = 9600;
      config.parity = 0;
      config.stopBits = 1;
      port!.config = config;
      port!.openRead();

      // _getData(context);
      reader = LibSerial.SerialPortReader(port!);

      getDataFromLibSerial(port, reader);
    } catch (e) {}
  } else if (connectionProvider.type == 'TCP/IP') {
    try {
      socket = await HttpSocketClient.http_socket(
          connectionProvider.ip_address, connectionProvider.port_number);
      _getDataFromSocket(socket);
    } catch (e) {
      print('');
    }
  }
}
super.didChangeDependencies();

}

lucafabbri commented 2 months ago

@geoffrey-oongo check the #29 let me know if it solves the issue

geoffrey-oongo commented 1 month ago

Thanks for the assistance Nataraja

On Wed, Feb 21, 2024 at 4:05 PM Nataraja @.***> wrote:

@geoffrey-oongo https://github.com/geoffrey-oongo check the #29 https://github.com/jpnurmi/flutter_libserialport/issues/29 let me know if it solves the issue

— Reply to this email directly, view it on GitHub https://github.com/jpnurmi/flutter_libserialport/issues/87#issuecomment-1956611427, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN6OYJI3RZYOS6TOOSJRLJ3YUXWKZAVCNFSM6AAAAAA4MR6D6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJWGYYTCNBSG4 . You are receiving this because you were mentioned.Message ID: @.***>