flutter_libserialport icon indicating copy to clipboard operation
flutter_libserialport copied to clipboard

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

Open geoffrey-oongo opened this issue 2 years ago • 4 comments

void getDataFromLibSerial( LibSerial.SerialPort? port, LibSerial.SerialPortReader? reader) async { try { if (reader != null) { Stream<String> 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<AuthProvider>(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();

}

geoffrey-oongo avatar Sep 06 '23 01:09 geoffrey-oongo

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

lucafabbri avatar Feb 21 '24 13:02 lucafabbri

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: @.***>

geoffrey-oongo avatar Apr 16 '24 11:04 geoffrey-oongo