obd2

package
v0.783.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package obd2 decodes OBD-II / SAE J1979 Mode-01 ("show current data") responses into engineering values — turning the raw measurement bytes of a diagnostic response into RPM, speed, coolant temperature, MAF, etc. via the standard per-PID formulas.

Wrap-vs-native judgement

Native. The J1979 Mode-01 PID set and its conversion formulas are public, exact, and transport-independent (the same A/B-byte formulas apply whether the response arrived over CAN/ISO 15765, J1850 VPW/PWM, or ISO 9141) — documented in SAE J1979 and reproduced identically by python-OBD, ELM327-based tools, and the widely-cited OBD-II PID tables. Decoding is a static table of (name, unit, byte-count, formula) entries; no hardware, no vendor SDK, no probing. The existing internal/j1850 decoder names the PID but stops at the raw payload bytes ("Engine RPM" + payload_hex) — this computes the value those bytes encode, and works for any transport since the caller supplies the already-extracted Mode-01 payload.

No confidently-wrong output

Only PIDs whose formula is in the table are given a value; an unknown PID, or a known PID with too few data bytes, is surfaced with its raw hex (and name when known) plus a note — never a guessed number. Manufacturer- specific PIDs and the bitmask/string PIDs (e.g. 0x00/0x20 "PIDs supported", 0x03 fuel status, VIN) are intentionally left to the raw bytes.

Index

Constants

View Source
const Mode01Response = 0x41

Mode01Response is the service byte of a Mode-01 ("current data") response — the request mode 0x01 plus 0x40.

Variables

This section is empty.

Functions

This section is empty.

Types

type DTC added in v0.396.0

type DTC struct {
	Code                 string `json:"code"`                  // canonical form, e.g. "P0143"
	Category             string `json:"category"`              // Powertrain | Chassis | Body | Network
	Raw                  string `json:"raw"`                   // the 2 source bytes as hex
	Generic              bool   `json:"generic"`               // SAE/ISO-controlled (first digit 0)
	ManufacturerSpecific bool   `json:"manufacturer_specific"` // manufacturer-controlled (first digit 1)
}

DTC is one decoded Diagnostic Trouble Code.

func DecodeDTC added in v0.396.0

func DecodeDTC(aByte, bByte byte) DTC

DecodeDTC unpacks a 2-byte Diagnostic Trouble Code into its canonical J2012 form. Byte A's top two bits select the category letter (P/C/B/U), the next two bits are the first digit (0-3), A's low nibble is the second digit, and byte B's two nibbles are the third and fourth digits.

A = 0x01, B = 0x43  ->  "P0143"
A = 0x04, B = 0x20  ->  "P0420"

type DTCResponse added in v0.396.0

type DTCResponse struct {
	Mode     int      `json:"mode,omitempty"`      // 0x43 / 0x47 / 0x4A when a service byte was present
	ModeName string   `json:"mode_name,omitempty"` // human service name
	Count    int      `json:"count"`               // number of trouble codes decoded
	DTCs     []DTC    `json:"dtcs"`
	Notes    []string `json:"notes,omitempty"`
}

DTCResponse is the decoded view of a stored/pending/permanent DTC response.

func DecodeDTCResponse added in v0.396.0

func DecodeDTCResponse(hexStr string) (*DTCResponse, error)

DecodeDTCResponse parses a Mode-03/07/0A DTC response into its trouble codes. The input may be the response payload prefixed with the service byte (0x43 / 0x47 / 0x4A) or the bare stream of 2-byte DTCs. All-zero pairs (0x0000) are padding and are skipped. Separators and a 0x prefix are tolerated.

type Reading

type Reading struct {
	PID     int      `json:"pid"`
	PIDHex  string   `json:"pid_hex"`
	Name    string   `json:"name"`
	RawHex  string   `json:"raw_hex"`
	Value   *float64 `json:"value,omitempty"`
	Unit    string   `json:"unit,omitempty"`
	Formula string   `json:"formula,omitempty"`
	Note    string   `json:"note,omitempty"`
}

Reading is the decoded view of one Mode-01 PID.

func DecodePID

func DecodePID(pid int, data []byte) *Reading

DecodePID decodes one Mode-01 PID from its measurement bytes, computing the engineering value via the J1979 formula when the PID and byte count are known.

func DecodeResponse

func DecodeResponse(hexStr string) (*Reading, error)

DecodeResponse parses a Mode-01 response payload — the service byte (0x41) + PID + measurement bytes — and decodes the PID. A request byte (0x01) is also accepted (named, no value, since a request carries no measurement). Separators and a 0x prefix are tolerated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL