我的问题是,PySerial似乎丢失了一些数据包,我不知道为什么。
我有两个python脚本,第一个从文本文件中读取数据并将其写入微控制器,在那里操作数据。然后,微控制器通过不同的串行端口将修改后的数据发送回PC。 (澄清一下:我需要两个串口,因为在最终的应用程序中,脚本将在不同的PC上运行。但是,出于测试目的,在一台PC上运行这两个脚本更容易)
基本上,我的硬件设置如下:
PC ----(serial port 1)----> microcontroller
<---(serial port 2)-----
将数据写入微控制器后,我希望能够获得一定数量的数据字节。如果我使用终端程序(如HyperTerminal)来监控收到的数据,一切看起来都很好。但是,如果我尝试使用Python脚本读取数据,我只获得预期数据字节的一小部分。
例如:
+--------------------+--------------------+
| with HyperTerminal | with Python script |
+--------------------+--------------------+
| 1:W:00522 | 1:W:00522 |
| 1:W:00532 | 1:W:00532 |
| 1:W:00518 | 1:W:00522 |
| 1:W:00522 | 1:W:00526 |
| 1:W:00522 | 1:W:00514 |
| 1:W:00526 | 1:W:00520 |
| 1:W:00514 | 1:W:00514 |
| 1:W:00520 | 1:W:00522 |
| 1:W:00520 | 1:W:00526 |
| 1:W:00514 | 1:W:00520 |
| 1:W:00516 | 1:W:00526 |
| 1:W:00522 | 1:W:00520 |
| 1:W:00526 | 1:W:00524 |
| 1:W:00520 | 1:W:00526 |
| 1:W:00520 | 1:W:00532 |
| 1:W:00526 | 1:W:00506 |
| 1:W:00522 | 1:W:00520 |
| 1:W:00520 | 1:W:00526 |
| 1:W:00524 | 1:W:00524 |
| 1:W:00522 | 1:W:00526 |
| 1:W:00526 | 1:W:00514 |
| 1:W:00514 | 1:W:00522 |
| 1:W:00532 | 1:W:00520 |
| 1:W:00506 | 1:W:00510 |
| 1:W:00522 | 1:W:00506 |
| 1:W:00520 | |
| 1:W:00526 | |
| 1:W:00530 | |
| 1:W:00524 | |
| 1:W:00526 | |
| 1:W:00514 | |
| 1:W:00514 | |
| 1:W:00522 | |
| 1:W:00524 | |
| 1:W:00520 | |
| 1:W:00510 | |
| 1:W:00506 | |
+--------------------+--------------------+
正如您所看到的,如果我尝试使用Python脚本从串行端口读取数据,我会丢失一些数据。由于这个事实,如果我使用终端程序,我得到了预期的数据,我认为我的Python脚本有错误。
用于向微控制器发送数据的Python脚本如下所示:
import serial
import re
import time
class digiRealTest():
def __init__(self):
#configure serial port
self.ser = serial.Serial(7, 9600, parity=serial.PARITY_NONE)
def main(self):
filepath = 'C:\\Users\\Bernhard\\Desktop\\TomatoView\\Qt\\test_output.txt'
with open(filepath, 'r') as content_file:
content = content_file.read().decode("hex")
for match in re.finditer('[\02](.*?)[\03]', content, re.S):
res = match.group(1)
complete = '\x02' + res + '\x03'
# time.sleep(0.3) <-- if i uncomment this line, it work's!!!
self.ser.write(complete)
if __name__ == "__main__": #wenn Modul direkt ausgefuehrt wird
d = digiRealTest()
d.main()
我的Python脚本,用于接收从微控制器发送的数据:
import Queue
import threading
import serial
class mySerial(threading.Thread):
def __init__(self, queue):
super(mySerial, self).__init__()
self.queue = queue #the received data is put in a queue
self.buffer = ''
#configure serial connection
self.ser = serial.Serial(timeout = 0, port = 3, baudrate=9600)
def run(self):
while True:
self.buffer += self.ser.read(self.ser.inWaiting()) #read all char in buffer
if '\n' in self.buffer: #split data line by line and store it in var
var, self.buffer = self.buffer.split('\n')[-2:]
self.queue.put(var) #put received line in the queue
time.sleep(0.01) #do not monopolize CPU
class Base():
def __init__(self):
self.queue = Queue.Queue(0) #create a new queue
self.ser = mySerial(self.queue)
self.ser.start() #run thread
def main(self ):
while(True):
try:
var = self.queue.get(False) #try to fetch a value from queue
except Queue.Empty:
pass #if it is empty, do nothing
else:
print(var)
if __name__ == '__main__':
b = Base()
b.main()
我不知道为什么,但如果我在发送脚本中取消注释行#time.sleep(0.3)
,一切正常,我得到了预期的数据。所以对我来说,似乎我的脚本从串口读取数据的速度太慢......但为什么呢?
答案 0 :(得分:5)
接收器的分裂正在抛弃线条。这应该更好:
def run(self):
while True:
self.buffer += self.ser.read(self.ser.inWaiting()) #read all char in buffer
while '\n' in self.buffer: #split data line by line and store it in var
var, self.buffer = self.buffer.split('\n', 1)
self.queue.put(var) #put received line in the queue
time.sleep(0.01) #do not monopolize CPU
您还可以通过在创建串行端口时消除超时来消除轮询/休眠,并在队列中没有任何内容时请求1个字节。
class mySerial(threading.Thread):
def __init__(self, queue):
super(mySerial, self).__init__()
self.queue = queue #the received data is put in a queue
self.buffer = ''
#configure serial connection
self.ser = serial.Serial(port = 3, baudrate=9600)
def run(self):
while True:
self.buffer += self.ser.read(self.ser.inWaiting() or 1) #read all char in buffer
while '\n' in self.buffer: #split data line by line and store it in var
var, self.buffer = self.buffer.split('\n', 1)
self.queue.put(var) #put received line in the queue
答案 1 :(得分:1)
当你睡觉什么都没发生的时候......串口缓冲器相当小,可能只是在睡眠时间(在接收器上)填满...一旦它完全不能再用,直到你读完它所以其他位丢失(或者它们可能会覆盖缓冲区中的位)...因为串口没有一些握手来说嘿我收到了这些位
当您从发送方取消注释睡眠线时,它会让它睡眠,直到接收方已读取整个流并将其清除以获取更多数据
或者尝试
var, self.buffer = self.buffer.split('\n',1) #this will only split once
因为信息可能会丢失
var, self.buffer = self.buffer.split('\n')[-2:]
如果您已收到2(或更多)\n