2013-06-01 31 views
2

我是python和pyserial的新手。我在我的電腦中安裝了python 2.7.4和pyserial-2.5 win32。在這裏,我使用微控制器設備作爲主(主),我的電腦作爲從(次)。這裏每次微控制器都會傳輸數據,而且我的電腦必須通過串口接收數據。我想用python中的代碼來接收連續的數據。這裏傳輸的數據大小會隨時變化。在這裏,我寫了一個代碼發送數據,並且所述代碼是Python代碼從串口連續接收可變數據

import serial 
ser= serial.serial("COM10", 9600) 
ser.write("Hello world\n") 
x = ser.readline() 
print(x)  

與此代碼我可以將數據傳輸到其他電腦,我通過交叉檢查在其他的PC打開超級終端和我可以看到所發送的數據(世界你好)。

我也寫代碼來接收數據,

import serial 
ser=serial.serial("COM10", 9600) 
while 1: 
if ser.inwaiting(): 
val = ser.readline(ser.inwaiting()) 
print(val) 

如果我發送數據(你好嗎)從超級終端,我可以在我的電腦接收數據,與上面的代碼。

到現在爲止每件事情都很好。

我現在的問題是,當微控制器設備在可變時間段傳輸變量數據時,我需要在我的電腦中接收數據(安裝了python),請幫我編寫代碼。我是否需要使用緩衝區來存儲收到的數據。如果是的話,代碼將如何。爲什麼以及如何在python中使用緩衝區。根據我在互聯網上的搜索,緩衝區用於切分字符串。

在此先感謝。 對不起我的英文不好。 希望有人會幫助代碼。

+0

我不明白你的問題,你想只存儲數據而不是打印它嗎?在這種情況下,創建一個新列表('data = []')並在循環內附加您接收的內容('data.append(val)') –

+0

在打印之前是否需要將傳輸的數據存儲在緩衝區中? – Steve

+0

「緩衝區」是什麼意思?列表是用於存儲所需數據的有效結構。 –

回答

1

通常,您與微通信所做的工作是將單個字符用於輕量級或創建通信協議。基本上你有一個開始標誌,結束標誌和某種校驗和,以確保數據正確傳輸。有很多方法可以做到這一點。

以下代碼適用於Python 3.您可能必須對字節數據進行更改。

# On micro 
data = b"[Hello,1234]" 
serial.write(data) 

在電腦上,你會運行,如果您使用的是GUI

def read_data(ser, buf=b'', callback=None): 
    if callback is None: 
     callback = print 

    # Read enough data for a message 
    buf += ser.read(ser.inwaiting()) # If you are using threading +10 or something so the thread has to wait for more data, this makes the thread sleep and allows the main thread to run. 
    while b"[" not in buf or b"]" not in buf: 
     buf += ser.read(ser.inwaiting()) 

    # There may be multiple messages received 
    while b"[" in buf and b']' in buf: 
     # Find the message 
     start = buf.find(b'[') 
     buf = buf[start+1:] 
     end = buf.find(b']') 
     msg_parts = buf[:end].split(",") # buf now has b"Hello, 1234" 
     buf = buf[end+1:] 

     # Check the checksum to make sure the data is valid 
     if msg_parts[-1] == b"1234": # There are many different ways to make a good checksum 
      callback(msg_parts[:-1]) 

    return buf 

running = True 
ser = serial.serial("COM10", 9600) 
buf = b'' 
while running: 
    buf = read_data(ser, buf) 

線程是非常有用的。然後,當GUI顯示數據時,您可以讓線程在後臺讀取數據。

import time 
import threading 

running = threading.Event() 
running.set() 
def thread_read(ser, callback=None): 
    buf = b'' 
    while running.is_set(): 
     buf = read_data(ser, buf, callback) 

def msg_parsed(msg_parts): 
    # Do something with the parsed data 
    print(msg_parsed) 

ser = serial.serial("COM10", 9600) 
th = threading.Thread(target=thread_read, args=(ser, msg_parsed)) 
th.start() 


# Do other stuff while the thread is running in the background 
start = time.clock() 
duration = 5 # Run for 5 seconds 
while running.is_set(): 
    time.sleep(1) # Do other processing instead of sleep 
    if time.clock() - start > duration 
     running.clear() 

th.join() # Wait for the thread to finish up and exit 
ser.close() # Close the serial port 

注意,在線程例子中,我使用回調它是被作爲一個變量傳遞後來被稱爲函數。另一種方式是將數據放入隊列中,然後在隊列的另一部分處理隊列中的數據。