brendan-w / python-OBD

OBD-II serial module for reading engine data
GNU General Public License v2.0
1.03k stars 361 forks source link

Unable to Connect via Bluetooth - No Valid Ports Found #10

Closed merktassel closed 9 years ago

merktassel commented 9 years ago

I'm having trouble accessing my ELM327 Bluetooth Adapter from python-OBD on Yosemite. I'm able to connect to it fine from third party programs like OBD Auto Doctor, but when using Scantool to find the correct port it returns an empty array. I've also tried manually passing in some ports I found by running ls /dev/tty.* with no luck. I've been trying various suggestions I found through Google, as well typical troubleshooting steps like restarting and reinstalling for the past couple of days, but I'm out of ideas as to what I could be doing wrong.

brendan-w commented 9 years ago

Thanks for bringing this up:

What little bluetooth information I have is actually from the upstream I forked from (which was linux). I don't have a mac, or the bluetooth module yet, but here are my thoughts so far:

I fear that these suggestions may be no better than what you've already googled.

brendan-w commented 9 years ago

Also, if you haven't already, enable debug information with obd.debug.console = True, which will print out serial errors

merktassel commented 9 years ago

Thanks for the quick response! I now know for sure that /dev/tty.OBDII-Port is the port that I should be using. However, running python with sudo and enabling debug information didn't make any difference. I also tried running the following script:

import obd

obd.debug.console = True

connection = obd.OBD("/dev/tty.OBDII-Port")

cmd = obd.commands.RPM

response = connection.query(cmd)

print(response.value)
print(response.unit)

which returned

Starting python-OBD
Explicit port defined
Opening serial port...
Serial port successfully opened on /dev/tty.OBDII-Port
atz response: D?
ate0 response: ate0OK
Connected to ECU
querying for supported PIDs (commands)...
Sending command: 0100: Supported PIDs [01-20]
command returned: 41 00 80 40 00 01 41 00 BE 7F B8 13 41 00 80 00 00 01 
Receieved more data than expected, trying to parse anyways...
Sending command: 0120: Supported PIDs [21-40]
command returned: 41 20 80 01 80 01 41 20 80 07 E0 11 41 20 80 01 80 01 
Receieved more data than expected, trying to parse anyways...
Sending command: 0140: Supported PIDs [41-60]
command returned: 41 40 FE D0 00 00 41 40 40 00 00 00 41 40 40 00 00 00 
Receieved more data than expected, trying to parse anyways...
finished querying with 15 commands supported
========================================
'010C: Engine RPM' is not supported
None
None
brendan-w commented 9 years ago

That return data is quite peculiar...

This is not a serial/bluetooth problem. This is either a bug in the library, or a malfunctioning adapter. When you said it "connected" using OBD Auto Doctor, were you able to get accurate RPM data from the vehicle?

When I get time later tonight, I'll write a script to help debug this.

merktassel commented 9 years ago

Yes, I was able to get accurate data from the vehicle using OBD Auto Doctor. Today I tried hooking my adapter up to a different vehicle and running the script, and it worked. I then plugged the adapter back into my vehicle and ran the script, and it failed as it did before. I have a 2012 Chevrolet Sonic. What could be going on?

brendan-w commented 9 years ago

Found it!

Turns out, your chevy is modern enough that it has more than one ECU, so there may be multiple responses to one command. For instance (from your debug output):

41 00 80 40 00 01 41 00 BE 7F B8 13 41 00 80 00 00 01

# is actually a multiline response (the newlines got scrubbed somehow...)

41 00 80 40 00 01
41 00 BE 7F B8 13
41 00 80 00 00 01

The ELM327 spec details multiline responses on page 42. It requires turning on some headers, and filtering the data. Implementing it in the library (correctly) will take a fair bit of time and research, but if you need something now, I might be able to make a one-off patch to get you data from only the engine.

merktassel commented 9 years ago

For now I'll see if I can get my hands on a different vehicle to test. But if it's something fixable from your end, I'd appreciate a patch as soon as you have time. Thanks for helping me out.

brendan-w commented 9 years ago

