2015-06-01 114 views
1

我目前正在研究樹莓派項目,在那裏我從OBD2讀取數據到我的汽車中的USB檢查員。無法從樹莓派上的OBD2串行端口連接讀取數據

當我使用屏幕連接到串口一切工作正常,但是當我嘗試在python中執行它時,serial.readline()返回一個空字符串。

有誰知道我怎麼能從python的串口檢索數據?

我試過了可用的每一個選項。

import serial 

ser = 0 

#Function to Initialize the Serial Port 
def init_serial(): 

    global ser   
    ser = serial.Serial() 
    ser.baudrate = 38400 
    ser.port = '/dev/ttyUSB0' 
    ser.timeout = 1 
    ser.open()   #Opens SerialPort 

    # print port open or closed 
    if ser.isOpen(): 
    print 'Open: ' + ser.portstr 
    #Function Ends Here 

init_serial() 

temp = raw_input('Type what you want to send, hit enter:\r\n') 
ser.write(temp)   #Writes to the SerialPort 

while 1:  
     bytes = ser.readline() #Read from Serial Port 
     print bytes  #Print What is Read from Port 

回答

0

您可能未發送有效數據以獲取響應。我相信ODB2接口使用AT命令集。發送AT \ n可能是一個很好的起點。

我正在使用藍牙ODB2接口,發現串口波特率是固定的。使用任何其他波特率無法正確獲取數據。

我建議從膩子或其他支持串口的終端進行測試,直到您得到設備正確響應。然後使用有效的設置來解決您的代碼。

0

您不會發送\ r \ n與您的命令,ELM327需要一個新的行字符在命令的末尾。