在 python 中使用 Ser.readline() 时出现不可见的未知字符

INVISIBLE unknown character when using Ser.readline() in python

我正在从事一个项目,在角度传感器(通过微型)与 python GUI

之间进行通信

我试图通过 UART 从微控制器发送 2 个字节的数据(~ 范围从 0-360,在 255 之后超过 1 个字节)

这就是我拆分数据并在接收端合并它们的方式

angle = readangle();
send[0] = angle ;
send[1] = angle >> 8;

然后回到 python

self.ser = serial.Serial("COM6", 115200, timeout=0.05)
f = self.ser.readline(2)
.
.
.
.
x = ord(self.f[1])  
x = x << 8 | ord(self.f[0])  
self.angle = x   #  this updates the angle value in the GUI 

它几乎总是给出正确的值但是! 该值突然指向零,这使我的 GUI 进行了深入研究(每 30-50 个样本一次)

%up0EEA$
³ 
iam here
³ 
179
%up0EEA$
³ 
iam here
³ 
179
.
.
.
%up0EEA$
Ê 
iam here
Ê 
202
%up0EEA$
ç 
iam here
ç 
231
%up0EEA$

iam here

257
%up0EEA$

iam here

261
%up0EEA$    
 Í         ---HERE ("Is that a white space before that ASCII " )  
iam here
 Í
0           
%up0EEA$
 ”
iam here
 ”         --- AGAIN !! 
0
%up0EEA$
î 
iam here
î 
238

现在!!我对如何摆脱串行数据中那个花哨的隐形字符感到震惊

我发现我的代码有很多self.ser.open()self.ser.close()

命令self.ser.flushInput()解决了我的问题

f = self.ser.readline()
        self.ser.flushInput()