1

I'm currently working on a raspberry pi project for school where I read data from an OBD2 to usb censor in my car.

When I'm using Screen to connect to the serial port everything works fine, but when I try to do it in python the serial.readline() returns an empty string.

Does anybody know how I can retrieve data from the serial port in python?

I've tried about every option available.

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
jeroenvdbergh
  • 362
  • 2
  • 16

2 Answers2

0

You may not be sending valid data to get a response. I believe the ODB2 interface uses the AT command set. Sending AT\n may be a good starting point.

I'm using a Bluetooth ODB2 interface and found that the serial baudrate was fixed. Using any other baudrate failed to correctly get data.

I recommend testing from putty or other terminal that supports serial ports, until you get the device to respond correctly. Then use valid settings to troubleshoot your code.

Jim Rush
  • 4,143
  • 3
  • 25
  • 27
  • Hi, i am using mcp2515 with nodemcu-32s and am able to receive the messages which have few ids like 2c4, 2c1, 340 etc on the internet i see 2c4 is for rpm and i have verified it too. My question is that no matter which library i use to request the supported pids i send a message like this sendmsgbuf(0x7DF, 0 (ext), 8 (len), then data with 21, 01, 00 etc) and then i get a reply like 7E8,03 ( i assume len), 7F(negative res), 01 (for request), 11 (sub function wrong). I want to know that my car dont support requesting pid’s from it? Or is there something else which i dont know. Thanks – Jahanzaib Khan Jul 04 '23 at 02:47
  • @BrutalJazzy This should probably be it's own question. – Jim Rush Jul 05 '23 at 03:41
  • https://stackoverflow.com/questions/76621704/toyota-ecu-replying-0x03-0x7f-0x01-0x11-to-02-01-00-pid-query-request-from-mcp25 – Jahanzaib Khan Jul 05 '23 at 15:11
0

You're not sending a \r\n with your command, ELM327 requires a new line character at the end of a command.

Damon Earl
  • 324
  • 2
  • 8