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 bytes | Meaning |
|---|---|
| 15-14 | the letter: 00 P (powertrain), 01 C (chassis), 10 B (body), 11 U (network) |
| 13-12 | the first digit, 0 to 3 |
| 11-8, 7-4, 3-0 | three 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-16A powertrain code
show(0x030100);
show(0x012313);Expected output:
030100 -> P0301-00
012313 -> P0123-13Learn it first
- UDS: data identifiers and the fault memory · Reading and writing DIDs, and the services that report and clear trouble codes.
More UDS: data identifiers and the fault memory problems
- Read several data identifiers in one request · ReadDataByIdentifier with a DID table: skip unknown identifiers, and pick the right negative response.
- Report trouble codes by status mask · Build the responses to ReadDTCInformation's count and list sub-functions.