2016-03-01 13 views
0

我有一個小的python示例,我從另一個網站上下來了。我正試圖瞭解如何使用它從串行讀取。Python串行讀取返回奇怪的值

我送從FRDM K64f板信息通過串行和Python程序讀取此,但返回一個奇怪的值,下面是其中的一個示例:

YVkJZC

我的Python代碼:

import time 
import serial 

# configure the serial connections (the parameters differs on the  device you are connecting to) 
ser = serial.Serial(
port='/dev/ttyACM0', 
baudrate=9600, 
parity=serial.PARITY_ODD, 
stopbits=serial.STOPBITS_TWO, 
bytesize=serial.SEVENBITS 
) 

ser.isOpen() 

print 'Enter your commands below.\r\nInsert "exit" to leave the  application.' 

input=1 
while 1 : 
    # get keyboard input 
    input = raw_input(">> ") 
     # Python 3 users 
     # input = input(">> ") 
    if input == 'exit': 
     ser.close() 
     exit() 
    else: 
     # send the character to the device 
     # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device) 
     ser.write(input + '\r\n') 
     out = '' 
    # let's wait one second before reading output (let's give device time to answer) 
     time.sleep(1) 
     while ser.inWaiting() > 0: 
      out += ser.read(1) 

     if out != '': 
      print ">>" + out 

這是我的板代碼:

int main(){ 
    Serial pc(USBTX, USBRX); 
    pc.baud(9600); 
     while(1){ 
      char c = pc.getc(); 
     if((c == 'w')) { 
      pc.printf("Hello"); 
     } 
    } 
} 

我得到的確切回報是這樣的:

Enter your commands below. 
Insert "exit" to leave the application. 
>> w 
>>YVkJ�ZC 
>> 
+0

你希望從'ser'獲得什麼樣的數據? – acdr

+0

如果沒有真正想過它,我還沒有在Python中使用過串行。 BUut我希望我可以從板 – UniqueName

+0

返回字符串的值,因爲也許你得到的那些「無意義」字符串實際上是你得到的,如果你解釋你得到的數據作爲unicode(或任何編碼)字符串。 – acdr

回答

0

管理解決這個問題。

我的序列聲明似乎沒有正常工作。

回到pyserial文檔並聲明我的序列如下,並使用readline()解決了這個問題。

ser = serial.Serial('/dev/ttyACM0')