Skip to content
brendanwhitfield edited this page Dec 6, 2014 · 23 revisions

A Command in python-OBD is an object used to query information from the vehicle. They contain all of the information neccessary to perform the query, and decode the cars response. Python-OBD has built in tables for the most common commands. They can be looked up by name, or by mode and PID (for a full list, see commands.py).

import obd

c = obd.commands.RPM

# OR

c = obd.commands['RPM']

# OR

c = obd.commands[1][12] # mode 1, PID 12 (decimal)

Once a connection is made, python-OBD will determine which commands your car supports. The has_command() function can be used check support for any given command.

	import obd
	connection = obd.Obd()

	if connection.has_command(obd.commands.RPM):
		...

A list of all supported commands is stored in the supported_commands property of the connection object.

Custom commands

If the command you need is not in python-OBDs tables, you can create a new OBDCommand object. The constructor accepts the following arguments (each will become a property).

Argument Type Description
name string (human readability only)
desc string (human readability only)
mode string OBD mode (hex)
pid string OBD PID (hex)
bytes int Number of bytes expected in response
decoder callable Function used for decoding the hex response
supported (optional) bool Flag to prevent the sending of unsupported commands (False by default)

The decoder argument is a function of following form.

	def <name>(_hex):
		...
		return (<value>, <unit>)

The _hex argument is the data recieved from the car, and is guaranteed to be the size of the bytes property specified in the OBDCommand.

For example:

import obd
from obd.utils import unhex

def rpm(_hex):
	v = unhex(_hex) # helper function to convert hex to int
	v = v / 4.0
	return (v, obd.Unit.RPM)

c = obd.OBDCommand("RPM", "Engine RPM", "01", "0C", 2, rpm)

Docs have been moved to python-obd.readthedocs.org

Clone this wiki locally