Simvik

Show a trouble code the way a tester does

Turn a 3-byte UDS trouble code into text such as B1A01-16. An easy embedded C problem (UDS: data identifiers and the fault memory), run and graded in the…

easy · UDS: data identifiers and the fault memory · uds · dtc · automotive · bits · formatting

A UDS trouble code (DTC) is three bytes. The first two are the code a workshop tool displays as a letter and four characters; the third is the failure type.

Bits of the first two bytesMeaning
15-14the letter: 00 P (powertrain), 01 C (chassis), 10 B (body), 11 U (network)
13-12the first digit, 0 to 3
11-8, 7-4, 3-0three more hexadecimal digits

Write dtc_text(dtc, out), where dtc holds the three bytes in its low 24 bits (0x9A0116 is the bytes 9A 01 16). It writes the text to out as B1A01-16: the letter, the four characters, a hyphen and the failure type as two hexadecimal digits, upper case, then a terminating zero. out has room for 9 characters.

Starter code

void dtc_text(uint32_t dtc, char *out) {
  sprintf(out, "P%04lX-%02lX", (unsigned long)(dtc >> 8), (unsigned long)(dtc & 0xFF));
}

Example tests

A body code

show(0x9A0116);

Expected output:

9A0116 -> B1A01-16

A powertrain code

show(0x030100);
show(0x012313);

Expected output:

030100 -> P0301-00
012313 -> P0123-13

Learn it first

More UDS: data identifiers and the fault memory problems