I made a branch called multi-ecu. It works on my single ECU car (and in the tests for multi-ECU cars), but if you get some time, I'd love to have you test it on your Chevy. Either clone or download the multi-ecu branch, and install it with sudo python setup.py install (preferably in a virtualenv). Run the same script as before, and if it doesn't work, the debug output should tell me what I need to know.

brendan-w commented 9 years ago

While I haven't been able to do a live test on a car with multiple ECUs, it is stable on single ECU cars, and in the software tests. Since no existing functionality was lost, I have chosen to release to pypi. If you still wish to try it out, feel free to pip install the latest version.

merktassel commented 9 years ago

Sorry I haven't had a chance to try it out yet. I've been busy and the submission deadline is coming up soon. After that, I should have plenty of time to test it and let you know what I find. Thanks for being so responsive.

merktassel commented 9 years ago

I just updated to the latest release (0.3.0) and tried running the same script . I received the following output this time:

[obd] ========================== Starting python-OBD ==========================
[obd] Explicit port defined
[obd] Opening serial port '/dev/tty.OBDII-Port'
[obd] Serial port successfully opened on /dev/tty.OBDII-Port
[obd] write: 'atz\r\n'
[obd] read() found nothing
[obd] read() found nothing
[obd] read: ''
[obd] Connection Error:
[obd]     atz (reset) did not return with an ELM version
[obd] Failed to connect
[obd] =========================================================================
[obd] '010C: Engine RPM' is not supported
None
None

I decided to try running it a second time and received something different:

[obd] ========================== Starting python-OBD ==========================
[obd] Explicit port defined
[obd] Opening serial port '/dev/tty.OBDII-Port'
[obd] Serial port successfully opened on /dev/tty.OBDII-Port
[obd] write: 'atz\r\n'
[obd] read: 'Y\r?\r\r'
[obd] write: 'ate0\r\n'
[obd] read: 'ate0\rOK\r\r'
[obd] write: 'ath1\r\n'
[obd] read: 'OK\r\r'
[obd] Connection successful
[obd] querying for supported PIDs (commands)...
[obd] Sending command: 0100: Supported PIDs [01-20]
[obd] write: '0100\r\n'
[obd] read: '7E8 06 41 00 BE 7F B8 13 \r7EB 06 41 00 80 40 00 01 \r7EA 06 41 00 80 00 00 01 \r\r'
[obd] no valid data returned
[obd] finished querying with 0 commands supported
[obd] =========================================================================
[obd] '010C: Engine RPM' is not supported
None
None
brendan-w commented 9 years ago

Thank you! That tells me exactly what I need to know. It turns out, your car is also speaking a different protocol (in addition to having multiple ECUs). For now, I'd recommend rolling back to version 0.2.0 while I hash this out.

$ pip uninstall obd
$ pip install obd==0.2.0

I'm sorry it didn't work out of the box

merktassel commented 9 years ago

Have you made any progress with this? I'd be happy to test it out and provide diagnostic information if needed.

brendan-w commented 9 years ago

Quite a bit of progress. A lot of code has changed, so when it comes time for release, I would love to have you test. Not quite done with it yet, so I don't want to waste your time, but it's nearing the finish line. I'll ping you I have something complete.

btw, Your previous debug data was a huge help in designing the new system. Thank you.

brendan-w commented 9 years ago

Ok, looks like master (81fe93b052) is stable. Feel free to try it out at your leisure.

bitmarker commented 9 years ago

I have some troubles with connecting via bluetooth on my Mac. I could solve the problem by waiting 1 or 2 seconds after opening the serial port. Without this delay the communication seems not to work properly.

brendan-w commented 9 years ago

@bitmarker I have a hunch that this is a different issue than the one in this thread. Please open a new issue and post your debug output from the failure. Enable debug mode with obd.debug.console = True

brendan-w commented 9 years ago

@merktassel version 0.4.0 has all the completed work in it, and should (finally) solve this problem. Ok to close?

merktassel commented 9 years ago

Have you had a chance to field test it yet? If not, I'd like to try it out tomorrow to confirm things are good to go. Otherwise, go ahead and close the issue. Either way, thanks for addressing it for me.

brendan-w commented 9 years ago

Yes, it's been tested on quite a few vehicles. Just letting you know, more than anything. Thanks for all your help on this!