我的問題是,PySerial似乎失去了一些數據包,我不知道爲什麼。PySerial丟失數據
我有兩個python腳本,第一個從文本文件中讀取數據並將其寫入微控制器,數據在其中進行處理。然後,微控制器通過不同的串行端口將修改後的數據發送回PC。 (爲了說明:我需要兩個串口,因爲在最終的應用程序中,腳本將運行在不同的PC上,但爲了測試目的,在一臺PC上運行這兩個腳本更容易)。硬件設置的樣子:
PC ----(serial port 1)----> microcontroller
<---(serial port 2)-----
將數據寫入到我期待獲得一定量的數據的微控制器後字節回。如果我使用終端程序(如超級終端)來監視收到的數據,一切都很正常。但是,如果我嘗試使用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)
這一行,那麼一切正常,我會收到預期的數據。所以對我來說,似乎我的腳本從串口讀取數據太慢......但是爲什麼?
謝謝,它工作得很好! – user2494129