obd2_kit: Dart Library for ELM327 OBD-II Adapters
Zusammenfassung
Pixel’s announcement introduces obd2_kit, a Dart library that handles ELM327 sessions and SAE J1979 services over transports such as Bluetooth, BLE, Wi-Fi, and USB. It covers adapter initialization, command timeouts and cancellation, multi-ECU and multi-frame replies, and live polling—helping developers build vehicle diagnostics apps without reimplementing protocol handling.
DEV Community
obd2_kit: talk to ELM327 OBD-II adapters from Dart over any transport
Reading data from a car looks simple on paper. You plug a small ELM327 adapter into the OBD-II port, send 010C, and get the engine speed back. In practice the adapter is a text terminal with its own habits: it echoes your commands, prints SEARCHING... while it looks for the bus, answers in several small chunks, and reports errors as plain words such as NO DATA or BUS BUSY. Replies can come from several ECUs at once, and a long reply such as the VIN is split over several frames. On top of that, the link to the adapter is different in every app: Bluetooth SPP, BLE UART, Wi-Fi TCP or USB serial. obd2_kit is a Dart library that handles the protocol side and leaves the link to you. It works over any byte transport and covers the adapter session, the SAE J1979 services, multi-ECU and multi-frame replies, and live polling. What it does The ELM327 session. Elm327 runs the init sequence (ATZ, ATE0, ATL0, ATS0, ATH1 or ATH0, ATAT1, ATSP0), then sends a 0100 request that runs the protocol search, and reads the protocol the adapter found with ATDPN. It waits for the > prompt, also when a reply arrives in many small chunks, and removes the command echo, SEARCHING... and BUS INIT: ...OK. Error replies become an ElmErrorException carrying an ElmError value, so you match on a type instead of comparing strings. Commands go through a FIFO queue that sends one at a time. Each command has a timeout and can take an ObdCancelToken. A queued command that is cancelled is never sent. A running command that times out or is cancelled is interrupted: the session sends one byte, waits for the prompt, and then goes on, so a late reply is never matched to the next command. The J1979 services. ObdClient covers these modes: Mode 01, current data: readPid returns the reply from the lowest ECU id, readPidAll returns the replies from every ECU, and supportedPids walks the bitmaps at 00, 20, 40, 60, 80, A0 and C0. Mode 02, freeze frame, including PID 02 (the code that stored the frame). Mode 03 stored codes, Mode 07 pending codes and Mode 0A permanent codes, grouped by ECU. Mode 04, clear codes. It runs only with clearDtcs(confirm: true). Mode 09: VIN, calibration IDs, ECU name and the supported PIDs. Frames. parseObdLines reads replies with headers on: 11-bit CAN (7E8 ...), 29-bit CAN, and the three header bytes plus checksum of ISO 9141-2, ISO 14230-4 KWP and SAE J1850. Replies from several ECUs are separated by their id. It reassembles ISO 15765-2 single, first and consecutive frames per ECU and checks the sequence numbers, including the wrap from F to 0. With headers off, it reads the ELM's own multi-frame layout. Replies are parsed with spaces on or off. Typed PID values. decodePid returns a NumericValue (a value and an ObdUnit), a MonitorStatusValue, an ObdStandardValue, a SupportedPidsValue, a DtcValue, or a RawValue for PIDs the package does not decode. The decoded list includes PID 01 (MIL, code count and readiness monitors), engine load, coolant temperature, fuel trims, RPM, speed and many more; the README has the full list. Trouble codes. Dtc decodes and parses P, C, B and U codes such as P0133, sorts them into generic, manufacturer or reserved using the SAE J2012 ranges, and gives the P0 subsystem group (misfire, fuel and air metering, and so on). Live monitoring. ObdMonitor polls a set of PIDs, each at its own interval, and emits a Stream. Only one request is in flight at a time. The most overdue PID goes next, and ties go round-robin, so every PID gets its turn when the adapter is slower than the rates you asked for. Request errors are emitted as stream errors and polling continues. How to use it Add it from GitHub: dependencies:
obd2_kit:
git: https://github.com/AbdeldjalilChougui/obd2_kit
The package ships a TCP transport for Wi-Fi dongles in package:obd2_kit/io.dart. Most of them listen on 192.168.0.10:35000: import 'package:obd2_kit/io.dart';
import 'package:obd2_kit/obd2_kit.dart';
Future main() async {
final tra...

Powered by Readybot.io