Making Sense of CAN-Bus: Vehicle Data in Its Raw Form

In February 2019, one of our logistics clients called at eight in the morning: "Your system is draining my vehicles' tanks at night." We looked at the dashboard; on one of the man's tractor units, the fuel level dropped from 68 percent to 12 percent at 2:10 a.m. and climbed back up to 65 percent on its own at 6:40 a.m. The fuel theft alarm had gone off four times in three nights, and the client was talking about putting a night guard on the parking lot. We watched the camera footage: no one had even come near the vehicle all night.

There was no thief. But the data wasn't lying, either. What was lying was the way we read that data.

The Crowd Talking Inside the Vehicle

When I first started seriously poking around in CAN-Bus, what surprised me most was this: on the in-vehicle network, nobody asks anybody a "question." The engine ECU, the ABS, the transmission, the instrument cluster all keep shouting the values they know onto the line, nonstop. Hundreds of frames per second, each carrying at most 8 bytes of data, with no recipient address on them, only an identifier (ID). We encounter 11-bit standard IDs in passenger cars and 29-bit extended IDs in heavy vehicles. Everyone listening hears everything; they take what's useful to them and throw the rest away.

Our telematics device is a silent listener in that crowd too. The problem is that it has to know what the things it hears mean. It has to learn from somewhere that the second byte of a frame with ID 0x18FEFC17 is the fuel level, and that the value should be multiplied by 0.4 and read as a percentage. If the manufacturer gave you a DBC file (a map of the signals), you're lucky. If not (and in the mixed fleets we worked with in Turkey, they mostly didn't), you're left with a pile of bytes and plenty of patience.

The Polite but Narrow Door of OBD-II

Plenty of people told us, "Just read it from the OBD socket, it's standard." They're partly right. OBD-II really is a standard: you send a request to ID 0x7DF, and the ECU responds from 0x7E8. PID 0x0C is engine RPM, 0x0D is vehicle speed, 0x05 is coolant temperature. Even the formulas are known; for temperature you subtract 40 from the byte in the response, and for RPM you calculate ((A*256)+B)/4.

But OBD-II is an emissions diagnostics standard, not a fleet management standard. The odometer isn't in OBD. Fuel level (PID 0x2F) exists in theory, but roughly half of the 2010-2016 model vehicles we tried didn't support that PID. Data that's critical for fleets, such as clutch, retarder, axle load and engine hours, never passes through the OBD door at all. In heavy vehicles, J1939 and the FMS standard that sits on top of it come to the rescue; from a tractor unit with an FMS gateway you can properly get the odometer, total fuel consumption and pedal position. On a vehicle without FMS, though, you have to go down to raw CAN.

Layers of access to vehicle data: J1939 and FMS in heavy vehicles, the standard but narrow OBD-II, and raw CAN when there is no FMS.
OBD-II is an emissions diagnostics standard, not a fleet management standard.

Nights Spent with candump

Signal discovery on raw CAN is archaeology in the truest sense of the word. Our method was this: we take the vehicle out to the yard, have the device write all traffic to a file in debug mode (an embedded version of what candump does on Linux), then change a single thing on the vehicle and repeat the recording. Open the door, record. Close it, record. The bytes that change in the diff of the two dumps are your door signal. Press the throttle 20 percent, then 40 percent; whichever byte grows proportionally is the pedal position.

Byte order, by the way, is a trap of its own. The same 16-bit signal comes in Intel (little-endian) order from one manufacturer and Motorola (big-endian) order from another. On one model where we read the RPM signal in the wrong order, the engine showed 24,000 RPM at idle; fortunately, values that absurd give themselves away immediately. The truly insidious ones are values that look "reasonable" in both orders. We spent a week reading the speed signal backward and showing 35 km/h instead of 90 km/h, and didn't notice until the client called saying "my drivers can't possibly be going that slow."

It sounds simple, but with 700-800 frames per second flowing in a passenger car, diffing two 60-second recordings shows you hundreds of "changed" bytes. There are counters, there are checksum bytes, there are sensors that jitter constantly. Over time we developed an elimination process: first compute the variance of the bytes for each ID, add the ones whose variance spikes during the experiment to a candidate list, then correlate the candidates with the physical event. Finding a vehicle's fuel signal with this method took us half a day on average. (Our fastest record was 40 minutes, the worst three days; on that vehicle the signal turned out to be multiplexed, which I'll get to in a moment.)

Signal discovery steps: recording the traffic, changing one thing, comparing the recordings, eliminating candidates and matching them to the physical event.
Finding the fuel signal took half a day on average.

Then there's the scaling trap. Let's say you've found the signal; the raw value is between 0 and 255. Is that a percentage, liters, or quarter liters? We would fill the tank to the brim and read the value, then drain it to half and read it again. On one model we found that the tank's "full" value was 247, not 255, and that the range above it was never used. We baked the calibration into a table; for that vehicle family, we calculated the percentage against 247. This kind of table knowledge isn't in any document; you only learn it by living it.

The Frame That Drained the Tank at 2:10 a.m.

Now back to the thief from the beginning. On that tractor unit, we had found the fuel signal through reverse engineering months earlier and tied it to a specific byte of a specific ID. When we pulled the night recordings from the device's raw log, the oddity revealed itself: at 2:10 a.m., the first byte of the frames coming from that same ID was changing. It turned out the frame was multiplexed: the first byte is a "page number," and the meaning of the remaining seven bytes changes depending on that page. During the day we always saw page 0, which is where the fuel was. At 2:10 a.m., the independent fuel-fired heater (which the driver had programmed for cold mornings) kicked in and started broadcasting page 2 on the same ID, and since our code didn't distinguish between pages, it took the value of the heater's own small tank for the main tank. When the heater went quiet at 6:40 a.m., the "fuel" came back too.

The first byte of a multiplexed frame is the page number; when the heater's page 2 broadcast at night was mistaken for the main tank, a fake fuel theft alarm was raised.
The fix was two lines: check the mux byte, and skip the frame if it isn't page 0.

The fix was two lines: check the mux byte, and skip the frame if it isn't page 0. The real gain, though, was in the process. From that day on, we added "is there a mux?" as a mandatory field to every signal definition, and we put this rule into the alarm engine: if the fuel level drops by more than 30 percent in under 10 minutes and then rises again on its own, it isn't theft, it's a data error; suppress the alarm and send it to the engineering queue. Our false alarm rate fell from 4.1 percent to 0.6 percent that quarter.

We also ripped the signal definitions out of the code and moved them into the database. We now have a "signal dictionary" table matched by make, model, year and chassis range; the ID, start bit, length, multiplier, offset, byte order and, if any, the mux page all sit in a single row. When a new vehicle type comes along, adding a row to the table is enough, instead of a firmware update. By the end of 2019, this dictionary had accumulated more than 60 vehicle families and over 400 signal definitions; it was probably the company's most valuable asset that never showed up on the balance sheet.

The most lasting lesson this work taught me is this: in telematics, data quality is won or lost not on the device but in the interpretation layer. Same device, same vehicle, same bus; the only difference is whether you read a byte or not. Underneath every number that goes to a client are dozens of small decisions like these, and none of them are written in the brochures. If you think the data coming from your fleet is behaving strangely (fuel that evaporates at night, an odometer that runs backward, an engine that drops to minus 40 degrees), write to us; chances are we've seen something similar.

In the next post I'll talk about another ordeal of this work: GPS itself lies too, and the name of the thing that tames it is the Kalman filter.