obd2: SAE J1979 OBD-II services
| (require obd2) | package: obd2 |
The "obd2" package implements the message layer of OBD-II — the SAE J1979 diagnostic services ("modes") that every road vehicle sold in major markets must answer: live sensor PIDs, stored and pending diagnostic trouble codes, freeze frames, vehicle information and emissions readiness.
Everything is implemented in pure Racket. The Mode 01 PID table is a frozen port of the Python python-OBD library (0.7.3), whose live decoders also provide the arithmetic test anchors. DTC text forms are shared with the uds package.
1 Quick start
> (make-request 1 12) #"\1\f"
> (define r (parse-response 1 (bytes 65 12 14 112))) > (obd-response-pid r) 12
> (obd-response-value r) '(924.0 . "rpm")
> (decode-pid (pid-for 0) (bytes 190 31 168 19)) '(1 3 4 5 6 7 12 13 14 15 16 17 19 21 28 31 32)
2 The PID table
struct
number : exact-nonnegative-integer? name : string? data-bytes : (integer-in 0 255) decode : symbol?
value
mode-01-pids : (listof pid?)
procedure
number : exact-nonnegative-integer?
3 Decoding
procedure
(decode-pid p payload)
→
(or/c pair? exact-integer? boolean? string? (listof exact-integer?) #f (listof (or/c string? #f))) p : pid? payload : bytes?
'supported-pids and 'o2-presence — the list of PID numbers declared by a 32-bit bitmap;
'status / 'monitor-status — (list* mil-on? dtc-count ignition-type) via status-word;
'freeze-dtc — the DTC text (e.g. "P0420") or #f;
'fuel-status / 'air-status / 'obd-compliance / 'fuel-type — the J1979 text tables, with #f for invalid or out-of-range entries.
procedure
(status-word data)
→ (list* boolean? exact-nonnegative-integer? list?) data : bytes?
procedure
(supported-pids base bitmap)
→ (listof exact-nonnegative-integer?) base : exact-nonnegative-integer? bitmap : bytes?
procedure
(parse-freeze-dtc data) → (or/c string? #f)
data : bytes?
procedure
(parse-dtcs data) → (listof string?)
data : bytes?
4 Services
struct
(struct obd-response (mode pid negative? nrc value))
mode : exact-nonnegative-integer? pid : (or/c exact-nonnegative-integer? #f) negative? : boolean? nrc : (or/c exact-nonnegative-integer? #f) value : any/c
procedure
(make-request mode pid) → bytes?
mode : exact-nonnegative-integer? pid : exact-nonnegative-integer?
procedure
(parse-response mode data) → obd-response?
mode : exact-nonnegative-integer? data : bytes?
procedure
(vin-from-response data) → string?
data : bytes?
5 License
MIT. The PID table is ported from python-OBD (MIT).