Last active
November 3, 2015 01:45
-
-
Save brendan-w/fda7266f65df0fd64556 to your computer and use it in GitHub Desktop.
Attempts to parse obd data from the zaicus-gps-tracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# written for obd==0.4.1 | |
# this will need a few modifications once 0.5.0 rolls out | |
import obd | |
from obd.utils import Response, unhex | |
from obd.protocols.protocol import Message | |
def parse(data): | |
""" converts hex string into dict of OBDCommands and Responses """ | |
output = {} | |
# removes and returns the first N characters of a string | |
take = lambda s, n: (s[:n], s[n:]) | |
while data: | |
pid, data = take(data, 2) | |
pid = unhex(pid) # convert hex to integer | |
# check that it's supported | |
if not obd.commands.has_pid(1, pid): | |
print("WARNING: unknown PID: %d" % pid) | |
# WARNING: these are sensors that python-OBD can't decode yet | |
# I'm skipping them manually for now | |
if pid == 99: | |
value, data = take(data, 2*2) # chop off the correct amount of data. Happens to be 2 bytes for PID 99 | |
continue | |
# elif pid == xx: | |
# consult the OBD-II PID tables if you run into more | |
# if we've come this far, then there's nothing we can do to recover | |
# from this (without knowing the size of the unknown command's return data). | |
# Return all we have | |
print("ERROR: cant parse PID %d, stopping..." % pid) | |
return output | |
command = obd.commands[1][pid] # lookup the corresponding command | |
# consume the data | |
value, data = take(data, (command.bytes * 2)) # x2 because 1 byte = 2 hex chars | |
# parse the message | |
response = Response(command) # make a new response object | |
response.value, response.unit = command.decode(value) # decode the hex data | |
output[command] = response | |
return output | |
def query(obd_data, command): | |
""" returns null responses when no data is available """ | |
if command in obd_data: | |
return obd_data[command] | |
else: | |
print("No data for %s" % str(command)) | |
return Response() # empty response | |
if __name__ == "__main__": | |
obd_data = parse("05830b5e0c95630d630f05111f0e4e074e630163145b63428c54") | |
# example | |
r = query(obd_data, obd.commands.RPM) | |
print(r.value, r.unit) | |
r = query(obd_data, obd.commands.COOLANT_TEMP) | |
print(r.value, r.unit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment