From ec976e90dfa8b3f65a497f128a40d215990f7aab Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sat, 27 Dec 2025 15:29:07 +0200 Subject: [PATCH 01/21] Refactor and improve 5-baud data reading logic --- src/OBD2_KLine.cpp | 68 +++++++++++++++++++++++++++------------------- src/OBD2_KLine.h | 2 +- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index e583dbe..8feabb6 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -697,44 +697,56 @@ void OBD2_KLine::setProtocol(const String &protocolName) { } // 5 Baud 7O1 (1 start, 7 data, 1 parity, 1 stop) -uint8_t OBD2_KLine::read5baud() { - unsigned long t0 = millis(); - while (digitalRead(_rxPin) == HIGH) { - if (millis() - t0 > 2000) return -1; - } +int OBD2_KLine::read5baud() { + // debugPrintln(F("Waiting for 5-baud init...")); - setSerial(false); // Disable serial to read 5 baud data + // HIGH -> LOW (start bit decrease) + while (digitalRead(_rxPin) == HIGH); + unsigned long tStart = micros(); - uint8_t bits[10]; - uint8_t data = 0; - int ones = 0; - delayMicroseconds(100000); + // Measure the start-bit LOW duration + while (digitalRead(_rxPin) == LOW); + unsigned long lowDuration = micros() - tStart; - for (int i = 0; i < 10; i++) { // bits: 0=start, 1..7=data, 8=parity, 9=stop - bits[i] = digitalRead(_rxPin) ? 1 : 0; + // debugPrint(F("Bit duration: ")); + // debugPrintln(lowDuration / 1000.00); - if (i >= 1 && i <= 7) { - data |= (bits[i] << (i - 1)); // save data bits - if (bits[i]) ones++; - } else if (i == 8) { // parity bit - if (bits[i]) ones++; - } else if (i == 9) { // stop bit - break; - } + if (lowDuration < 150000 || lowDuration > 300000) { + // debugPrintln(F("Not 5-baud, ignored")); + return -1; + } - delayMicroseconds(200000); + debugPrint(F("✅ Received 5 Baud data - ")); + uint8_t bits[10]; + + delay(100); + for (int i = 1; i < 10; i++) { + bits[i] = digitalRead(_rxPin); + delay(200); } - // Parity control (Odd) - if (ones % 2 == 0) { - debugPrintln(F("Parity error!")); - return -2; + debugPrint(F("Bits: ")); + for (int i = 0; i < 10; i++) { + debugPrint(bits[i] ? "1" : "0"); } - debugPrint(F("Received 5 Baud Data: ")); - debugPrintln(String(data, HEX).c_str()); + uint8_t data = 0; + int ones = 0; + for (int i = 1; i <= 7; i++) { + data |= (bits[i] << (i - 1)); + if (bits[i]) ones++; + } + if (bits[8]) ones++; + + debugPrint(F(", DATA: 0x")); + debugPrintHex(data); + + if ((ones & 1) == 0) + debugPrintln(F(", Parity ERROR (odd expected)")); + else + debugPrintln(F(", Parity OK")); + // debugPrintln(); - setSerial(true); // Re-enable serial after reading 5 baud data return data; } diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 49324be..1858d77 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -50,7 +50,7 @@ class OBD2_KLine { uint8_t readData(); bool compareData(const uint8_t *dataArray, uint8_t length); void send5baud(uint8_t data); - uint8_t read5baud(); + int read5baud(); float getPID(uint8_t mode, uint8_t pid); float getLiveData(uint8_t pid); From 1f5f4d161d06d8186b8f3500c81c9dcb6a7b7cdb Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sat, 27 Dec 2025 15:56:02 +0200 Subject: [PATCH 02/21] Add module address parameter to OBD2 init methods --- src/OBD2_KLine.cpp | 14 ++++++++------ src/OBD2_KLine.h | 10 +++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index 8feabb6..75efd74 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -24,17 +24,17 @@ void OBD2_KLine::setSerial(bool enabled) { } } -bool OBD2_KLine::initOBD2() { +bool OBD2_KLine::initOBD2(uint8_t moduleAddress) { if (connectionStatus) return true; debugPrintln(F("Initializing OBD2...")); if (selectedProtocol == "Automatic" || selectedProtocol == "ISO14230_Slow" || selectedProtocol == "ISO9141") { - if (trySlowInit()) return true; + if (trySlowInit(moduleAddress)) return true; } if (selectedProtocol == "Automatic" || selectedProtocol == "ISO14230_Fast") { - if (tryFastInit()) return true; + if (tryFastInit(moduleAddress)) return true; } debugPrintln(F("❌ No Protocol Matched. Initialization Failed.")); @@ -42,12 +42,12 @@ bool OBD2_KLine::initOBD2() { return false; } -bool OBD2_KLine::trySlowInit() { +bool OBD2_KLine::trySlowInit(uint8_t moduleAddress) { debugPrintln(F("🔁 Trying ISO9141 / ISO14230_Slow")); setSerial(false); delay(5500); - send5baud(slowInitByte); + send5baud(moduleAddress); setSerial(true); setInterByteTimeout(30); @@ -80,7 +80,8 @@ bool OBD2_KLine::trySlowInit() { } } -bool OBD2_KLine::tryFastInit() { +bool OBD2_KLine::tryFastInit(uint8_t moduleAddress) { + // 83 F1 11 C1 EF 8F C4 debugPrintln(F("🔁 Trying ISO14230_Fast")); setSerial(false); @@ -91,6 +92,7 @@ bool OBD2_KLine::tryFastInit() { digitalWrite(_txPin, HIGH); delay(25); + initMsg[1] = moduleAddress; setSerial(true); writeRawData(initMsg, sizeof(initMsg), 2); diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 1858d77..b10a20e 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -35,6 +35,7 @@ const uint8_t read_ID = 0x04; // Read Calibration ID const uint8_t read_ID_Num_Length = 0x05; // Read Calibration ID Number Length const uint8_t read_ID_Num = 0x06; // Read Calibration ID Number +const uint8_t defaultInitAddress = 0x33; class OBD2_KLine { public: @@ -42,9 +43,9 @@ class OBD2_KLine { void setDebug(Stream &serial); void setSerial(bool enabled); - bool initOBD2(); - bool trySlowInit(); - bool tryFastInit(); + bool initOBD2(uint8_t moduleAddress = defaultInitAddress); + bool trySlowInit(uint8_t moduleAddress = defaultInitAddress); + bool tryFastInit(uint8_t moduleAddress = defaultInitAddress); void writeData(uint8_t mode, uint8_t pid); void writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t checksumType); uint8_t readData(); @@ -81,8 +82,7 @@ class OBD2_KLine { void setProtocol(const String &protocolName); void updateConnectionStatus(bool messageReceived); - uint8_t initMsg[4] = {0xC1, 0x33, 0xF1, 0x81}; // ISO14230-Fast init message - uint8_t slowInitByte = 0x33; // ISO9141/ISO14230-Slow init byte + uint8_t initMsg[4] = {0xC1, defaultInitAddress, 0xF1, 0x81}; // ISO14230-Fast init message private: SerialType *_serial; From 0f46a1f3bb7e979f8c402169ac72d242ddebc9d2 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Thu, 1 Jan 2026 17:30:06 +0200 Subject: [PATCH 03/21] Add OBD2 K-Line sniffer example --- examples/OBD2 Standard/Sniffer/Sniffer.ino | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 examples/OBD2 Standard/Sniffer/Sniffer.ino diff --git a/examples/OBD2 Standard/Sniffer/Sniffer.ino b/examples/OBD2 Standard/Sniffer/Sniffer.ino new file mode 100644 index 0000000..69271fc --- /dev/null +++ b/examples/OBD2 Standard/Sniffer/Sniffer.ino @@ -0,0 +1,29 @@ +#include "OBD2_KLine.h" // Include the library for OBD2 K-Line communication + +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega2560__) +#include +AltSoftSerial Alt_Serial; +OBD2_KLine KLine(Alt_Serial, 10400, 8, 9); // Uses AltSoftSerial at 10400 baud, with RX on pin 8 and TX on pin 9. +#elif defined(ESP32) +OBD2_KLine KLine(Serial1, 9600, 10, 11); // Uses Hardware Serial (Serial1) at 10400 baud, with RX on pin 10 and TX on pin 11. +#else +#error "Unsupported board! This library currently supports Arduino Uno, Nano, Mega, and ESP32. Please select a compatible board in your IDE." +#endif + +void setup() { + Serial.begin(115200); // Start the default serial (for logging/debugging) + Serial.println("OBD2 K-Line Sniffing Example"); + + KLine.setDebug(Serial); // Optional: outputs debug messages to the selected serial port + KLine.setByteWriteInterval(5); // Optional: delay (ms) between bytes when writing + KLine.setInterByteTimeout(20); // Optional: sets the maximum inter-byte timeout (ms) while receiving data + KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + + Serial.println("OBD2 Starting."); +} + +void loop() { + // int result = KLine.read5baud(); + // if (result < 0) KLine.readData(); + KLine.readData(); +} From 5c65c9829dc2640170a54804691ec50a8e7f2a8d Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sat, 10 Jan 2026 17:30:11 +0200 Subject: [PATCH 04/21] Improve 5-baud detection and parity reporting --- src/OBD2_KLine.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index 75efd74..689d047 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -701,27 +701,32 @@ void OBD2_KLine::setProtocol(const String &protocolName) { // 5 Baud 7O1 (1 start, 7 data, 1 parity, 1 stop) int OBD2_KLine::read5baud() { // debugPrintln(F("Waiting for 5-baud init...")); + setSerial(false); + const unsigned long THRESHOLD = 100000; // HIGH -> LOW (start bit decrease) while (digitalRead(_rxPin) == HIGH); - unsigned long tStart = micros(); - // Measure the start-bit LOW duration - while (digitalRead(_rxPin) == LOW); - unsigned long lowDuration = micros() - tStart; + // debugPrintln(F("Transition detected. Measuring start bit... ")); + unsigned long tStart = micros(); - // debugPrint(F("Bit duration: ")); - // debugPrintln(lowDuration / 1000.00); + while (digitalRead(_rxPin) == LOW) { + if (micros() - tStart > THRESHOLD) { + // debugPrintln(F("✅ LOW > 100ms, 5-baud detected")); + break; + } + } - if (lowDuration < 150000 || lowDuration > 300000) { - // debugPrintln(F("Not 5-baud, ignored")); + if (digitalRead(_rxPin) == HIGH && (micros() - tStart <= THRESHOLD)) { + // debugPrintln(F("❌ No 5 Baud data detected.")); + setSerial(true); return -1; } debugPrint(F("✅ Received 5 Baud data - ")); uint8_t bits[10]; - delay(100); + delay(200); for (int i = 1; i < 10; i++) { bits[i] = digitalRead(_rxPin); delay(200); @@ -744,10 +749,11 @@ int OBD2_KLine::read5baud() { debugPrintHex(data); if ((ones & 1) == 0) - debugPrintln(F(", Parity ERROR (odd expected)")); + debugPrintln(F(", ❌ Parity ERROR (odd expected)")); else - debugPrintln(F(", Parity OK")); + debugPrintln(F(", ✅ Parity OK")); // debugPrintln(); + setSerial(true); return data; } From 47a6daea1e849ca1d38a5659fdeedbf4cd548486 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sat, 10 Jan 2026 17:30:42 +0200 Subject: [PATCH 05/21] Update comment --- src/OBD2_KLine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index 689d047..fe11bb0 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -803,7 +803,7 @@ uint8_t OBD2_KLine::checksum8_Modulo256(const uint8_t *dataArray, int length) { for (int i = 0; i < length; i++) { sum += dataArray[i]; } - return (byte)(sum % 256); // veya (byte)sum; çünkü uint8_t overflow da mod 256 etkisi verir + return (byte)(sum % 256); // or (byte)sum; because uint8_t overflow also gives a mod 256 effect. } uint8_t OBD2_KLine::checksum8_TwosComplement(const uint8_t *dataArray, int length) { From 3a52bab058e954d2a8f27726722cea50bfeab8f6 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sat, 10 Jan 2026 17:31:38 +0200 Subject: [PATCH 06/21] Add example request and response comments to getPID --- src/OBD2_KLine.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index fe11bb0..f836f5b 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -271,6 +271,8 @@ float OBD2_KLine::getFreezeFrame(uint8_t pid) { } float OBD2_KLine::getPID(uint8_t mode, uint8_t pid) { + // example Request: C2 33 F1 01 0C F3 + // example Response: 84 F1 11 41 0C 0D 58 38 writeData(mode, pid); int len = readData(); From ee1dced89ad049b88af178eeeb674c032ce974b9 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:04:12 +0200 Subject: [PATCH 07/21] Add header and address setters to OBD2_KLine --- src/OBD2_KLine.cpp | 27 +++++++++++++++++++++++++++ src/OBD2_KLine.h | 5 +++++ 2 files changed, 32 insertions(+) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index f836f5b..e94cc92 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -895,4 +895,31 @@ void OBD2_KLine::debugPrintHexln(uint8_t val) { debugPrintHex(val); _debugSerial->println(); } +} + +void OBD2_KLine::setInitAddress(uint8_t address) { + defaultInitAddress = address; + // ISO14230 Header'ındaki hedef adresini de otomatik güncellemek isteyebilirsiniz: + header_ISO14230_Fast[1] = address; + debugPrint(F("✅ New Init Address set to: ")); + debugPrintHex(address); + debugPrintln(F("")); +} + +void OBD2_KLine::setISO9141Header(uint8_t h1, uint8_t h2, uint8_t h3) { + header_ISO9141[0] = h1; + header_ISO9141[1] = h2; + header_ISO9141[2] = h3; + debugPrintln(F("✅ ISO9141 Header Updated.")); +} + +void OBD2_KLine::setISO14230Header(uint8_t h1, uint8_t h2, uint8_t h3) { + header_ISO14230_Fast[0] = h1; + header_ISO14230_Fast[1] = h2; + header_ISO14230_Fast[2] = h3; + debugPrintln(F("✅ ISO14230 Header Updated.")); +} + +void OBD2_KLine::setLengthMode(bool inHeader) { + useLengthInHeader = inHeader; } \ No newline at end of file diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index b10a20e..321ac07 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -91,6 +91,11 @@ class OBD2_KLine { uint8_t _txPin; Stream *_debugSerial = nullptr; // Debug serial port + uint8_t defaultInitAddress = 0x33; + uint8_t header_ISO9141[3] = {0x68, 0x6A, 0xF1}; + uint8_t header_ISO14230_Fast[3] = {0xC0, 0x33, 0xF1}; + bool useLengthInHeader = true; + uint8_t resultBuffer[160] = {0}; uint8_t unreceivedDataCount = 0; bool connectionStatus = false; From 6bf6b63fe0d8f49ed05487b19e0834ed9a4d703f Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:15:04 +0200 Subject: [PATCH 08/21] Refactor writeData Finction --- src/OBD2_KLine.cpp | 61 +++++++++++++++++++++++++--------------------- src/OBD2_KLine.h | 8 +++++- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index e94cc92..47adeeb 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -94,7 +94,7 @@ bool OBD2_KLine::tryFastInit(uint8_t moduleAddress) { initMsg[1] = moduleAddress; setSerial(true); - writeRawData(initMsg, sizeof(initMsg), 2); + writeData((uint8_t[]){0x81}); if (!readData()) return false; @@ -157,42 +157,42 @@ void OBD2_KLine::writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t clearEcho(totalLength); } -void OBD2_KLine::writeData(uint8_t mode, uint8_t pid) { - uint8_t message[7] = {0}; - size_t length = (mode == read_FreezeFrame || mode == test_OxygenSensors) ? 7 : - (mode == read_storedDTCs || mode == clear_DTCs || mode == read_pendingDTCs) ? 5 : - 6; +void OBD2_KLine::writeData(const uint8_t* data, uint8_t dataLength) { + uint8_t headerLength = 3; + uint8_t actualLengthByteCount = useLengthInHeader ? 0 : 1; + uint8_t fullDataLength = headerLength + actualLengthByteCount + dataLength + 1; // +1 for checksum + uint8_t message[fullDataLength]; if (connectedProtocol == "ISO9141") { - message[0] = (mode == read_FreezeFrame || mode == test_OxygenSensors) ? 0x69 : 0x68; - message[1] = 0x6A; - } else if (connectedProtocol == "ISO14230_Fast" || connectedProtocol == "ISO14230_Slow") { - message[0] = (mode == read_FreezeFrame || mode == test_OxygenSensors) ? 0xC3 : - (mode == read_storedDTCs || mode == clear_DTCs || mode == read_pendingDTCs) ? 0xC1 : - 0xC2; - message[1] = 0x33; + memcpy(message, header_ISO9141, headerLength); + } else if (connectedProtocol == "ISO14230_Fast" || connectedProtocol == "ISO14230_Slow" || connectionStatus == false) { + memcpy(message, header_ISO14230_Fast, headerLength); + + if (useLengthInHeader) { + message[0] += dataLength; + } else { + message[3] = dataLength; + } } - message[2] = 0xF1; - message[3] = mode; - if (length > 5) message[4] = pid; - if (length == 7) message[5] = 0x00; + uint8_t dataStartOffset = headerLength + actualLengthByteCount; + memcpy(&message[dataStartOffset], data, dataLength); - message[length - 1] = checksum8_Modulo256(message, length - 1); + message[fullDataLength - 1] = checksum8_Modulo256(message, fullDataLength - 1); debugPrint(F("\n➡️ Sending Data: ")); - for (size_t i = 0; i < length; i++) { + for (size_t i = 0; i < fullDataLength; i++) { debugPrintHex(message[i]); debugPrint(F(" ")); } debugPrintln(F("")); - for (size_t i = 0; i < length; i++) { + for (size_t i = 0; i < fullDataLength; i++) { _serial->write(message[i]); delay(_byteWriteInterval); } - clearEcho(length); + clearEcho(fullDataLength); } uint8_t OBD2_KLine::readData() { @@ -273,7 +273,12 @@ float OBD2_KLine::getFreezeFrame(uint8_t pid) { float OBD2_KLine::getPID(uint8_t mode, uint8_t pid) { // example Request: C2 33 F1 01 0C F3 // example Response: 84 F1 11 41 0C 0D 58 38 - writeData(mode, pid); + if (mode == read_LiveData) { + writeData((uint8_t[]){mode, pid}); + } else if (mode == read_FreezeFrame) { + writeData((uint8_t[]){mode, pid, 0x00}); + } + int len = readData(); if (len <= 0) return -1; // Data not received @@ -471,7 +476,7 @@ uint8_t OBD2_KLine::readDTCs(uint8_t mode) { return -1; // Invalid mode } - writeData(mode, 0x00); + writeData((uint8_t[]){mode}); int len = readData(); if (len >= 3) { @@ -499,7 +504,7 @@ String OBD2_KLine::getPendingDTC(uint8_t index) { } bool OBD2_KLine::clearDTCs() { - writeData(clear_DTCs, 0x00); + writeData((uint8_t[]){clear_DTCs}); int len = readData(); if (len >= 3) { if (resultBuffer[3] == 0x44) { @@ -529,9 +534,9 @@ String OBD2_KLine::getVehicleInfo(uint8_t pid) { messageCount = 5; } else if (pid == 0x04 || pid == 0x06) { if (pid == 0x04) { - writeData(read_VehicleInfo, read_ID_Length); + writeData((uint8_t[]){read_VehicleInfo, read_ID_Length}); } else if (pid == 0x06) { - writeData(read_VehicleInfo, read_ID_Num_Length); + writeData((uint8_t[]){read_VehicleInfo, read_ID_Num_Length}); } else { return ""; } @@ -543,7 +548,7 @@ String OBD2_KLine::getVehicleInfo(uint8_t pid) { } } - writeData(read_VehicleInfo, pid); + writeData((uint8_t[]){read_VehicleInfo, pid}); if (readData()) { for (int j = 0; j < messageCount; j++) { @@ -626,7 +631,7 @@ uint8_t OBD2_KLine::readSupportedData(uint8_t mode) { // Group 0 is always processed, others must be checked if (n != 0 && !isInArray(targetArray, 32, pidCmds[n])) break; - writeData(mode, pidCmds[n]); + writeData((uint8_t[]){mode, pidCmds[n]}); if (readData() && resultBuffer[3] == 0x40 + mode) { for (int i = 0; i < 4; i++) { uint8_t value = resultBuffer[i + startByte]; diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 321ac07..07ddfd7 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -46,8 +46,14 @@ class OBD2_KLine { bool initOBD2(uint8_t moduleAddress = defaultInitAddress); bool trySlowInit(uint8_t moduleAddress = defaultInitAddress); bool tryFastInit(uint8_t moduleAddress = defaultInitAddress); - void writeData(uint8_t mode, uint8_t pid); void writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t checksumType); + void writeData(const uint8_t* data, uint8_t length); + + template + void writeData(const uint8_t (&dataArray)[N]) { + writeData(dataArray, N); + } + uint8_t readData(); bool compareData(const uint8_t *dataArray, uint8_t length); void send5baud(uint8_t data); From 0659ebc6a4070ee965f4d4198b710f9e5c5a0f84 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:16:16 +0200 Subject: [PATCH 09/21] Add the forgotten headers and address setters to OBD2_KLine --- src/OBD2_KLine.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 07ddfd7..20a9fe4 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -89,6 +89,10 @@ class OBD2_KLine { void updateConnectionStatus(bool messageReceived); uint8_t initMsg[4] = {0xC1, defaultInitAddress, 0xF1, 0x81}; // ISO14230-Fast init message + void setInitAddress(uint8_t address); + void setISO9141Header(uint8_t h1, uint8_t h2, uint8_t h3); + void setISO14230Header(uint8_t h1, uint8_t h2, uint8_t h3); + void setLengthMode(bool inHeader); private: SerialType *_serial; From e15156b5bd09c9792da54f2acf026a8b104a47e7 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:19:53 +0200 Subject: [PATCH 10/21] Refactor OBD2 initialization --- src/OBD2_KLine.cpp | 13 ++++++------- src/OBD2_KLine.h | 5 ----- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index 47adeeb..a8b4e3d 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -24,17 +24,17 @@ void OBD2_KLine::setSerial(bool enabled) { } } -bool OBD2_KLine::initOBD2(uint8_t moduleAddress) { +bool OBD2_KLine::initOBD2() { if (connectionStatus) return true; debugPrintln(F("Initializing OBD2...")); if (selectedProtocol == "Automatic" || selectedProtocol == "ISO14230_Slow" || selectedProtocol == "ISO9141") { - if (trySlowInit(moduleAddress)) return true; + if (trySlowInit()) return true; } if (selectedProtocol == "Automatic" || selectedProtocol == "ISO14230_Fast") { - if (tryFastInit(moduleAddress)) return true; + if (tryFastInit()) return true; } debugPrintln(F("❌ No Protocol Matched. Initialization Failed.")); @@ -42,12 +42,12 @@ bool OBD2_KLine::initOBD2(uint8_t moduleAddress) { return false; } -bool OBD2_KLine::trySlowInit(uint8_t moduleAddress) { +bool OBD2_KLine::trySlowInit() { debugPrintln(F("🔁 Trying ISO9141 / ISO14230_Slow")); setSerial(false); delay(5500); - send5baud(moduleAddress); + send5baud(defaultInitAddress); setSerial(true); setInterByteTimeout(30); @@ -80,8 +80,8 @@ bool OBD2_KLine::trySlowInit(uint8_t moduleAddress) { } } -bool OBD2_KLine::tryFastInit(uint8_t moduleAddress) { // 83 F1 11 C1 EF 8F C4 +bool OBD2_KLine::tryFastInit() { debugPrintln(F("🔁 Trying ISO14230_Fast")); setSerial(false); @@ -92,7 +92,6 @@ bool OBD2_KLine::tryFastInit(uint8_t moduleAddress) { digitalWrite(_txPin, HIGH); delay(25); - initMsg[1] = moduleAddress; setSerial(true); writeData((uint8_t[]){0x81}); diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 20a9fe4..a8ecaec 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -35,17 +35,12 @@ const uint8_t read_ID = 0x04; // Read Calibration ID const uint8_t read_ID_Num_Length = 0x05; // Read Calibration ID Number Length const uint8_t read_ID_Num = 0x06; // Read Calibration ID Number -const uint8_t defaultInitAddress = 0x33; - class OBD2_KLine { public: OBD2_KLine(SerialType &serialStream, uint32_t baudRate, uint8_t rxPin, uint8_t txPin); void setDebug(Stream &serial); void setSerial(bool enabled); - bool initOBD2(uint8_t moduleAddress = defaultInitAddress); - bool trySlowInit(uint8_t moduleAddress = defaultInitAddress); - bool tryFastInit(uint8_t moduleAddress = defaultInitAddress); void writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t checksumType); void writeData(const uint8_t* data, uint8_t length); From 4e69918544a4de9974a04f52ecae1438e6003594 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:23:09 +0200 Subject: [PATCH 11/21] Improve echo clearing and delay handling --- src/OBD2_KLine.cpp | 48 ++++++++++++++++++++++++++++++++-------------- src/OBD2_KLine.h | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index a8b4e3d..93614e2 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -150,7 +150,7 @@ void OBD2_KLine::writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t for (size_t i = 0; i < totalLength; i++) { _serial->write(sendData[i]); - delay(_byteWriteInterval); + if (i < totalLength - 1) delay(_byteWriteInterval); } clearEcho(totalLength); @@ -188,7 +188,7 @@ void OBD2_KLine::writeData(const uint8_t* data, uint8_t dataLength) { for (size_t i = 0; i < fullDataLength; i++) { _serial->write(message[i]); - delay(_byteWriteInterval); + if (i < fullDataLength - 1) delay(_byteWriteInterval); } clearEcho(fullDataLength); @@ -234,20 +234,40 @@ uint8_t OBD2_KLine::readData() { return 0; } -void OBD2_KLine::clearEcho(int length) { - int result = _serial->available(); - if (result > 0) { - debugPrint(F("🗑️ Cleared Echo Data: ")); - for (int i = 0; i < length; i++) { - uint8_t readedByte = _serial->read(); - debugPrintHex(readedByte); - debugPrint(F(" ")); +void OBD2_KLine::clearEcho(uint8_t length) { + const unsigned long byteTimeoutMs = 100; + + // Wait for the first byte + unsigned long startTime = millis(); + while (_serial->available() == 0) { + if (millis() - startTime >= byteTimeoutMs) { + debugPrintln(F("❌ Echo not received")); + return; } - debugPrintln(F("")); - // debugPrintln(F("Echo Data Cleared")); - } else { - debugPrintln(F("❌ Not Received Echo Data")); + delayMicroseconds(100); + } + + // First byte received, now read the rest + debugPrint(F("🗑️ Cleared Echo Data: ")); + + uint8_t readedByte; + for (size_t readCount = 0; readCount < length; readCount++) { + startTime = millis(); + + while (_serial->available() == 0) { + if (millis() - startTime >= byteTimeoutMs) { + debugPrintln(F("\n❌ Echo incomplete")); + return; + } + delayMicroseconds(100); + } + + readedByte = _serial->read(); + debugPrintHex(readedByte); + debugPrint(F(" ")); } + + debugPrintln(F("")); } bool OBD2_KLine::compareData(const uint8_t *dataArray, uint8_t length) { diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index a8ecaec..b12d523 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -128,11 +128,11 @@ class OBD2_KLine { bool isInArray(const uint8_t *dataArray, uint8_t length, uint8_t value); String convertBytesToHexString(const uint8_t *dataArray, uint8_t length); String convertHexToAscii(const uint8_t *dataArray, uint8_t length); - void clearEcho(int length); void debugPrint(const char *msg); void debugPrint(const __FlashStringHelper *msg); void debugPrintln(const char *msg); void debugPrintln(const __FlashStringHelper *msg); + void clearEcho(uint8_t length); void debugPrintHex(uint8_t val); // Hexadecimal output void debugPrintHexln(uint8_t val); // Hexadecimal + newline }; From 26e4a8cab1ef4fd66ded40f8d4b19f23b13e8df2 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:23:56 +0200 Subject: [PATCH 12/21] Add forgotten OBD2 initialization methods to OBD2_KLine --- src/OBD2_KLine.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index b12d523..135e777 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -42,6 +42,9 @@ class OBD2_KLine { void setDebug(Stream &serial); void setSerial(bool enabled); void writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t checksumType); + bool initOBD2(); + bool trySlowInit(); + bool tryFastInit(); void writeData(const uint8_t* data, uint8_t length); template From 73c86f3ad46a9fdcaaa9c36894ecb1a19935abba Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:26:49 +0200 Subject: [PATCH 13/21] Refactor pointer and reference types for consistency --- src/OBD2_KLine.cpp | 37 +++++++++++++++++++------------------ src/OBD2_KLine.h | 35 +++++++++++++++++------------------ 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index 93614e2..1ec99c7 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -1,6 +1,6 @@ #include "OBD2_KLine.h" -OBD2_KLine::OBD2_KLine(SerialType &serialPort, uint32_t baudRate, uint8_t rxPin, uint8_t txPin) +OBD2_KLine::OBD2_KLine(SerialType& serialPort, uint32_t baudRate, uint8_t rxPin, uint8_t txPin) : _serial(&serialPort), _rxPin(rxPin), _txPin(txPin), _baudRate(baudRate) { // Start serial setSerial(true); @@ -80,8 +80,9 @@ bool OBD2_KLine::trySlowInit() { } } - // 83 F1 11 C1 EF 8F C4 bool OBD2_KLine::tryFastInit() { + // example Request: C1 33 F1 81 66 + // example Response: 83 F1 11 C1 EF 8F C4 debugPrintln(F("🔁 Trying ISO14230_Fast")); setSerial(false); @@ -110,7 +111,7 @@ bool OBD2_KLine::tryFastInit() { // ----------------------------------- Basic Read/Write functions ----------------------------------- -void OBD2_KLine::writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t checksumType) { +void OBD2_KLine::writeRawData(const uint8_t* dataArray, uint8_t length, uint8_t checksumType) { uint8_t totalLength = length; // default no checksum uint8_t checksum = 0; @@ -270,7 +271,7 @@ void OBD2_KLine::clearEcho(uint8_t length) { debugPrintln(F("")); } -bool OBD2_KLine::compareData(const uint8_t *dataArray, uint8_t length) { +bool OBD2_KLine::compareData(const uint8_t* dataArray, uint8_t length) { for (size_t i = 0; i < length; i++) { if (dataArray[i] != resultBuffer[i]) { return false; @@ -485,7 +486,7 @@ uint8_t OBD2_KLine::readDTCs(uint8_t mode) { // example Response: 87 F1 11 43 01 70 01 34 00 00 72 // example Response: 87 F1 11 43 00 00 CC int dtcCount = 0; - String *targetArray = nullptr; + String* targetArray = nullptr; if (mode == read_storedDTCs) { targetArray = storedDTCBuffer; @@ -620,7 +621,7 @@ uint8_t OBD2_KLine::readSupportedData(uint8_t mode) { int pidIndex = 0; int startByte = 0; int arraySize = 32; // Size of supported data arrays - uint8_t *targetArray = nullptr; + uint8_t* targetArray = nullptr; if (mode == read_LiveData) { // Mode 01 startByte = 5; @@ -717,7 +718,7 @@ void OBD2_KLine::setReadTimeout(uint16_t timeoutMs) { _readTimeout = timeoutMs; } -void OBD2_KLine::setProtocol(const String &protocolName) { +void OBD2_KLine::setProtocol(const String& protocolName) { selectedProtocol = protocolName; connectionStatus = false; // Reset connection status connectedProtocol = ""; // Reset connected protocol @@ -816,7 +817,7 @@ void OBD2_KLine::send5baud(uint8_t data) { debugPrintln(F("")); } -uint8_t OBD2_KLine::checksum8_XOR(const uint8_t *dataArray, int length) { +uint8_t OBD2_KLine::checksum8_XOR(const uint8_t* dataArray, int length) { uint8_t checksum = 0; for (int i = 0; i < length; i++) { checksum ^= dataArray[i]; // XOR operation @@ -824,7 +825,7 @@ uint8_t OBD2_KLine::checksum8_XOR(const uint8_t *dataArray, int length) { return checksum; } -uint8_t OBD2_KLine::checksum8_Modulo256(const uint8_t *dataArray, int length) { +uint8_t OBD2_KLine::checksum8_Modulo256(const uint8_t* dataArray, int length) { unsigned int sum = 0; for (int i = 0; i < length; i++) { sum += dataArray[i]; @@ -832,7 +833,7 @@ uint8_t OBD2_KLine::checksum8_Modulo256(const uint8_t *dataArray, int length) { return (byte)(sum % 256); // or (byte)sum; because uint8_t overflow also gives a mod 256 effect. } -uint8_t OBD2_KLine::checksum8_TwosComplement(const uint8_t *dataArray, int length) { +uint8_t OBD2_KLine::checksum8_TwosComplement(const uint8_t* dataArray, int length) { unsigned int sum = 0; for (int i = 0; i < length; i++) { sum += dataArray[i]; @@ -855,7 +856,7 @@ String OBD2_KLine::decodeDTC(uint8_t input_byte1, uint8_t input_byte2) { return ErrorCode; } -bool OBD2_KLine::isInArray(const uint8_t *dataArray, uint8_t length, uint8_t value) { +bool OBD2_KLine::isInArray(const uint8_t* dataArray, uint8_t length, uint8_t value) { for (int i = 0; i < length; i++) { if (dataArray[i] == value) { return true; @@ -864,7 +865,7 @@ bool OBD2_KLine::isInArray(const uint8_t *dataArray, uint8_t length, uint8_t val return false; } -String OBD2_KLine::convertHexToAscii(const uint8_t *dataArray, uint8_t length) { +String OBD2_KLine::convertHexToAscii(const uint8_t* dataArray, uint8_t length) { String asciiString = ""; for (int i = 0; i < length; i++) { uint8_t b = dataArray[i]; @@ -875,7 +876,7 @@ String OBD2_KLine::convertHexToAscii(const uint8_t *dataArray, uint8_t length) { return asciiString; } -String OBD2_KLine::convertBytesToHexString(const uint8_t *dataArray, uint8_t length) { +String OBD2_KLine::convertBytesToHexString(const uint8_t* dataArray, uint8_t length) { String hexString = ""; for (int i = 0; i < length; i++) { if (dataArray[i] < 0x10) hexString += "0"; // Pad leading zero @@ -887,23 +888,23 @@ String OBD2_KLine::convertBytesToHexString(const uint8_t *dataArray, uint8_t len // ----------------------------------- Debug Functions ----------------------------------- -void OBD2_KLine::setDebug(Stream &serial) { +void OBD2_KLine::setDebug(Stream& serial) { _debugSerial = &serial; } -void OBD2_KLine::debugPrint(const char *msg) { +void OBD2_KLine::debugPrint(const char* msg) { if (_debugSerial) _debugSerial->print(msg); } -void OBD2_KLine::debugPrint(const __FlashStringHelper *msg) { +void OBD2_KLine::debugPrint(const __FlashStringHelper* msg) { if (_debugSerial) _debugSerial->print(msg); } -void OBD2_KLine::debugPrintln(const char *msg) { +void OBD2_KLine::debugPrintln(const char* msg) { if (_debugSerial) _debugSerial->println(msg); } -void OBD2_KLine::debugPrintln(const __FlashStringHelper *msg) { +void OBD2_KLine::debugPrintln(const __FlashStringHelper* msg) { if (_debugSerial) _debugSerial->println(msg); } diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 135e777..e439136 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -37,14 +37,14 @@ const uint8_t read_ID_Num = 0x06; // Read Calibration ID Number class OBD2_KLine { public: - OBD2_KLine(SerialType &serialStream, uint32_t baudRate, uint8_t rxPin, uint8_t txPin); + OBD2_KLine(SerialType& serialStream, uint32_t baudRate, uint8_t rxPin, uint8_t txPin); - void setDebug(Stream &serial); + void setDebug(Stream& serial); void setSerial(bool enabled); - void writeRawData(const uint8_t *dataArray, uint8_t length, uint8_t checksumType); bool initOBD2(); bool trySlowInit(); bool tryFastInit(); + void writeRawData(const uint8_t* dataArray, uint8_t length, uint8_t checksumType); void writeData(const uint8_t* data, uint8_t length); template @@ -53,7 +53,7 @@ class OBD2_KLine { } uint8_t readData(); - bool compareData(const uint8_t *dataArray, uint8_t length); + bool compareData(const uint8_t* dataArray, uint8_t length); void send5baud(uint8_t data); int read5baud(); @@ -83,21 +83,20 @@ class OBD2_KLine { void setByteWriteInterval(uint16_t interval); void setInterByteTimeout(uint16_t interval); void setReadTimeout(uint16_t timeoutMs); - void setProtocol(const String &protocolName); + void setProtocol(const String& protocolName); void updateConnectionStatus(bool messageReceived); - uint8_t initMsg[4] = {0xC1, defaultInitAddress, 0xF1, 0x81}; // ISO14230-Fast init message void setInitAddress(uint8_t address); void setISO9141Header(uint8_t h1, uint8_t h2, uint8_t h3); void setISO14230Header(uint8_t h1, uint8_t h2, uint8_t h3); void setLengthMode(bool inHeader); private: - SerialType *_serial; + SerialType* _serial; uint32_t _baudRate; uint8_t _rxPin; uint8_t _txPin; - Stream *_debugSerial = nullptr; // Debug serial port + Stream* _debugSerial = nullptr; // Debug serial port uint8_t defaultInitAddress = 0x33; uint8_t header_ISO9141[3] = {0x68, 0x6A, 0xF1}; @@ -123,19 +122,19 @@ class OBD2_KLine { uint8_t supportedControlComponents[32]; uint8_t supportedVehicleInfo[32]; - uint8_t checksum8_XOR(const uint8_t *dataArray, int length); - uint8_t checksum8_Modulo256(const uint8_t *dataArray, int length); - uint8_t checksum8_TwosComplement(const uint8_t *dataArray, int length); + uint8_t checksum8_XOR(const uint8_t* dataArray, int length); + uint8_t checksum8_Modulo256(const uint8_t* dataArray, int length); + uint8_t checksum8_TwosComplement(const uint8_t* dataArray, int length); String decodeDTC(uint8_t input_byte1, uint8_t input_byte2); - bool isInArray(const uint8_t *dataArray, uint8_t length, uint8_t value); - String convertBytesToHexString(const uint8_t *dataArray, uint8_t length); - String convertHexToAscii(const uint8_t *dataArray, uint8_t length); - void debugPrint(const char *msg); - void debugPrint(const __FlashStringHelper *msg); - void debugPrintln(const char *msg); - void debugPrintln(const __FlashStringHelper *msg); + bool isInArray(const uint8_t* dataArray, uint8_t length, uint8_t value); + String convertBytesToHexString(const uint8_t* dataArray, uint8_t length); + String convertHexToAscii(const uint8_t* dataArray, uint8_t length); void clearEcho(uint8_t length); + void debugPrint(const char* msg); + void debugPrint(const __FlashStringHelper* msg); + void debugPrintln(const char* msg); + void debugPrintln(const __FlashStringHelper* msg); void debugPrintHex(uint8_t val); // Hexadecimal output void debugPrintHexln(uint8_t val); // Hexadecimal + newline }; From 9760215dc28ac34092a21c7f89b960dd8cd69142 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:29:59 +0200 Subject: [PATCH 14/21] Refine debug outputs --- src/OBD2_KLine.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index 1ec99c7..cde1ec6 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -196,7 +196,7 @@ void OBD2_KLine::writeData(const uint8_t* data, uint8_t dataLength) { } uint8_t OBD2_KLine::readData() { - debugPrintln(F("Reading...")); + debugPrint(F("Reading Data ... ")); unsigned long startMillis = millis(); int bytesRead = 0; @@ -224,7 +224,8 @@ uint8_t OBD2_KLine::readData() { } } - debugPrintln(F("\n✅ Data reception completed.")); + debugPrintln(F("")); + // debugPrintln(F("\n✅ Data reception completed.")); return bytesRead; } } @@ -722,7 +723,8 @@ void OBD2_KLine::setProtocol(const String& protocolName) { selectedProtocol = protocolName; connectionStatus = false; // Reset connection status connectedProtocol = ""; // Reset connected protocol - debugPrintln(("Protocol set to: " + selectedProtocol).c_str()); + debugPrint(F("Protocol set to: ")); + debugPrintln((selectedProtocol).c_str()); } // 5 Baud 7O1 (1 start, 7 data, 1 parity, 1 stop) From 314f6a2379d1beacd1cac2931d62a187b8df2b9e Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 03:30:34 +0200 Subject: [PATCH 15/21] Add setConnectionStatus function --- src/OBD2_KLine.cpp | 4 ++++ src/OBD2_KLine.h | 1 + 2 files changed, 5 insertions(+) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index cde1ec6..d32547e 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -727,6 +727,10 @@ void OBD2_KLine::setProtocol(const String& protocolName) { debugPrintln((selectedProtocol).c_str()); } +void OBD2_KLine::setConnectionStatus(bool status) { + connectionStatus = status; +} + // 5 Baud 7O1 (1 start, 7 data, 1 parity, 1 stop) int OBD2_KLine::read5baud() { // debugPrintln(F("Waiting for 5-baud init...")); diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index e439136..3c1ec66 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -85,6 +85,7 @@ class OBD2_KLine { void setReadTimeout(uint16_t timeoutMs); void setProtocol(const String& protocolName); void updateConnectionStatus(bool messageReceived); + void setConnectionStatus(bool status); void setInitAddress(uint8_t address); void setISO9141Header(uint8_t h1, uint8_t h2, uint8_t h3); From 23df635d60e0a4ac41976e0dbb6059b8b0d4c4eb Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Sun, 25 Jan 2026 04:08:33 +0200 Subject: [PATCH 16/21] Add advanced KLine configuration to example sketches --- examples/Car Specific/Honda/Honda.ino | 5 +++++ examples/Car Specific/Opel/Opel.ino | 15 ++++++++++----- examples/OBD2 Standard/ClearDTC/ClearDTC.ino | 7 ++++++- .../GetFreezeFrame/GetFreezeFrame.ino | 5 +++++ .../OBD2 Standard/GetLiveData/GetLiveData.ino | 5 +++++ .../GetSupportedPIDs/GetSupportedPIDs.ino | 5 +++++ .../GetVehicleInfo/GetVehicleInfo.ino | 5 +++++ examples/OBD2 Standard/ReadDTC/ReadDTC.ino | 5 +++++ examples/OBD2 Standard/Sniffer/Sniffer.ino | 5 +++++ 9 files changed, 51 insertions(+), 6 deletions(-) diff --git a/examples/Car Specific/Honda/Honda.ino b/examples/Car Specific/Honda/Honda.ino index 591c492..0c49407 100644 --- a/examples/Car Specific/Honda/Honda.ino +++ b/examples/Car Specific/Honda/Honda.ino @@ -20,6 +20,11 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("Honda Code."); } diff --git a/examples/Car Specific/Opel/Opel.ino b/examples/Car Specific/Opel/Opel.ino index ccecab1..84f6fe9 100644 --- a/examples/Car Specific/Opel/Opel.ino +++ b/examples/Car Specific/Opel/Opel.ino @@ -17,11 +17,16 @@ int kw81_Stage = 0; void setup() { Serial.begin(115200); // Start the default serial (for logging/debugging) - KLine.setDebug(Serial); // Optional: outputs debug messages to the selected serial port - //KLine.setProtocol("ISO14230_Fast"); // Optional: communication protocol (default: Automatic; supported: ISO9141, ISO14230_Slow, ISO14230_Fast, Automatic) - KLine.setByteWriteInterval(5); // Optional: delay (ms) between bytes when writing - KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data - KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setDebug(Serial); // Optional: outputs debug messages to the selected serial port + KLine.setProtocol("ISO14230_Fast"); // Optional: communication protocol (default: Automatic; supported: ISO9141, ISO14230_Slow, ISO14230_Fast, Automatic) + KLine.setByteWriteInterval(5); // Optional: delay (ms) between bytes when writing + KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data + KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. Serial.println("Opel Code."); } diff --git a/examples/OBD2 Standard/ClearDTC/ClearDTC.ino b/examples/OBD2 Standard/ClearDTC/ClearDTC.ino index 939dd78..931e895 100644 --- a/examples/OBD2 Standard/ClearDTC/ClearDTC.ino +++ b/examples/OBD2 Standard/ClearDTC/ClearDTC.ino @@ -20,13 +20,18 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } void loop() { // Attempt to initialize OBD2 communication if (KLine.initOBD2()) { - KLine.clearDTCs(); // Clear Diagnostic Trouble Codes (DTCs) + KLine.clearDTCs(); // Clear Diagnostic Trouble Codes (DTCs) delay(1000); } } diff --git a/examples/OBD2 Standard/GetFreezeFrame/GetFreezeFrame.ino b/examples/OBD2 Standard/GetFreezeFrame/GetFreezeFrame.ino index b390cc9..823f673 100644 --- a/examples/OBD2 Standard/GetFreezeFrame/GetFreezeFrame.ino +++ b/examples/OBD2 Standard/GetFreezeFrame/GetFreezeFrame.ino @@ -20,6 +20,11 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } diff --git a/examples/OBD2 Standard/GetLiveData/GetLiveData.ino b/examples/OBD2 Standard/GetLiveData/GetLiveData.ino index 86974b7..e6737e3 100644 --- a/examples/OBD2 Standard/GetLiveData/GetLiveData.ino +++ b/examples/OBD2 Standard/GetLiveData/GetLiveData.ino @@ -20,6 +20,11 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } diff --git a/examples/OBD2 Standard/GetSupportedPIDs/GetSupportedPIDs.ino b/examples/OBD2 Standard/GetSupportedPIDs/GetSupportedPIDs.ino index e8176a5..663673e 100644 --- a/examples/OBD2 Standard/GetSupportedPIDs/GetSupportedPIDs.ino +++ b/examples/OBD2 Standard/GetSupportedPIDs/GetSupportedPIDs.ino @@ -20,6 +20,11 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } diff --git a/examples/OBD2 Standard/GetVehicleInfo/GetVehicleInfo.ino b/examples/OBD2 Standard/GetVehicleInfo/GetVehicleInfo.ino index 7b46665..82194de 100644 --- a/examples/OBD2 Standard/GetVehicleInfo/GetVehicleInfo.ino +++ b/examples/OBD2 Standard/GetVehicleInfo/GetVehicleInfo.ino @@ -20,6 +20,11 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } diff --git a/examples/OBD2 Standard/ReadDTC/ReadDTC.ino b/examples/OBD2 Standard/ReadDTC/ReadDTC.ino index 09a5eef..9a8df41 100644 --- a/examples/OBD2 Standard/ReadDTC/ReadDTC.ino +++ b/examples/OBD2 Standard/ReadDTC/ReadDTC.ino @@ -20,6 +20,11 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } diff --git a/examples/OBD2 Standard/Sniffer/Sniffer.ino b/examples/OBD2 Standard/Sniffer/Sniffer.ino index 69271fc..e51f8c8 100644 --- a/examples/OBD2 Standard/Sniffer/Sniffer.ino +++ b/examples/OBD2 Standard/Sniffer/Sniffer.ino @@ -19,6 +19,11 @@ void setup() { KLine.setInterByteTimeout(20); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request + KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + Serial.println("OBD2 Starting."); } From 0cb1ad759f83c2cd0fe750cdc0c18b67e7b87db2 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Mon, 26 Jan 2026 00:35:03 +0200 Subject: [PATCH 17/21] Small changes in send5baud Function --- src/OBD2_KLine.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/OBD2_KLine.cpp b/src/OBD2_KLine.cpp index d32547e..3303735 100644 --- a/src/OBD2_KLine.cpp +++ b/src/OBD2_KLine.cpp @@ -793,19 +793,21 @@ int OBD2_KLine::read5baud() { // 5 Baud 7O1 (1 start, 7 data, 1 parity, 1 stop) void OBD2_KLine::send5baud(uint8_t data) { - uint8_t even = 1; // for calculating parity bit uint8_t bits[10]; - bits[0] = 0; // start bit bits[9] = 1; // stop bit - // 7-bit data and parity calculation - for (int i = 1; i <= 7; i++) { - bits[i] = (data >> (i - 1)) & 1; - even ^= bits[i]; + // 7-bit data + for (int i = 0; i < 7; i++) { + bits[i + 1] = (data >> i) & 1; } - bits[8] = (even == 0) ? 1 : 0; // parity bit + // Odd parity calculation + uint8_t ones = 0; + for (int i = 1; i <= 7; i++) { + if (bits[i]) ones++; + } + bits[8] = (ones % 2 == 0) ? 1 : 0; // parity bit debugPrint(F("➡️ 5 Baud Init for Module 0x")); debugPrintHex(data); From 4e3a5dbb2ac897e121b42962ffde21a345d4f335 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Mon, 26 Jan 2026 00:55:55 +0200 Subject: [PATCH 18/21] Added more templates --- src/OBD2_KLine.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/OBD2_KLine.h b/src/OBD2_KLine.h index 3c1ec66..d5b4d74 100644 --- a/src/OBD2_KLine.h +++ b/src/OBD2_KLine.h @@ -52,6 +52,16 @@ class OBD2_KLine { writeData(dataArray, N); } + template + void writeRawData(const uint8_t (&dataArray)[N], uint8_t checksumType) { + writeRawData(dataArray, N, checksumType); + } + + template + bool compareData(const uint8_t (&dataArray)[N]) { + return compareData(dataArray, N); + } + uint8_t readData(); bool compareData(const uint8_t* dataArray, uint8_t length); void send5baud(uint8_t data); From 188cf77117796df77052a1b9bc52f9b1d620d1f9 Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Mon, 26 Jan 2026 00:57:12 +0200 Subject: [PATCH 19/21] Update Opel Codes --- examples/Car Specific/Opel/Opel.ino | 50 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/Car Specific/Opel/Opel.ino b/examples/Car Specific/Opel/Opel.ino index 84f6fe9..bff7ebb 100644 --- a/examples/Car Specific/Opel/Opel.ino +++ b/examples/Car Specific/Opel/Opel.ino @@ -32,23 +32,23 @@ void setup() { } void loop() { - //Opel_Vectra_Test1(); + //KLine.read5baud(); + Opel_Vectra_Test1(); //Opel_Vectra_Test2(); - Opel_Vectra_Simulator1(); + //Opel_Vectra_Simulator1(); //Engine and Immobilizer //Opel_Vectra_Simulator2(); //Instrument Cluster Simulator (Select 4800 Baud) } void Opel_Vectra_Test1() { if (KLine.initOBD2()) { - KLine.writeRawData(imoKeepalive, sizeof(imoKeepalive), 2), KLine.readData(); - KLine.writeRawData(imoReadLiveData, sizeof(imoReadLiveData), 2), KLine.readData(); + KLine.writeRawData(engineReadLiveData1, 2), KLine.readData(); + KLine.writeRawData(engineReadLiveData2, 2), KLine.readData(); + KLine.writeRawData(engineReadLiveData3, 2), KLine.readData(); - KLine.writeRawData(imoKeepalive, sizeof(imoKeepalive), 2), KLine.readData(); - KLine.writeRawData(imoReadDTCs, sizeof(imoReadDTCs), 2), KLine.readData(); - - KLine.writeRawData(imoKeepalive, sizeof(imoKeepalive), 2), KLine.readData(); - KLine.writeRawData(imoClearDTCs, sizeof(imoClearDTCs), 2), KLine.readData(); + // KLine.writeRawData(imoReadLiveData, 2), KLine.readData(); + // KLine.writeRawData(imoReadDTCs, 2), KLine.readData(); + // KLine.writeRawData(imoClearDTCs, 2), KLine.readData(); } } @@ -61,17 +61,17 @@ void Opel_Vectra_Test2() { void Opel_Vectra_Simulator1() { if (KLine.readData()) { - if (KLine.compareData(engineInit0, sizeof(engineInit0)) || KLine.compareData(engineInit, sizeof(engineInit))) KLine.writeRawData(engineInit_Response, sizeof(engineInit_Response), 2); - else if (KLine.compareData(engineCheckConnection, sizeof(engineCheckConnection))) KLine.writeRawData(engineCheckConnection_Response, sizeof(engineCheckConnection_Response), 2); - else if (KLine.compareData(engineReadLiveData1, sizeof(engineReadLiveData1))) KLine.writeRawData(engineReadLiveData1_Response, sizeof(engineReadLiveData1_Response), 2); - else if (KLine.compareData(engineReadLiveData2, sizeof(engineReadLiveData2))) KLine.writeRawData(engineReadLiveData2_Response, sizeof(engineReadLiveData2_Response), 2); - else if (KLine.compareData(engineReadLiveData3, sizeof(engineReadLiveData3))) KLine.writeRawData(engineReadLiveData3_Response, sizeof(engineReadLiveData3_Response), 2); - - if (KLine.compareData(imoInit0, sizeof(imoInit0))) KLine.writeRawData(imoInit_Response, sizeof(imoInit_Response), 2); - else if (KLine.compareData(imoKeepalive, sizeof(imoKeepalive))) KLine.writeRawData(imoKeepalive_Response, sizeof(imoKeepalive_Response), 2); - else if (KLine.compareData(imoReadLiveData, sizeof(imoReadLiveData))) KLine.writeRawData(imoReadLiveData_Response, sizeof(imoReadLiveData_Response), 2); - else if (KLine.compareData(imoReadDTCs, sizeof(imoReadDTCs))) KLine.writeRawData(imoReadDTCs_Response, sizeof(imoReadDTCs_Response), 2); - else if (KLine.compareData(imoClearDTCs, sizeof(imoClearDTCs))) KLine.writeRawData(imoClearDTCs_Response2, sizeof(imoClearDTCs_Response2), 2); + if (KLine.compareData(engineInit0) || KLine.compareData(engineInit)) KLine.writeRawData(engineInit_Response, 2); + else if (KLine.compareData(engineCheckConnection)) KLine.writeRawData(engineCheckConnection_Response, 2); + else if (KLine.compareData(engineReadLiveData1)) KLine.writeRawData(engineReadLiveData1_Response, 2); + else if (KLine.compareData(engineReadLiveData2)) KLine.writeRawData(engineReadLiveData2_Response, 2); + else if (KLine.compareData(engineReadLiveData3)) KLine.writeRawData(engineReadLiveData3_Response, 2); + + if (KLine.compareData(imoInit0)) KLine.writeRawData(imoInit_Response, 2); + else if (KLine.compareData(imoKeepalive)) KLine.writeRawData(imoKeepalive_Response, 2); + else if (KLine.compareData(imoReadLiveData)) KLine.writeRawData(imoReadLiveData_Response, 2); + else if (KLine.compareData(imoReadDTCs)) KLine.writeRawData(imoReadDTCs_Response, 2); + else if (KLine.compareData(imoClearDTCs)) KLine.writeRawData(imoClearDTCs_Response2, 2); } } @@ -79,17 +79,17 @@ void Opel_Vectra_Simulator2() { if (connectionStatus == false) { if (KLine.read5baud() == 0x60) { connectionStatus = true; - KLine.writeRawData(instumentClusterInit_Response, sizeof(instumentClusterInit_Response), 0); + KLine.writeRawData(instumentClusterInit_Response, 0); } } if (connectionStatus == true) { - if (kw81_Stage == 0) KLine.writeRawData(instumentClusterECUID_Response, sizeof(instumentClusterECUID_Response), 0); - else if (kw81_Stage == 1) KLine.writeRawData(instumentClusterLiveData_Response, sizeof(instumentClusterLiveData_Response), 0); + if (kw81_Stage == 0) KLine.writeRawData(instumentClusterECUID_Response, 0); + else if (kw81_Stage == 1) KLine.writeRawData(instumentClusterLiveData_Response, 0); else if (kw81_Stage == 2) {} if (KLine.readData()) { - if (KLine.compareData(instumentClusterLiveData, sizeof(instumentClusterLiveData))) kw81_Stage = 1; - else if (KLine.compareData(instumentClusterClearDTC, sizeof(instumentClusterClearDTC))) kw81_Stage = 2; + if (KLine.compareData(instumentClusterLiveData)) kw81_Stage = 1; + else if (KLine.compareData(instumentClusterClearDTC)) kw81_Stage = 2; } } } From 07359ffab9dbf65a6ed45d41f21c15ff3015fead Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Mon, 26 Jan 2026 01:03:41 +0200 Subject: [PATCH 20/21] Update Opel.ino --- examples/Car Specific/Opel/Opel.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/Car Specific/Opel/Opel.ino b/examples/Car Specific/Opel/Opel.ino index bff7ebb..3afaa80 100644 --- a/examples/Car Specific/Opel/Opel.ino +++ b/examples/Car Specific/Opel/Opel.ino @@ -23,10 +23,10 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request - KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. - KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. - KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. - KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + //KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + //KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + KLine.setISO14230Header(0x80, 0x11, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + //KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. Serial.println("Opel Code."); } From e0a262e066cf3de0942d0fc3e896b669448fe33a Mon Sep 17 00:00:00 2001 From: Muksin Muksin <75759731+muki01@users.noreply.github.com> Date: Mon, 26 Jan 2026 01:03:54 +0200 Subject: [PATCH 21/21] Update Honda.ino --- examples/Car Specific/Honda/Honda.ino | 36 +++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/Car Specific/Honda/Honda.ino b/examples/Car Specific/Honda/Honda.ino index 0c49407..77603b2 100644 --- a/examples/Car Specific/Honda/Honda.ino +++ b/examples/Car Specific/Honda/Honda.ino @@ -20,10 +20,10 @@ void setup() { KLine.setInterByteTimeout(60); // Optional: sets the maximum inter-byte timeout (ms) while receiving data KLine.setReadTimeout(1000); // Optional: maximum time (ms) to wait for a response after sending a request - KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. - KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. - KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. - KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. + //KLine.setInitAddress(0x33); // Optional: Sets the target ECU address used during the 5-baud Slow Init sequence. + //KLine.setISO9141Header(0x68, 0x6A, 0xF1); // Optional: Configures the 3-byte header (Priority, Receiver, Transmitter) for ISO9141. + //KLine.setISO14230Header(0xC0, 0x33, 0xF1); // Optional: Configures the 3-byte header (Format, Receiver, Transmitter) for KWP2000. + //KLine.setLengthMode(true); // Optional: Defines if data length is embedded in the header or sent as a separate byte. Serial.println("Honda Code."); } @@ -34,19 +34,19 @@ void loop() { void Honda_Simulator() { if (KLine.readData()) { - if (KLine.compareData(hondaEngine, sizeof(hondaEngine))) KLine.writeRawData(hondaEngine_Response, sizeof(hondaEngine_Response), 0); - else if (KLine.compareData(hondaEngine2, sizeof(hondaEngine2))) KLine.writeRawData(hondaEngine_Response, sizeof(hondaEngine_Response), 0); - else if (KLine.compareData(hondaEngine_LiveData1, sizeof(hondaEngine_LiveData1))) KLine.writeRawData(hondaEngine_LiveData1_R, sizeof(hondaEngine_LiveData1_R), 0); - else if (KLine.compareData(hondaEngine_LiveData2, sizeof(hondaEngine_LiveData2))) KLine.writeRawData(hondaEngine_LiveData2_R, sizeof(hondaEngine_LiveData2_R), 3); - else if (KLine.compareData(hondaEngine_LiveData3, sizeof(hondaEngine_LiveData3))) KLine.writeRawData(hondaEngine_LiveData3_R, sizeof(hondaEngine_LiveData3_R), 0); - else if (KLine.compareData(hondaEngine_LiveData4, sizeof(hondaEngine_LiveData4))) KLine.writeRawData(hondaEngine_LiveData4_R, sizeof(hondaEngine_LiveData4_R), 0); - else if (KLine.compareData(hondaEngine_LiveData5, sizeof(hondaEngine_LiveData5))) KLine.writeRawData(hondaEngine_LiveData5_R, sizeof(hondaEngine_LiveData5_R), 0); - else if (KLine.compareData(hondaEngine_LiveData6, sizeof(hondaEngine_LiveData6))) KLine.writeRawData(hondaEngine_LiveData6_R, sizeof(hondaEngine_LiveData6_R), 0); - else if (KLine.compareData(hondaEngine_LiveData7, sizeof(hondaEngine_LiveData7))) KLine.writeRawData(hondaEngine_LiveData7_R, sizeof(hondaEngine_LiveData7_R), 0); - else if (KLine.compareData(hondaEngine_LiveData8, sizeof(hondaEngine_LiveData8))) KLine.writeRawData(hondaEngine_LiveData8_R, sizeof(hondaEngine_LiveData8_R), 0); - else if (KLine.compareData(hondaEngine_LiveData9, sizeof(hondaEngine_LiveData9))) KLine.writeRawData(hondaEngine_LiveData9_R, sizeof(hondaEngine_LiveData9_R), 0); - else if (KLine.compareData(hondaEngine_ReadDTCs, sizeof(hondaEngine_ReadDTCs))) KLine.writeRawData(hondaEngine_ReadDTCs_R, sizeof(hondaEngine_ReadDTCs_R), 0); - else if (KLine.compareData(hondaEngine_ClearDTCs, sizeof(hondaEngine_ClearDTCs))) KLine.writeRawData(hondaEngine_ClearDTCs_R, sizeof(hondaEngine_ClearDTCs_R), 0); - //else if (KLine.compareData(hondaABS, sizeof(hondaABS))) KLine.writeRawData(hondaABS_Response, sizeof(hondaABS_Response), false); + if (KLine.compareData(hondaEngine)) KLine.writeRawData(hondaEngine_Response, 0); + else if (KLine.compareData(hondaEngine2)) KLine.writeRawData(hondaEngine_Response, 0); + else if (KLine.compareData(hondaEngine_LiveData1)) KLine.writeRawData(hondaEngine_LiveData1_R, 0); + else if (KLine.compareData(hondaEngine_LiveData2)) KLine.writeRawData(hondaEngine_LiveData2_R, 3); + else if (KLine.compareData(hondaEngine_LiveData3)) KLine.writeRawData(hondaEngine_LiveData3_R, 0); + else if (KLine.compareData(hondaEngine_LiveData4)) KLine.writeRawData(hondaEngine_LiveData4_R, 0); + else if (KLine.compareData(hondaEngine_LiveData5)) KLine.writeRawData(hondaEngine_LiveData5_R, 0); + else if (KLine.compareData(hondaEngine_LiveData6)) KLine.writeRawData(hondaEngine_LiveData6_R, 0); + else if (KLine.compareData(hondaEngine_LiveData7)) KLine.writeRawData(hondaEngine_LiveData7_R, 0); + else if (KLine.compareData(hondaEngine_LiveData8)) KLine.writeRawData(hondaEngine_LiveData8_R, 0); + else if (KLine.compareData(hondaEngine_LiveData9)) KLine.writeRawData(hondaEngine_LiveData9_R, 0); + else if (KLine.compareData(hondaEngine_ReadDTCs)) KLine.writeRawData(hondaEngine_ReadDTCs_R, 0); + else if (KLine.compareData(hondaEngine_ClearDTCs)) KLine.writeRawData(hondaEngine_ClearDTCs_R, 0); + //else if (KLine.compareData(hondaABS)) KLine.writeRawData(hondaABS_Response, false); } }