libserialport.dart icon indicating copy to clipboard operation
libserialport.dart copied to clipboard

Sometime open() is hang up.

Open Leadrive opened this issue 3 years ago • 3 comments

    var baudRate = "19200";
    var port = SerialPort("COM3");
    var portConfig = SerialPortConfig();
    portConfig.baudRate = int.parse(baudRate);
    port.config = portConfig;
    if (!port.open(mode: SerialPortMode.read)) {
      print("串口打开失败,请检查设置或是否已占用");
    } else {
    }

Windows 10.

I tested this code. maybe 20-30% will hang up in port.open. I think this is a bug, please check.

Leadrive avatar Sep 07 '21 17:09 Leadrive

Are you by any chance able to try it with one of the examples in the libserialport C-library?

jpnurmi avatar Sep 07 '21 17:09 jpnurmi

Sorry, port.open not hang up, is open fail. But the port is idle.

I use this component to read the IC card, the data length is 13 or 14. But sometime, only read one byte every time.

I think this component is not stable now, so I have to try another now.

Windows 10, and not test in Linux.

Leadrive avatar Sep 08 '21 02:09 Leadrive

@Leadrive Not sure if this would work, but try to config the serial port after open and listen to the reader stream for the values. Here's a simple snippet,

if (!port.isOpen) {
  if (!port.openRead()) {
    throw Exception('Failed to open serial port');
  }

  // Update the config for your setup
  final config = SerialPortConfig()
    ..baudRate = 9600
    ..bits = 8
    ..stopBits = 1
    ..parity = SerialPortParity.none
    ..setFlowControl(SerialPortFlowControl.none);
  
  port.config = config;

  // Remember to close the port and the reader when done.
  reader = SerialPortReader(port)
    ..stream.listen((value) {
      print(value);
    },
    onError: (Object error) {
      // handle port error
    });
}

k10dev avatar Sep 26 '21 14:09 k10dev