libserialport.dart
libserialport.dart copied to clipboard
Sometime open() is hang up.
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.
Are you by any chance able to try it with one of the examples in the libserialport C-library?
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 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
});
}