2017-10-16 46 views
0

我正在使用pyserial同時從3個串行端口讀取數據,並使用python將這些數據記錄到3個單獨的帶時間戳的文本文件。但是,經過一段時間(少於約5分鐘)後,寫入文本文件的數據將無法正確打印。準確的數據的使用python從3個串行端口進行不一致的數據記錄。

例如:

2017年10月16日10點41分27秒

傳感器讀數平均讀數/平均左上:1
7.00 0.14 midLeft:2 8.00 0.25 botLeft: 3 9.00 0.33 topRight:4 10.00
0.40 midRight:5 11.00 0.45 botRight:不準確的數據的6 12.00 0.50

例如:

2017年10月16日10點55分十一秒

傳感器Reading新3克讀/平均左上:1
7.00 0.14 midLeft:2 8.00 0.25 botLeft:3 9.00 00.50 HT:4 10.00 0.40 midRight:5 11.00
0.45 botRight:6 12.00 0.50 0.45 botRight:6 12.00 0.50

這裏是我正在使用的代碼:

# to use microseconds, delete ".replace(microsecond = 0)" 

## includes 
from __future__ import print_function 
import threading, serial, time, io, datetime 
from serial import Serial 

device1 = "_base_station" ## device name used for filename 
addr1 = "COM39" ## edit serial port here 

device2 = "_collector" ## device name used for filename 
addr2 = "COM40" ## edit serial port here 

device3 = "_receiver" ## device name used for filename 
addr3 = "COM42" ## edit serial port here 

baud = 57600 ## edit baud rate here 

## function for initializing serial ports 
def init_port(addr, baud): 
    return serial.Serial(
     port = addr,\ 
     baudrate = baud,\ 
     parity=serial.PARITY_NONE,\ 
     stopbits=serial.STOPBITS_ONE,\ 
     bytesize=serial.EIGHTBITS,\ 
     timeout=.01) 

## initialize serial ports 
ser1 = init_port(addr1, baud) 
ser2 = init_port(addr2, baud) 
ser3 = init_port(addr3, baud) 

## print port numbers so the user knows the script is working 
print("Collecting data from port: " + ser1.portstr + "\r\n") ## give status update 
print("Collecting data from port: " + ser2.portstr + "\r\n") ## give status update 
print("Collecting data from port: " + ser3.portstr + "\r\n") ## give status update 

def handle_data(ser, device): 
    while True: 
     line = ser.readline() ## get line from serial port 
     filename = datetime.datetime.now().strftime("%Y-%m-%d-%H") + device + ".txt" ## create filename based on device and time. updates at midnight. 
     filehandle = open(filename, 'a') ## append data to file. 

     if str(line) == "#\r\n": ## logging delimiter 
      filehandle.write("\r\n" + str(datetime.datetime.now().replace(microsecond = 0)) + "\r\n") ## start each data entry with a timestamp 
     else: 
      filehandle.write(str(line)[:-1]) ## log line (strip off extra line, data is already being parsed by line) 
      filehandle.flush() 

     filehandle.close() ## close file 
    ser.close() ## close serial port 

## create threads 
thread1 = threading.Thread(target = handle_data, args = [ser1, device1]) 
thread2 = threading.Thread(target = handle_data, args = [ser2, device2]) 
thread3 = threading.Thread(target = handle_data, args = [ser3, device3]) 

## start threads 
thread1.start() 
thread2.start() 
thread3.start() 

我也嘗試過使用多個腳本而不使用線程,並且問題仍然存在。

任何理論爲什麼發生這種情況?任何解決方案

在此先感謝。

+0

當使用多線程或多處理這是一個非常好的主意,用成語: '如果__name__ ==「__main __」:' – Aaron

+0

它看起來像你同時沒有任何同步寫入同一文件。儘管如此,啓動多個進程或線程似乎是矯枉過正,你可以做這樣的事情https://stackoverflow.com/questions/16255807/pyserial-is-there-a-way-to-select-on-multiple-端口在一次 – pvg

+0

謝謝pvg。它看起來像寫入同一個文件,但設備名稱正在從線程參數傳遞給文件名。我會檢查出來的。 另外,謝謝Aaron。我是Python新手,所以所有的幫助都是值得歡迎的。 –

回答

0

超時看起來很可疑,我會延長串口的超時時間。我懷疑你得到的碎片數據是從超時超過傳入數據時開始的。

def init_port(addr, baud): 
    return serial.Serial(
     port = addr,\ 
     baudrate = baud,\ 
     parity=serial.PARITY_NONE,\ 
     stopbits=serial.STOPBITS_ONE,\ 
     bytesize=serial.EIGHTBITS,\ 
     timeout=1.0) 

您正在使用.readline(),它會尋找「\ n」終止,但如果超過超時會提前退出。

相關問題