FengChendian / serial_port_win32

A flutter SerialPort library using win32 API.
BSD 3-Clause "New" or "Revised" License
31 stars 9 forks source link

Reading data #32

Open kapold opened 6 months ago

kapold commented 6 months ago

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:

  1. The speed is 9600;
  2. 8 bits of data;
  3. 1 stop bit;
  4. Without parity bits;
  5. Reset the IObit Mode;
  6. Turn off the flow control;

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');

final port = SerialPort(
  ports.first,
  openNow: false,
  BaudRate: CBR_9600,
  ByteSize: 8,
  StopBits: ONESTOPBIT,
  Parity: NOPARITY,
);
port.readOnListenFunction = (data) {
  Logs.infoLog('Read data: $data');
};

port.open();
if (port.isOpened) {
  Logs.infoLog('Port is opened');
}
else {
  Logs.infoLog('Error open port');
}

} }

FengChendian commented 6 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.

kapold commented 6 months ago

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]

FengChendian commented 6 months ago

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:

  1. Your devices always send data without any handshake. Then Buffer overflow or error. So data is mixed with different frames.
  2. Maybe you use UART TO USB chip. The chip may generate wrong time series data.

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?