Parsers

class obdii.parsers.formula.Formula(expression: str)[source]

Bases: object

Represents a mathematical formula based on a string expression.

Example

# 11 (dec) -> A
# 64 (dec) -> B
unparsed = [11, 64]

formula = Formula("(256*A+B)/4")
result = formula(unparsed)
# (256 * 11 + 64)/4

>>> result
720.0
__init__(expression: str)[source]

Initialize the Formula with a given expression.

Parameters:

expression (str) – The expression string to evaluate. Variable names are converted to uppercase.

__call__(unparsed: List[int]) int | float[source]

Evaluate the formula on the given unparsed.

Parameters:

unparsed (List[int]) – The data to evaluate the formula against.

class obdii.parsers.formula.MultiFormula(*expressions: str)[source]

Bases: object

Represents multiple mathematical formulas based on string expressions.

Example

# 143 (dec) -> A
# 125 (dec) -> B
# 149 (dec) -> C
# 128 (dec) -> D
# 111 (dec) -> E
unparsed = [143, 125, 149, 128, 111]

engine_torque = MultiFormula("A-125", "B-125", "C-125", "D-125", "E-125")
result = engine_torque(unparsed)

>>> result
[18, 0, 24, 3, -14]
__init__(*expressions: str) None[source]

Initialize formulas based on given expressions.

Parameters:

*expressions (str) – Expression strings to evaluate. Variable names are converted to uppercase.

__call__(unparsed: List[int]) List[int | float][source]

Evaluate all formulas on the given unparsed.

Parameters:

unparsed (List[int]) – The data to evaluate the formulas against.

class obdii.parsers.pids.SupportedPIDS(base_pid: int)[source]

Bases: object

Parse supported PIDs from the response data.

Each bit in the 4 byte (32 bit) response represents support for one PID, starting from the Base PID. A bit set to 1 indicates the PID is supported, 0 means it is not. Bits are read from most-significant to least-significant.

Example

unparsed = [190, 31, 168, 19]
# 190       31        168       19
# B    E    1    F    A    8    1    3
# 1011 1110 0001 1111 1010 1000 0001 0011

supported_pids = SupportedPIDS(0x01)
result = supported_pids(unparsed)

>>> result
[0x01, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x13, 0x15, 0x1C, 0x1F, 0x20]
__init__(base_pid: int) None[source]

Initialize SupportedPIDS with a given base PID.

Parameters:

base_pid (int) – The base PID from which the support bits start.

__call__(unparsed: List[int]) List[int][source]

Parse supported PIDs from the response data.

Parameters:

unparsed (List[int]) – The data to parse.

Returns:

List of supported PIDs.

Return type:

List[int]

class obdii.parsers.pids.EnumeratedPIDS(mapping: Dict[int | Iterable[int], Any])[source]

Bases: object

Map parsed data bytes to enumerated values.

Example

unparsed = [0, 0]

fuel_system_status = EnumeratedPIDS(
    {
        0: "The motor is off",
        1: "Open loop due to insufficient engine temperature",
        2: "Closed loop, using oxygen sensor feedback to determine fuel mix",
        4: "Open loop due to engine load OR fuel cut due to deceleration",
        8: "Open loop due to system failure",
        16: "Closed loop, using at least one oxygen sensor but there is a fault in the feedback system",
    }
)
result = fuel_system_status(unparsed)

>>> result
[(0, "The motor is off"), (0, "The motor is off")]
__init__(mapping: Dict[int | Iterable[int], Any]) None[source]

Initialize EnumeratedPIDS with a mapping dictionary.

Parameters:

mapping (Dict[Union[int, Iterable[int]], Any]) – A dictionary mapping byte values to their corresponding enumerated meanings.

__call__(unparsed: List[int]) List[Tuple[int, Any]][source]

Map parsed data bytes to enumerated values.

Parameters:

unparsed (List[int]) – The data to map.

Returns:

List of tuples containing the byte value and its corresponding enumerated meaning.

Return type:

List[Tuple[int, Any]]

class obdii.parsers.dtc.DTC(raw: str)[source]

Bases: object

Diagnostic Trouble Code (DTC) representation.

5 characters structured as follows:

U    0    1    5    8
|    |    |    |____|
|    |    |    descriptor
|    |    system
|    scope
category
__init__(raw: str) None[source]

Initialize DTC object.

Parameters:

*raw (str) – The raw DTC code string, e.g. “U0158”.

code: str

Full DTC code, e.g. “U0158”.

category: Category

DTC category, e.g. Category.NETWORK.

scope: Scope

DTC scope, e.g. Scope.GENERIC.

system: System

DTC system, e.g. System.FUEL_AIR_METERING.

descriptor: str

DTC descriptor, e.g. “58”.

classmethod from_bytes(high: int, low: int) DTC[source]

Parse two raw bytes into a DTC.

0xC1, 0x58
1100 0001 0101 1000
U 0  1    5    8
static parse(unparsed: List[int]) List[DTC][source]

Parse a raw Mode 03 CAN payload into a list of DTCs.

[0x02, 0xC1, 0x58, 0x01, 0x96, 0x00, 0x00, 0x00]
    |   |_____|     |_____|     |___________|
count    DTC #1      DTC #2      padding (ignored)
Parameters:

unparsed (List[int]) – The data to evaluate the formula against.