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

Not able to read port data. port.read() is always throwing false.

Open MANOJKUMAR26 opened this issue 4 months ago • 0 comments

Hi, When I'm trying to connect the serial port with the below code, I am getting port.read() always as false. I am not able to read the data as the port is not getting opened. I can confirm that the device which is sending the data is has no problem. Can you please help us resolving this issue.

Future _getPortsAndOpen(Device selectedDevice) async { final availablePorts = SerialPort.availablePorts; if (availablePorts.isNotEmpty) {

_port = SerialPort(selectedDevice.portName);
_port.openRead();

var config = _port.config;
config.baudRate = 9600;
_port.config = config;

_port.config.bits = 8;
_port.config.stopBits = 1;
_port.config.parity = 0;
_port.config.xonXoff = 0;
_port.config.rts = 0;
_port.config.cts = 0;
_port.config.dsr = 0;
_port.config.dtr = 0;

_port.config = config; 

// config.dispose();

if (_port.openRead()) {
  print('Port opened: ${selectedDevice.portName}');
  // Start reading data from the port
  _reader = SerialPortReader(_port!);
  _reader!.stream.listen((Uint8List data) {
    buffer += String.fromCharCodes(data);
    print('Received data: $buffer');
  });
} else {
  print('Failed to open the port.');
}

} else { print('No ports available.'); } }

MANOJKUMAR26 avatar Sep 29 '24 11:09 MANOJKUMAR26