因此,大型项目的一部分需要使用raspberry pi从串行端口接收一个长的十六进制字符串。我原以为一切正常,但后来发现它在字符串中间丢失了一大块数据。在
def BUTTON_Clicked(self, widget, data= None):
ser = serial.Serial("/dev/ex_device", 115200, timeout=3)
RECEIVEDfile = open("RECIEVED.txt", "r+", 0) #unbuffered
#Commands sent out
ser.write("*n\r")
time.sleep(1)
ser.flush()
ser.write("*E")
ser.write("\r")
#Read back string rx'd
RECEIVED= ser.read()
RECEIVED= re.sub(r'[\W_]+', '', RECEIVED) #remove non-alphanumeric characters (caused by noise maybe?)
RECEIVEDfile.write(re.sub("(.{4})", "\\1\n", RECEIVED, 0, re.DOTALL)) #new line every 4 characters
RECEIVEDfile.close
ser.write("*i\r")
ser.close
这是用于检索数据的脚本,波特率和串行命令设置正确,脚本作为“unbuffered”(-u)运行,但不会保存完整的字符串。字符串长度约为16384个字符,但仅保存了大约9520个字符(可能有所不同)(无法提供用于分析的字符串)。有人知道我错过了什么吗?为你能给我的任何帮助干杯。在
很高兴我的评论有帮助!在
将超时设置为较低的数值,例如1秒。那就试试这样吧。它试图读取一个大的块,但超时很快,并且不会长时间阻塞。任何读过的内容都会被放入一个列表(rx_buf)。然后永远循环,只要还有待读取的字节。真正的问题是“知道”什么时候不需要更多的数据。在
我把块放在列表中的原因是join操作比字符串上的'+='更有效。在
根据this question,您需要分块(这里是单字节)从缓冲区中读取数据:
我怀疑你的情况是你得到了一个完整的“缓冲区”的数据,这取决于缓冲区的状态。在
相关问题 更多 >
编程相关推荐