2017-07-22 16 views
0

我正在通過使用python pyserial包的計算機的seral端口讀取數據。源代碼是一塊FPGA板。python pyserial如何在某些值後存儲數據

源發送24個字節,然後有一個空閒期。這個過程重演。因此爲了找出24次傳輸的第一個字節,有一個0字節,它是一種標記字節。所以當我得到這個標記字節時,我想要開始記錄下24個字節,並且實時爲接下來的24個字節重複這個操作。我可以識別第零個字節,但堅持下一步。

下面你可以看到當前的代碼...提前

感謝

import serial 
port = serial.Serial('/dev/ttyUSB1', 115200) 
file = open("my_file.txt","a") 
i=0 
j=0 
while True: 
    i=i+1 
    print "  i --> % d " % (i) 

    raw_data = ord(port.read()) 

    if raw_data==127: 
     j=j+1 
     print('-----',j,'------------------------------') 
    else : 
     print(raw_data,a) 
     file.write(str(raw_data) + "\n")    

file.close() 
+0

怎麼沒看到你_ **確定零字節** _,'如果raw_data == 127:'不是零? – stovfl

+0

127是指示符或第零位告訴有用的24字節在此之後。 「1字節指示符+ 24字節」 – macellan

回答

0

問題:...如何將數據經過一定的值存儲

彙總到list的24字節,例如:

# Get in Sync with Byte == 127 
while True: 
    raw_data = ord(port.read()) 
    if raw_data == 127: 
     break 


record = [] 
while True: 
    raw_data = ord(port.read()) 

    if raw_data != 127: 
     record.append(raw_data) 
    else: 
     # Write Record 
     print('record[{}]:{}'.format(len(record), record)) 
     # Empty Record List 
     record = [] 

的Python»3.6.2文檔:class list([iterable])

Lists may be constructed in several ways: 

    Using a pair of square brackets to denote the empty list: [] 
    Using square brackets, separating items with commas: [a], [a, b, c] 
    Using a list comprehension: [x for x in iterable] 
    Using the type constructor: list() or list(iterable) 

測試使用Python 3.4.2

相關問題