After I open the serial port com1 in the app, the message I receive is incorrect. I sent aa06000000000000000000ff, but the serial tool received 00 80 80 80 00 00 00 00 00 00 00.
I use the serial tool to open com1, then close it, and then use the application to open com1, the message sent is normal, what is the reason.
code show as below:
void open(String portName, int baudRate) async {
_serialPort = SerialPort(portName);
var config = SerialPortConfig();
config.baudRate = 9600;
config.parity = 0;
config.bits = 8;
// config.cts = 0;
// config.rts = 0;
config.stopBits = 1;
config.xonXoff = 0;
_serialPort!.config = config;
_readData();
}
void sendData(String sendData) async {
List dataList = [];
int len = sendData.length ~/ 2;
for (int i = 0; i < len; i++) {
String data = sendData.trim().substring(2 * i, 2 * (i + 1));
int? d = _hexToInt2(data);
dataList.add(d!);
}
var bytes = Uint8List.fromList(dataList);
_serialPort!.write(bytes);
}
void _readData() async {
// 读数据
final reader = SerialPortReader(_serialPort!, timeout: 25);
// List<String> list = [];
if (!_serialPort!.openReadWrite()) {
print(SerialPort.lastError);
return;
}
_connectStreamController.add(true);
reader.stream.listen((data) {
for (int i = 0; i < data.length; i++) {
String hexString = data[i].toRadixString(16).padLeft(2, '0');
if ((hexString.toUpperCase().endsWith("AC") ||
hexString.toUpperCase().endsWith("EE")) &&
Global.serialPortMessage.length == 12) {
Global.serialPortMessage.clear();
}
Global.serialPortMessage.add(hexString);
print('receivedHex: $hexString');
if (Global.serialPortMessage.length == 12 &&
hexString.toUpperCase().endsWith('FF')) {
_receiveStreamController.add(Global.serialPortMessage);
Global.messageHandledStatus = false;
}
}
});
}
After I open the serial port com1 in the app, the message I receive is incorrect. I sent aa06000000000000000000ff, but the serial tool received 00 80 80 80 00 00 00 00 00 00 00. I use the serial tool to open com1, then close it, and then use the application to open com1, the message sent is normal, what is the reason. code show as below: