Open kapold opened 7 months ago
Do you set port.readBytesSize
and ensure that you open ture COM port? Any logs from your infoLog will help.
If readOnListenFunction is still failed, you can try another function like:
print(await port.readBytesUntil(Uint8List.fromList("\n".codeUnits)));
PS: Do not use readBytesOnListen
when you use readBytesUntil
function.
If solutions above failed, please test if you can write
anything to the COM port and tell me your windows system version.
I tried to use readBytesUntil, but nothing seems to happen. The readOnListenFunction function returns different sets of bytes once every 30 scan attempts. Windows version 11.
flutter: [I] Available ports: [COM4] flutter: [I] Port is opened flutter: [I] Before flutter: [I] Read data: [2, 10, 2, 51, 0, 56, 94, 208] flutter: [I] Before flutter: [I] Read data: [141, 3, 2, 10, 2, 51, 0, 56]
You read 8 bytes, so function returns 8 bytes. It's the right behaviour for readOnListenFunction
.
What do you want to read? In COM port/RS485/RS232/UART, data frame should be processed by application itself, not read function.
I guess your data is [2, 51, 0, 56, 94, 208, 141, 3, 2, 10]. Your computer stores all data in buffer. And you read data between two data frame. There some reasons:
By the way, the function doesn't check char terminator or other terminators in readOnListenFunction. It just fetches n <= N bytes from Windows'COM RECV BUFFER.
And readBytesUntil will continuously read bytes until expected char.
In port.readBytesUntil(Uint8List.fromList("\n".codeUnits))
, it will read until '\n'. Then it will return a Uint8List.
Do you have any code example for readBytesUntil
? And what is your original data content?
Good afternoon. For some reason, I can't read the data. Can you tell me what's wrong? Here are the parameters that should be:
import 'package:rfid_for_workers/utils/logs.dart'; import 'package:serial_port_win32/serial_port_win32.dart'; import 'package:win32/win32.dart';
class SerialPortHelper { late SerialPort port;
static void initSerialPort() { final ports = SerialPort.getAvailablePorts(); Logs.infoLog('Available ports: $ports');
} }