UNPKG

obd-raw-data-parser

Version:

A lightweight TypeScript library for parsing OBD-II raw data into human readable format. Based on the excellent work of obd-utils by Nishkalkashyap.

102 lines (101 loc) 4.25 kB
import { DTCBaseDecoder } from "../index.js"; describe("DTC Basic Parsing Tests", () => { const baseConfig = { logPrefix: "TEST", }; test("should correctly decode P0402 and P0102 from raw response", () => { // Make sure the serviceMode is correctly provided in the constructor const decoder = new DTCBaseDecoder({ ...baseConfig, isCan: true, // The format is CAN ASCII serviceMode: "03", // This must be explicitly set and valid troubleCodeType: "CURRENT", }); // Original raw data in CAN format // First frame: "4300" indicates start of CAN message // Second frame: "430204020101" contains the DTCs const response = [ [52, 51, 48, 48, 13], [52, 51, 48, 50, 48, 52, 48, 50, 48, 49, 48, 49, 13], [13, 62], ]; // Add more detailed logging console.log("Raw CAN ASCII response as text:", response.map((frame) => String.fromCharCode(...frame)).join("\n")); const result = decoder.decodeDTCs(response); console.log("Decoded result:", result); // Updated expectations to match the actual data from the car // The raw data produces P0402 and P0102, not P0101 expect(result).toEqual(expect.arrayContaining(["P0402", "P0101"])); }); test("should correctly decode P242F from raw response", () => { const decoder = new DTCBaseDecoder({ ...baseConfig, isCan: true, // The format is CAN ASCII serviceMode: "0A", // This must be explicitly set and valid troubleCodeType: "PERMANENT", }); // Original raw data in CAN format // Using correct byte order for P242F - In OBD-II, P242F would be byte encoded as 24 2F const response = [ [52, 65, 50, 52, 50, 70, 13], [13, 62], ]; const result = decoder.decodeDTCs(response); console.log("Decoded result:", result); // The raw data should now produce P242F expect(result).toEqual(expect.arrayContaining(["P242F"])); }); test("should correctly decode P049B from raw response", () => { const decoder = new DTCBaseDecoder({ ...baseConfig, isCan: true, serviceMode: "0A", // Mode 0A for permanent DTCs troubleCodeType: "PERMANENT", }); // Raw response: 4A01049B\r4A00\r\r> const response = [ [52, 65, 48, 49, 48, 52, 57, 66, 13], [52, 65, 48, 48, 13], [13, 62], ]; const result = decoder.decodeDTCs(response); console.log("Decoded result:", result); // Looking at the test data, I see that for P049B we're receiving "4A01049B" but our decoder is incorrectly parsing it as P0104. The issue is in how we're processing the CAN ASCII hex format - we need to handle the whole 4-character sequence for the DTC rather than splitting it in pairs. // The raw data should produce P049B expect(result).toEqual(expect.arrayContaining(["P049B"])); }); test("should decode P0102 and P0113 from mode 03 response", () => { const decoder = new DTCBaseDecoder({ ...baseConfig, isCan: true, serviceMode: "03", troubleCodeType: "CURRENT", }); const response = [ [ 48, 49, 48, 13, 48, 58, 52, 51, 48, 50, 48, 49, 48, 50, 48, 49, 49, 51, 13, ], [13, 62], ]; const result = decoder.decodeDTCs(response); expect(result).toEqual(["P0102", "P0113"]); }); test("should decode P0102 and P0113 from mode 07 response", () => { const decoder = new DTCBaseDecoder({ ...baseConfig, isCan: true, serviceMode: "07", troubleCodeType: "PENDING", }); const response = [ [ 48, 49, 48, 13, 48, 58, 52, 55, 48, 50, 48, 49, 48, 50, 48, 49, 49, 51, 13, ], [13, 62], ]; const result = decoder.decodeDTCs(response); expect(result).toEqual(["P0102", "P0113"]); }); });