├── src ├── keyed_struct.h ├── decimals.h ├── units.h ├── fault_code_elaboration_EN.h ├── fault_code_elaboration_DE.h ├── fault_code_elaboration_RO.h ├── fault_code_elaboration_PL.h └── KLineKWP1281Lib_ESP32.h ├── library.properties ├── examples ├── 06.Output_tests │ ├── configuration.h │ ├── communication.h │ └── 06.Output_tests.ino ├── 02.Fault_code_test │ ├── configuration.h │ ├── communication.h │ └── 02.Fault_code_test.ino ├── 03.Full_measurement_test │ ├── configuration.h │ ├── communication.h │ └── 03.Full_measurement_test.ino ├── 04.Continuous_measurement_test │ ├── configuration.h │ ├── communication.h │ └── 04.Continuous_measurement_test.ino ├── 05.Single_measurement_test │ ├── configuration.h │ ├── communication.h │ └── 05.Single_measurement_test.ino ├── 07.Dual_K-line_test │ ├── configuration.h │ ├── communication.h │ └── 07.Dual_K-line_test.ino └── 01.Connection_test │ ├── configuration.h │ ├── communication.h │ └── 01.Connection_test.ino ├── README.md ├── KEYWORDS.txt └── LICENSE.md /src/keyed_struct.h: -------------------------------------------------------------------------------- 1 | #ifndef KEYED_STRUCT_H 2 | #define KEYED_STRUCT_H 3 | 4 | struct keyed_struct 5 | { 6 | uint16_t code; 7 | const char* text; 8 | }; 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=KLineKWP1281Lib_ESP32 2 | version=1.2.1 3 | author=domnulvlad 4 | maintainer=domnulvlad 5 | sentence=Library for interfacing with the VAG KWP1281 protocol, via the K-line. 6 | paragraph=This library offers optimised routines that facilitate the implementation of any necessary features. 7 | category=Communication 8 | url= 9 | architectures=* 10 | includes=KLineKWP1281Lib_ESP32.h 11 | -------------------------------------------------------------------------------- /src/decimals.h: -------------------------------------------------------------------------------- 1 | #ifndef DECIMALS_H 2 | #define DECIMALS_H 3 | 4 | const uint8_t KWP_decimals_table[] = { 5 | 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 0, 3, 1, 1, 1, 2, 6 | 0, 0, 1, 1, 1, 3, 2, 1, 3, 2, 1, 1, 1, 0, 1, 2, 7 | 1, 1, 1, 2, 0, 0, 2, 1, 1, 0, 1, 1, 0, 1, 3, 1, 8 | 1, 1, 0, 2, 1, 2, 0, 2, 0, 0, 1, 4, 2, 3, 1, 0, 9 | 0, 1, 2, 1, 2, 2, 2, 0, 2, 2, 0, 0, 0, 2, 1, 0, 10 | 2, 2, 2, 2, 3, 2, 1, 1, 2, 0, 1, 1, 0, 3, 1, 0, 11 | 1, 0, 0, 0, 1, 3, 1, 2, 0, 2, 1, 0, 0, 0, 0, 0, 12 | 3, 2, 0, 0, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 1, 0, 13 | 0, 1, 3, 1, 1, 2, 1, 1, 0, 2, 3, 0, 0, 0, 0, 1, 14 | 1, 1, 2, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 2, 1, 15 | 7, 0, 3, 0, 0, 0, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 16 | 0, 0, 1, 0, 0, 2 17 | }; 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /examples/06.Output_tests/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module address: 2 | #define connect_to_module 0x01 3 | 4 | // Select your target module speed: 5 | #define module_baud_rate 10400 6 | 7 | // Enable/disable printing bus traffic on the Serial Monitor. 8 | #define debug_traffic false 9 | 10 | // Uncomment one of the following options, and change the pins to your requirements: 11 | 12 | // ESP32 (can use Serial1/Serial2) 13 | /* 14 | #define K_line Serial1 15 | #define TX_pin 17 16 | #define RX_pin 16 17 | */ 18 | 19 | // ESP32-C6 (can use Serial1) 20 | /* 21 | #define K_line Serial1 22 | #define TX_pin 17 23 | #define RX_pin 16 24 | */ 25 | 26 | #ifndef K_line 27 | #error Please select an option in configuration.h! 28 | #endif 29 | -------------------------------------------------------------------------------- /examples/02.Fault_code_test/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module address: 2 | #define connect_to_module 0x01 3 | 4 | // Select your target module speed: 5 | #define module_baud_rate 10400 6 | 7 | // Enable/disable printing bus traffic on the Serial Monitor. 8 | #define debug_traffic false 9 | 10 | // Uncomment one of the following options, and change the pins to your requirements: 11 | 12 | // ESP32 (can use Serial1/Serial2) 13 | /* 14 | #define K_line Serial1 15 | #define TX_pin 17 16 | #define RX_pin 16 17 | */ 18 | 19 | // ESP32-C6 (can use Serial1) 20 | /* 21 | #define K_line Serial1 22 | #define TX_pin 17 23 | #define RX_pin 16 24 | */ 25 | 26 | #ifndef K_line 27 | #error Please select an option in configuration.h! 28 | #endif 29 | -------------------------------------------------------------------------------- /examples/03.Full_measurement_test/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module address: 2 | #define connect_to_module 0x01 3 | 4 | // Select your target module speed: 5 | #define module_baud_rate 10400 6 | 7 | // Enable/disable printing bus traffic on the Serial Monitor. 8 | #define debug_traffic false 9 | 10 | // Uncomment one of the following options, and change the pins to your requirements: 11 | 12 | // ESP32 (can use Serial1/Serial2) 13 | /* 14 | #define K_line Serial1 15 | #define TX_pin 17 16 | #define RX_pin 16 17 | */ 18 | 19 | // ESP32-C6 (can use Serial1) 20 | /* 21 | #define K_line Serial1 22 | #define TX_pin 17 23 | #define RX_pin 16 24 | */ 25 | 26 | #ifndef K_line 27 | #error Please select an option in configuration.h! 28 | #endif 29 | -------------------------------------------------------------------------------- /examples/04.Continuous_measurement_test/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module address: 2 | #define connect_to_module 0x01 3 | 4 | // Select your target module speed: 5 | #define module_baud_rate 10400 6 | 7 | // Enable/disable printing bus traffic on the Serial Monitor. 8 | #define debug_traffic false 9 | 10 | // Uncomment one of the following options, and change the pins to your requirements: 11 | 12 | // ESP32 (can use Serial1/Serial2) 13 | /* 14 | #define K_line Serial1 15 | #define TX_pin 17 16 | #define RX_pin 16 17 | */ 18 | 19 | // ESP32-C6 (can use Serial1) 20 | /* 21 | #define K_line Serial1 22 | #define TX_pin 17 23 | #define RX_pin 16 24 | */ 25 | 26 | #ifndef K_line 27 | #error Please select an option in configuration.h! 28 | #endif 29 | -------------------------------------------------------------------------------- /examples/05.Single_measurement_test/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module address: 2 | #define connect_to_module 0x01 3 | 4 | // Select your target module speed: 5 | #define module_baud_rate 10400 6 | 7 | // Enable/disable printing bus traffic on the Serial Monitor. 8 | #define debug_traffic false 9 | 10 | // Uncomment one of the following options, and change the pins to your requirements: 11 | 12 | // ESP32 (can use Serial1/Serial2) 13 | /* 14 | #define K_line Serial1 15 | #define TX_pin 17 16 | #define RX_pin 16 17 | */ 18 | 19 | // ESP32-C6 (can use Serial1) 20 | /* 21 | #define K_line Serial1 22 | #define TX_pin 17 23 | #define RX_pin 16 24 | */ 25 | 26 | #ifndef K_line 27 | #error Please select an option in configuration.h! 28 | #endif 29 | -------------------------------------------------------------------------------- /examples/07.Dual_K-line_test/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module addresses: 2 | uint8_t connect_to_modules[] = {0x01, 0x17}; // K-Line 1, K-Line 2 3 | 4 | // Select your target module speed: 5 | unsigned long module_baud_rates[] = {10400, 9600}; // K-Line 1, K-Line 2 6 | 7 | // Enable/disable printing bus traffic on the Serial Monitor. 8 | #define debug_traffic false 9 | 10 | // Uncomment one of the following options, and change the pins to your requirements: 11 | 12 | // ESP32 (can use Serial1 and Serial2) 13 | /* 14 | #define K_line1 Serial1 15 | #define TX1_pin 17 16 | #define RX1_pin 16 17 | 18 | #define K_line2 Serial2 19 | #define TX2_pin 27 20 | #define RX2_pin 26 21 | */ 22 | 23 | #if not defined(K_line1) or not defined(K_line2) 24 | #error Please select an option in configuration.h! 25 | #endif 26 | -------------------------------------------------------------------------------- /examples/01.Connection_test/configuration.h: -------------------------------------------------------------------------------- 1 | // Select your target module address: 2 | #define connect_to_module 0x01 3 | 4 | // Select your target module speed: 5 | //#define module_baud_rate 10400 6 | 7 | // If you don't define a baud rate above, connecting will be attempted at the following speeds: 8 | const unsigned long possible_baud_rates[] = {10400, 9600, 4800}; 9 | 10 | // Enable/disable printing bus traffic on the Serial Monitor. 11 | #define debug_traffic false 12 | 13 | // Uncomment one of the following options, and change the pins to your requirements: 14 | 15 | // ESP32 (can use Serial1/Serial2) 16 | /* 17 | #define K_line Serial1 18 | #define TX_pin 17 19 | #define RX_pin 16 20 | */ 21 | 22 | // ESP32-C6 (can use Serial1) 23 | /* 24 | #define K_line Serial1 25 | #define TX_pin 17 26 | #define RX_pin 16 27 | */ 28 | 29 | #ifndef K_line 30 | #error Please select an option in configuration.h! 31 | #endif 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## KLineKWP1281Lib_ESP32 2 | **KLineKWP1281Lib_ESP32** is a library designed for communicating with VAG control modules which use the proprietary VAG Key-Word 1281 protocol (KWP1281/KW1281), specifically through an ESP32 development board. 3 | 4 | It is based on **[KLineKWP1281Lib](https://github.com/domnulvlad/KLineKWP1281Lib)**, with a few differences to make it more suitable for the FreeRTOS operating system. 5 | The example sketches have been modified to illustrate these differences. 6 | 7 | ## Supported features: 8 | - Everything supported by **KLineKWP1281Lib** 9 | - Implied multi-instance support for managing multiple K-lines at the same time in a task-based environment (example included) 10 | 11 | ## Getting started 12 | Download [the latest release (.zip)](https://github.com/domnulvlad/KLineKWP1281Lib_ESP32/releases/latest) and [add it to your Arduino IDE](https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries#importing-a-zip-library). 13 | 14 | **Read the documentation on [the wiki](https://github.com/domnulvlad/KLineKWP1281Lib_ESP32/wiki)!** 15 | 16 | ## Contact 17 | For any inquiries, you can open an issue or contact me at [ne555timer@yahoo.com](mailto:ne555timer@yahoo.com) (but don't include any links in the email, as it will be marked as spam). 18 | -------------------------------------------------------------------------------- /KEYWORDS.txt: -------------------------------------------------------------------------------- 1 | ########################################## 2 | # Syntax Coloring Map For KLineKWP1281Lib 3 | ########################################## 4 | 5 | ########################################## 6 | # Datatypes (KEYWORD1) 7 | ########################################## 8 | 9 | KLineKWP1281Lib_ESP32 KEYWORD1 10 | KLineKWP1281Lib KEYWORD1 11 | executionStatus KEYWORD1 12 | measurementType KEYWORD1 13 | 14 | ########################################## 15 | # Functions (KEYWORD2) 16 | ########################################## 17 | 18 | KWP1281debugFunction KEYWORD2 19 | 20 | customErrorFunction KEYWORD2 21 | 22 | attemptConnect KEYWORD2 23 | connect KEYWORD2 24 | update KEYWORD2 25 | disconnect KEYWORD2 26 | 27 | getPartNumber KEYWORD2 28 | getIdentification KEYWORD2 29 | getExtraIdentification KEYWORD2 30 | getCoding KEYWORD2 31 | getWorkshopCode KEYWORD2 32 | 33 | login KEYWORD2 34 | 35 | recode KEYWORD2 36 | 37 | readFaults KEYWORD2 38 | getFaultCode KEYWORD2 39 | isOBDFaultCode KEYWORD2 40 | getOBDFaultCode KEYWORD2 41 | getFaultDescription KEYWORD2 42 | getFaultDescriptionLength KEYWORD2 43 | getFaultElaborationCode KEYWORD2 44 | getFaultElaboration KEYWORD2 45 | getFaultElaborationLength KEYWORD2 46 | clearFaults KEYWORD2 47 | 48 | readAdaptation KEYWORD2 49 | testAdaptation KEYWORD2 50 | adapt KEYWORD2 51 | 52 | basicSetting KEYWORD2 53 | getBasicSettingValue KEYWORD2 54 | 55 | readGroup KEYWORD2 56 | getFormula KEYWORD2 57 | getNWb KEYWORD2 58 | getMWb KEYWORD2 59 | getMeasurementData KEYWORD2 60 | getMeasurementDataLength KEYWORD2 61 | 62 | getMeasurementType KEYWORD2 63 | getMeasurementValue KEYWORD2 64 | getMeasurementUnits KEYWORD2 65 | getMeasurementText KEYWORD2 66 | getMeasurementTextLength KEYWORD2 67 | getMeasurementDecimals KEYWORD2 68 | 69 | readROM KEYWORD2 70 | 71 | outputTests KEYWORD2 72 | getOutputTestDescription KEYWORD2 73 | getOutputTestDescriptionLength KEYWORD2 74 | 75 | ########################################## 76 | # Constants/variables (LITERAL1) 77 | ########################################## 78 | 79 | FAIL LITERAL1 80 | SUCCESS LITERAL1 81 | ERROR LITERAL1 82 | 83 | UNKNOWN LITERAL1 84 | VALUE LITERAL1 85 | TEXT LITERAL1 86 | 87 | initComplementDelay LITERAL1 88 | complementDelay LITERAL1 89 | byteDelay LITERAL1 90 | blockDelay LITERAL1 91 | 92 | initResponseTimeout LITERAL1 93 | complementResponseTimeout LITERAL1 94 | echoTimeout LITERAL1 95 | responseTimeout LITERAL1 96 | byteTimeout LITERAL1 97 | -------------------------------------------------------------------------------- /examples/02.Fault_code_test/communication.h: -------------------------------------------------------------------------------- 1 | // This handle refers to the task managing the K-Line, in order to be able to send a notification to it. 2 | TaskHandle_t demo_task_handle; 3 | 4 | // Callback executed by the serial event task when serial data is available 5 | void onReceiveFunction() 6 | { 7 | // Send a notification to the task managing the K-Line. 8 | xTaskNotify(demo_task_handle, 0, eNoAction); 9 | 10 | // It is also possible to use an event group, semaphore or queue for the purpose of signaling that a byte was received. 11 | // If you don't want to use task notifications, also change the code in beginFunction() and receiveFunction() below. 12 | } 13 | 14 | // Initialize the serial port 15 | void beginFunction(unsigned long baud) { 16 | // Start serial communication. 17 | K_line.begin(baud, SERIAL_8N1, RX_pin, TX_pin); 18 | 19 | // Attach the callback that executes every time a byte is received. 20 | K_line.onReceive(onReceiveFunction, false); 21 | K_line.setRxFIFOFull(1); 22 | 23 | // Reset the task's notification state. 24 | xTaskNotifyStateClear(NULL); 25 | } 26 | 27 | // Stop communication on the serial port 28 | void endFunction() { 29 | K_line.end(); 30 | } 31 | 32 | // Send a byte 33 | void sendFunction(uint8_t data) { 34 | K_line.write(data); 35 | } 36 | 37 | // Receive a byte 38 | bool receiveFunction(uint8_t *data, unsigned long timeout_ticks) { 39 | // If no byte is immediately available, wait. 40 | if (K_line.available() <= 0) 41 | { 42 | // Wait until either a notification is received, or a timeout occurs. 43 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 44 | { 45 | return false; 46 | } 47 | } 48 | 49 | // A byte is available; store it in the variable passed by pointer. 50 | *data = K_line.read(); 51 | return true; 52 | } 53 | 54 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 55 | #if debug_traffic 56 | void KWP1281debugFunction(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 57 | { 58 | printf("\t%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 59 | 60 | if (length) 61 | { 62 | printf("D:"); 63 | 64 | // Iterate through the bytes of the message. 65 | for (size_t i = 0; i < length; i++) 66 | { 67 | printf("%02X ", data[i]); 68 | } 69 | } 70 | 71 | printf("\n"); 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /examples/06.Output_tests/communication.h: -------------------------------------------------------------------------------- 1 | // This handle refers to the task managing the K-Line, in order to be able to send a notification to it. 2 | TaskHandle_t demo_task_handle; 3 | 4 | // Callback executed by the serial event task when serial data is available 5 | void onReceiveFunction() 6 | { 7 | // Send a notification to the task managing the K-Line. 8 | xTaskNotify(demo_task_handle, 0, eNoAction); 9 | 10 | // It is also possible to use an event group, semaphore or queue for the purpose of signaling that a byte was received. 11 | // If you don't want to use task notifications, also change the code in beginFunction() and receiveFunction() below. 12 | } 13 | 14 | // Initialize the serial port 15 | void beginFunction(unsigned long baud) { 16 | // Start serial communication. 17 | K_line.begin(baud, SERIAL_8N1, RX_pin, TX_pin); 18 | 19 | // Attach the callback that executes every time a byte is received. 20 | K_line.onReceive(onReceiveFunction, false); 21 | K_line.setRxFIFOFull(1); 22 | 23 | // Reset the task's notification state. 24 | xTaskNotifyStateClear(NULL); 25 | } 26 | 27 | // Stop communication on the serial port 28 | void endFunction() { 29 | K_line.end(); 30 | } 31 | 32 | // Send a byte 33 | void sendFunction(uint8_t data) { 34 | K_line.write(data); 35 | } 36 | 37 | // Receive a byte 38 | bool receiveFunction(uint8_t *data, unsigned long timeout_ticks) { 39 | // If no byte is immediately available, wait. 40 | if (K_line.available() <= 0) 41 | { 42 | // Wait until either a notification is received, or a timeout occurs. 43 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 44 | { 45 | return false; 46 | } 47 | } 48 | 49 | // A byte is available; store it in the variable passed by pointer. 50 | *data = K_line.read(); 51 | return true; 52 | } 53 | 54 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 55 | #if debug_traffic 56 | void KWP1281debugFunction(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 57 | { 58 | printf("\t%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 59 | 60 | if (length) 61 | { 62 | printf("D:"); 63 | 64 | // Iterate through the bytes of the message. 65 | for (size_t i = 0; i < length; i++) 66 | { 67 | printf("%02X ", data[i]); 68 | } 69 | } 70 | 71 | printf("\n"); 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /examples/03.Full_measurement_test/communication.h: -------------------------------------------------------------------------------- 1 | // This handle refers to the task managing the K-Line, in order to be able to send a notification to it. 2 | TaskHandle_t demo_task_handle; 3 | 4 | // Callback executed by the serial event task when serial data is available 5 | void onReceiveFunction() 6 | { 7 | // Send a notification to the task managing the K-Line. 8 | xTaskNotify(demo_task_handle, 0, eNoAction); 9 | 10 | // It is also possible to use an event group, semaphore or queue for the purpose of signaling that a byte was received. 11 | // If you don't want to use task notifications, also change the code in beginFunction() and receiveFunction() below. 12 | } 13 | 14 | // Initialize the serial port 15 | void beginFunction(unsigned long baud) { 16 | // Start serial communication. 17 | K_line.begin(baud, SERIAL_8N1, RX_pin, TX_pin); 18 | 19 | // Attach the callback that executes every time a byte is received. 20 | K_line.onReceive(onReceiveFunction, false); 21 | K_line.setRxFIFOFull(1); 22 | 23 | // Reset the task's notification state. 24 | xTaskNotifyStateClear(NULL); 25 | } 26 | 27 | // Stop communication on the serial port 28 | void endFunction() { 29 | K_line.end(); 30 | } 31 | 32 | // Send a byte 33 | void sendFunction(uint8_t data) { 34 | K_line.write(data); 35 | } 36 | 37 | // Receive a byte 38 | bool receiveFunction(uint8_t *data, unsigned long timeout_ticks) { 39 | // If no byte is immediately available, wait. 40 | if (K_line.available() <= 0) 41 | { 42 | // Wait until either a notification is received, or a timeout occurs. 43 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 44 | { 45 | return false; 46 | } 47 | } 48 | 49 | // A byte is available; store it in the variable passed by pointer. 50 | *data = K_line.read(); 51 | return true; 52 | } 53 | 54 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 55 | #if debug_traffic 56 | void KWP1281debugFunction(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 57 | { 58 | printf("\t%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 59 | 60 | if (length) 61 | { 62 | printf("D:"); 63 | 64 | // Iterate through the bytes of the message. 65 | for (size_t i = 0; i < length; i++) 66 | { 67 | printf("%02X ", data[i]); 68 | } 69 | } 70 | 71 | printf("\n"); 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /examples/05.Single_measurement_test/communication.h: -------------------------------------------------------------------------------- 1 | // This handle refers to the task managing the K-Line, in order to be able to send a notification to it. 2 | TaskHandle_t demo_task_handle; 3 | 4 | // Callback executed by the serial event task when serial data is available 5 | void onReceiveFunction() 6 | { 7 | // Send a notification to the task managing the K-Line. 8 | xTaskNotify(demo_task_handle, 0, eNoAction); 9 | 10 | // It is also possible to use an event group, semaphore or queue for the purpose of signaling that a byte was received. 11 | // If you don't want to use task notifications, also change the code in beginFunction() and receiveFunction() below. 12 | } 13 | 14 | // Initialize the serial port 15 | void beginFunction(unsigned long baud) { 16 | // Start serial communication. 17 | K_line.begin(baud, SERIAL_8N1, RX_pin, TX_pin); 18 | 19 | // Attach the callback that executes every time a byte is received. 20 | K_line.onReceive(onReceiveFunction, false); 21 | K_line.setRxFIFOFull(1); 22 | 23 | // Reset the task's notification state. 24 | xTaskNotifyStateClear(NULL); 25 | } 26 | 27 | // Stop communication on the serial port 28 | void endFunction() { 29 | K_line.end(); 30 | } 31 | 32 | // Send a byte 33 | void sendFunction(uint8_t data) { 34 | K_line.write(data); 35 | } 36 | 37 | // Receive a byte 38 | bool receiveFunction(uint8_t *data, unsigned long timeout_ticks) { 39 | // If no byte is immediately available, wait. 40 | if (K_line.available() <= 0) 41 | { 42 | // Wait until either a notification is received, or a timeout occurs. 43 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 44 | { 45 | return false; 46 | } 47 | } 48 | 49 | // A byte is available; store it in the variable passed by pointer. 50 | *data = K_line.read(); 51 | return true; 52 | } 53 | 54 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 55 | #if debug_traffic 56 | void KWP1281debugFunction(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 57 | { 58 | printf("\t%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 59 | 60 | if (length) 61 | { 62 | printf("D:"); 63 | 64 | // Iterate through the bytes of the message. 65 | for (size_t i = 0; i < length; i++) 66 | { 67 | printf("%02X ", data[i]); 68 | } 69 | } 70 | 71 | printf("\n"); 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /examples/04.Continuous_measurement_test/communication.h: -------------------------------------------------------------------------------- 1 | // This handle refers to the task managing the K-Line, in order to be able to send a notification to it. 2 | TaskHandle_t demo_task_handle; 3 | 4 | // Callback executed by the serial event task when serial data is available 5 | void onReceiveFunction() 6 | { 7 | // Send a notification to the task managing the K-Line. 8 | xTaskNotify(demo_task_handle, 0, eNoAction); 9 | 10 | // It is also possible to use an event group, semaphore or queue for the purpose of signaling that a byte was received. 11 | // If you don't want to use task notifications, also change the code in beginFunction() and receiveFunction() below. 12 | } 13 | 14 | // Initialize the serial port 15 | void beginFunction(unsigned long baud) { 16 | // Start serial communication. 17 | K_line.begin(baud, SERIAL_8N1, RX_pin, TX_pin); 18 | 19 | // Attach the callback that executes every time a byte is received. 20 | K_line.onReceive(onReceiveFunction, false); 21 | K_line.setRxFIFOFull(1); 22 | 23 | // Reset the task's notification state. 24 | xTaskNotifyStateClear(NULL); 25 | } 26 | 27 | // Stop communication on the serial port 28 | void endFunction() { 29 | K_line.end(); 30 | } 31 | 32 | // Send a byte 33 | void sendFunction(uint8_t data) { 34 | K_line.write(data); 35 | } 36 | 37 | // Receive a byte 38 | bool receiveFunction(uint8_t *data, unsigned long timeout_ticks) { 39 | // If no byte is immediately available, wait. 40 | if (K_line.available() <= 0) 41 | { 42 | // Wait until either a notification is received, or a timeout occurs. 43 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 44 | { 45 | return false; 46 | } 47 | } 48 | 49 | // A byte is available; store it in the variable passed by pointer. 50 | *data = K_line.read(); 51 | return true; 52 | } 53 | 54 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 55 | #if debug_traffic 56 | void KWP1281debugFunction(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 57 | { 58 | printf("\t%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 59 | 60 | if (length) 61 | { 62 | printf("D:"); 63 | 64 | // Iterate through the bytes of the message. 65 | for (size_t i = 0; i < length; i++) 66 | { 67 | printf("%02X ", data[i]); 68 | } 69 | } 70 | 71 | printf("\n"); 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /examples/01.Connection_test/communication.h: -------------------------------------------------------------------------------- 1 | // This handle refers to the task managing the K-Line, in order to be able to send a notification to it. 2 | TaskHandle_t demo_task_handle; 3 | 4 | // Callback executed by the serial event task when serial data is available 5 | void onReceiveFunction() 6 | { 7 | // Send a notification to the task managing the K-Line. 8 | xTaskNotify(demo_task_handle, 0, eNoAction); 9 | 10 | // It is also possible to use an event group, semaphore or queue for the purpose of signaling that a byte was received. 11 | // If you don't want to use task notifications, also change the code in beginFunction() and receiveFunction() below. 12 | } 13 | 14 | // Initialize the serial port 15 | void beginFunction(unsigned long baud) 16 | { 17 | // Start serial communication. 18 | K_line.begin(baud, SERIAL_8N1, RX_pin, TX_pin); 19 | 20 | // Attach the callback that executes every time a byte is received. 21 | K_line.onReceive(onReceiveFunction, false); 22 | K_line.setRxFIFOFull(1); 23 | 24 | // Reset the task's notification state. 25 | xTaskNotifyStateClear(NULL); 26 | } 27 | 28 | // Stop communication on the serial port 29 | void endFunction() 30 | { 31 | K_line.end(); 32 | } 33 | 34 | // Send a byte 35 | void sendFunction(uint8_t data) 36 | { 37 | K_line.write(data); 38 | } 39 | 40 | // Receive a byte 41 | bool receiveFunction(uint8_t *data, unsigned long timeout_ticks) 42 | { 43 | // If no byte is immediately available, wait. 44 | if (K_line.available() <= 0) 45 | { 46 | // Wait until either a notification is received, or a timeout occurs. 47 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 48 | { 49 | return false; 50 | } 51 | } 52 | 53 | // A byte is available; store it in the variable passed by pointer. 54 | *data = K_line.read(); 55 | return true; 56 | } 57 | 58 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 59 | #if debug_traffic 60 | void KWP1281debugFunction(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 61 | { 62 | printf("\t%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 63 | 64 | if (length) 65 | { 66 | printf("D:"); 67 | 68 | // Iterate through the bytes of the message. 69 | for (size_t i = 0; i < length; i++) 70 | { 71 | printf("%02X ", data[i]); 72 | } 73 | } 74 | 75 | printf("\n"); 76 | } 77 | #endif 78 | -------------------------------------------------------------------------------- /src/units.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITS_H 2 | #define UNITS_H 3 | 4 | #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" 5 | #define BYTE_TO_BINARY(byte) \ 6 | ((byte) & 0x80 ? '1' : '0'), \ 7 | ((byte) & 0x40 ? '1' : '0'), \ 8 | ((byte) & 0x20 ? '1' : '0'), \ 9 | ((byte) & 0x10 ? '1' : '0'), \ 10 | ((byte) & 0x08 ? '1' : '0'), \ 11 | ((byte) & 0x04 ? '1' : '0'), \ 12 | ((byte) & 0x02 ? '1' : '0'), \ 13 | ((byte) & 0x01 ? '1' : '0') 14 | 15 | const char KWP_units_RPM[] = "/min"; 16 | const char KWP_units_Percentage[] = "%"; 17 | const char KWP_units_Angle[] = "deg"; 18 | const char KWP_units_Temperature[] = "degC"; 19 | const char KWP_units_Voltage[] = "V"; 20 | const char KWP_units_Voltage_m[] = "mV"; 21 | const char KWP_units_Speed[] = "km/h"; 22 | const char KWP_units_Resistance[] = "Ohm"; 23 | const char KWP_units_Resistance_k[] = "kOhm"; 24 | const char KWP_units_Resistance_m[] = "mOhm"; 25 | const char KWP_units_Distance[] = "m"; 26 | const char KWP_units_Distance_k[] = "km"; 27 | const char KWP_units_Distance_m[] = "mm"; 28 | const char KWP_units_Distance_c[] = "cm"; 29 | const char KWP_units_Pressure[] = "bar"; 30 | const char KWP_units_Pressure_m[] = "mbar"; 31 | const char KWP_units_Time[] = "s"; 32 | const char KWP_units_Time_m[] = "ms"; 33 | const char KWP_units_Time_h[] = "h"; 34 | const char KWP_units_Time_mo[] = "months"; 35 | const char KWP_units_Volume[] = "l"; 36 | const char KWP_units_Volume_m[] = "ml"; 37 | const char KWP_units_Current[] = "A"; 38 | const char KWP_units_Current_m[] = "mA"; 39 | const char KWP_units_Capacity[] = "Ah"; 40 | const char KWP_units_Power[] = "W"; 41 | const char KWP_units_Power_k[] = "kW"; 42 | const char KWP_units_Mass_Flow[] = "g/s"; 43 | const char KWP_units_Mass_Flow_m[] = "mg/s"; 44 | const char KWP_units_Mass_Flow_km[] = "mg/km"; 45 | const char KWP_units_Correction[] = "KW"; 46 | const char KWP_units_Segment_Correction[] = "degKW"; 47 | const char KWP_units_Consumption_h[] = "l/h"; 48 | const char KWP_units_Consumption_100km[] = "l/100km"; 49 | const char KWP_units_Consumption_1000km[] = "l/1000km"; 50 | const char KWP_units_Mass_Per_Stroke_m[] = "mg/h"; 51 | const char KWP_units_Mass_Per_Stroke_k[] = "kg/h"; 52 | const char KWP_units_Torque[] = "Nm"; 53 | const char KWP_units_Misfires[] = "/s"; 54 | const char KWP_units_Turn_Rate[] = "deg/s"; 55 | const char KWP_units_Acceleration[] = "m/s^2"; 56 | const char KWP_units_Mass[] = "g"; 57 | const char KWP_units_Mass_k[] = "kg"; 58 | const char KWP_units_Impulses[] = "/km"; 59 | const char KWP_units_Fuel_Level_Factor[] = "l/mm"; 60 | const char KWP_units_Attenuation[] = "dB"; 61 | const char KWP_units_Parts_Per_Million[] = "ppm"; 62 | 63 | const char KWP_units_Warm[] = "Warm"; 64 | const char KWP_units_Cold[] = "Cold"; 65 | 66 | const char KWP_units_Ignition_ATDC[] = "degATDC"; 67 | const char KWP_units_Ignition_BTDC[] = "degBTDC"; 68 | 69 | const char KWP_units_Map1[] = "Map1"; 70 | const char KWP_units_Map2[] = "Map2"; 71 | 72 | const char KWP_units_Vss[] = "Vss"; 73 | const char KWP_units_Wm2[] = "W/m^2"; 74 | const char KWP_units_Wcm2[] = "W/cm^2"; 75 | const char KWP_units_Wh[] = "Wh"; 76 | const char KWP_units_Ws[] = "Ws"; 77 | const char KWP_units_ms[] = "m/s"; 78 | const char KWP_units_lkm[] = "l/km"; 79 | const char KWP_units_N[] = "N"; 80 | const char KWP_units_angdeg[] = " RX pin 16 19 | *K-line RX -> TX pin 17 20 | 21 | ESP32-C6 22 | *has one additional serial port 23 | *pins (they can be remapped, this is what they are configured to in these examples): 24 | *K-line TX -> RX pin 16 25 | *K-line RX -> TX pin 17 26 | */ 27 | 28 | // Include the library. 29 | #include 30 | 31 | // Include the two files containing configuration options and the functions used for communication. 32 | #include "configuration.h" 33 | #include "communication.h" 34 | 35 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 36 | KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin); 37 | 38 | void setup() 39 | { 40 | // Initialize the Serial Monitor. 41 | Serial.begin(115200); 42 | delay(500); 43 | printf("Sketch started.\n"); 44 | 45 | // If debugging bus traffic was enabled, attach the debugging function. 46 | #if debug_traffic 47 | diag.KWP1281debugFunction(KWP1281debugFunction); 48 | #endif 49 | 50 | // Create the task that runs this demo, attaching the handle defined in "configuration.h". 51 | assert(xTaskCreate(demo_task, "demo", 4096, NULL, 5, &demo_task_handle)); 52 | } 53 | 54 | void loop() 55 | { 56 | // The Arduino loop is not used; delete its task. 57 | vTaskDelete(NULL); 58 | } 59 | 60 | void demo_task(void *args) 61 | { 62 | #ifdef module_baud_rate 63 | // If a specific baud rate was defined, connect with it. 64 | printf("Connecting with baud rate %lu\n", (unsigned long)module_baud_rate); 65 | diag.connect(connect_to_module, module_baud_rate); 66 | #else 67 | // Otherwise, try each baud rate from the possible_baud_rates array. 68 | size_t current_index = 0; 69 | while (true) 70 | { 71 | // If connecting fails, try the next baud rate. 72 | printf("Trying to connect with baud rate %lu\n", possible_baud_rates[current_index]); 73 | if (diag.attemptConnect(connect_to_module, possible_baud_rates[current_index]) != KLineKWP1281Lib::SUCCESS) 74 | { 75 | // If there are more baud rates to try, increment the index. 76 | if (current_index < (sizeof(possible_baud_rates) / sizeof(possible_baud_rates[0])) - 1) 77 | { 78 | current_index++; 79 | } 80 | // Otherwise, start over. 81 | else 82 | { 83 | current_index = 0; 84 | } 85 | 86 | // Delay 1 second before trying the next baud rate. 87 | vTaskDelay(pdMS_TO_TICKS(1000)); 88 | } 89 | // If the connection is successful, continue. 90 | else 91 | { 92 | break; 93 | } 94 | } 95 | #endif 96 | 97 | // Display the module's part number and identification. 98 | printf("\n%s\n%s\n", diag.getPartNumber(), diag.getIdentification()); 99 | 100 | // If it is available, display the module's extra identification. 101 | if (strlen(diag.getExtraIdentification())) 102 | { 103 | printf("%s\n", diag.getExtraIdentification()); 104 | } 105 | 106 | // Display the module's coding value and workshop code. 107 | printf("\nCoding: %05u\nWSC: %05lu\n\n", diag.getCoding(), diag.getWorkshopCode()); 108 | 109 | // A task must either have an infinite loop, or delete itself. 110 | printf("Maintaining the connection active.\n"); 111 | while (true) 112 | { 113 | // Maintain the connection. 114 | diag.update(); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /examples/07.Dual_K-line_test/communication.h: -------------------------------------------------------------------------------- 1 | //////////////////////////////// K-line 1 2 | 3 | // This handle refers to the task managing K-Line 1, in order to be able to send a notification to it. 4 | TaskHandle_t demo_task1_handle; 5 | 6 | // Callback executed by the serial event task when serial data is available on K-Line 1 7 | void onReceiveFunction1() 8 | { 9 | // Send a notification to the task managing K-Line 1. 10 | xTaskNotify(demo_task1_handle, 0, eNoAction); 11 | } 12 | 13 | // Initialize the serial port 14 | void beginFunction1(unsigned long baud) { 15 | // Start serial communication. 16 | K_line1.begin(baud, SERIAL_8N1, RX1_pin, TX1_pin); 17 | 18 | // Attach the callback that executes every time a byte is received. 19 | K_line1.onReceive(onReceiveFunction1, false); 20 | K_line1.setRxFIFOFull(1); 21 | 22 | // Reset the task's notification state. 23 | xTaskNotifyStateClear(NULL); 24 | } 25 | 26 | // Stop communication on the serial port 27 | void endFunction1() { 28 | K_line1.end(); 29 | } 30 | 31 | // Send a byte 32 | void sendFunction1(uint8_t data) { 33 | K_line1.write(data); 34 | } 35 | 36 | // Receive a byte 37 | bool receiveFunction1(uint8_t *data, unsigned long timeout_ticks) { 38 | // If no byte is immediately available, wait. 39 | if (K_line1.available() <= 0) 40 | { 41 | // Wait until either a notification is received, or a timeout occurs. 42 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 43 | { 44 | return false; 45 | } 46 | } 47 | 48 | // A byte is available; store it in the variable passed by pointer. 49 | *data = K_line1.read(); 50 | return true; 51 | } 52 | 53 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 54 | #if debug_traffic 55 | void KWP1281debugFunction1(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 56 | { 57 | printf("\t1.%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 58 | 59 | if (length) 60 | { 61 | printf("D:"); 62 | 63 | // Iterate through the bytes of the message. 64 | for (size_t i = 0; i < length; i++) 65 | { 66 | printf("%02X ", data[i]); 67 | } 68 | } 69 | 70 | printf("\n"); 71 | } 72 | #endif 73 | 74 | //////////////////////////////// K-line 2 75 | 76 | // This handle refers to the task managing K-Line 2, in order to be able to send a notification to it. 77 | TaskHandle_t demo_task2_handle; 78 | 79 | // Callback executed by the serial event task when serial data is available on K-Line 2 80 | void onReceiveFunction2() 81 | { 82 | // Send a notification to the task managing K-Line 2. 83 | xTaskNotify(demo_task2_handle, 0, eNoAction); 84 | } 85 | 86 | // Initialize the serial port 87 | void beginFunction2(unsigned long baud) { 88 | // Start serial communication. 89 | K_line2.begin(baud, SERIAL_8N1, RX2_pin, TX2_pin); 90 | 91 | // Attach the callback that executes every time a byte is received. 92 | K_line2.onReceive(onReceiveFunction2, false); 93 | K_line2.setRxFIFOFull(1); 94 | 95 | // Reset the task's notification state. 96 | xTaskNotifyStateClear(NULL); 97 | } 98 | 99 | // Stop communication on the serial port 100 | void endFunction2() { 101 | K_line2.end(); 102 | } 103 | 104 | // Send a byte 105 | void sendFunction2(uint8_t data) { 106 | K_line2.write(data); 107 | } 108 | 109 | // Receive a byte 110 | bool receiveFunction2(uint8_t *data, unsigned long timeout_ticks) { 111 | // If no byte is immediately available, wait. 112 | if (K_line2.available() <= 0) 113 | { 114 | // Wait until either a notification is received, or a timeout occurs. 115 | if (xTaskNotifyWait(0, 0, NULL, timeout_ticks) != pdTRUE) 116 | { 117 | return false; 118 | } 119 | } 120 | 121 | // A byte is available; store it in the variable passed by pointer. 122 | *data = K_line2.read(); 123 | return true; 124 | } 125 | 126 | // Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor. 127 | #if debug_traffic 128 | void KWP1281debugFunction2(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length) 129 | { 130 | printf("\t2.%s S:%02X T:%02X L:%d ", (direction ? "RECEIVE" : "SEND"), message_sequence, message_type, length); 131 | 132 | if (length) 133 | { 134 | printf("D:"); 135 | 136 | // Iterate through the bytes of the message. 137 | for (size_t i = 0; i < length; i++) 138 | { 139 | printf("%02X ", data[i]); 140 | } 141 | } 142 | 143 | printf("\n"); 144 | } 145 | #endif 146 | -------------------------------------------------------------------------------- /examples/06.Output_tests/06.Output_tests.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Title: 3 | 06.Output_tests 4 | 5 | Description: 6 | Demonstrates how to perform output tests. 7 | 8 | Notes: 9 | *The connection will be stopped after the output tests are performed. 10 | *Output test descriptions are the same as fault code descriptions 11 | *Having KWP1281_FAULT_CODE_DESCRIPTION_SUPPORTED disabled means the descriptions will be replaced by "EN_dsc". 12 | */ 13 | 14 | /* 15 | ***Uncomment the appropriate options in the "configuration.h" file! 16 | 17 | ESP32 18 | *has two additional serial ports 19 | *pins (they can be remapped, this is what they are configured to in these examples): 20 | *K-line TX -> RX pin 16 21 | *K-line RX -> TX pin 17 22 | 23 | ESP32-C6 24 | *has one additional serial port 25 | *pins (they can be remapped, this is what they are configured to in these examples): 26 | *K-line TX -> RX pin 16 27 | *K-line RX -> TX pin 17 28 | */ 29 | 30 | // Include the library. 31 | #include 32 | 33 | // Include the two files containing configuration options and the functions used for communication. 34 | #include "configuration.h" 35 | #include "communication.h" 36 | 37 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 38 | KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin); 39 | 40 | // How many milliseconds to idle on each output test 41 | #define WAIT_TIME_MS 5000 42 | 43 | void setup() 44 | { 45 | // Initialize the Serial Monitor. 46 | Serial.begin(115200); 47 | delay(500); 48 | printf("Sketch started.\n"); 49 | 50 | // If debugging bus traffic was enabled, attach the debugging function. 51 | #if debug_traffic 52 | diag.KWP1281debugFunction(KWP1281debugFunction); 53 | #endif 54 | 55 | // Create the task that runs this demo, attaching the handle defined in "configuration.h". 56 | assert(xTaskCreate(demo_task, "demo", 4096, NULL, 5, &demo_task_handle)); 57 | } 58 | 59 | void loop() 60 | { 61 | // The Arduino loop is not used; delete its task. 62 | vTaskDelete(NULL); 63 | } 64 | 65 | void demo_task(void *args) 66 | { 67 | // Connect to the module. 68 | diag.connect(connect_to_module, module_baud_rate, false); 69 | 70 | // Perform all output tests (procedure moved to a function). 71 | printf("Performing output tests.\n"); 72 | performOutputTests(); 73 | 74 | // Disconnect from the module. 75 | diag.disconnect(false); 76 | printf("Disconnected.\n"); 77 | 78 | // A task must either have an infinite loop, or delete itself. 79 | vTaskDelete(NULL); 80 | } 81 | 82 | void performOutputTests() 83 | { 84 | // Output tests are always performed in a predefined sequence that cannot be changed. 85 | // It is possible to determine which output test is currently running, as they return a value. 86 | // This value is technically a fault code, so its description can be retrieved. 87 | uint16_t current_output_test; 88 | 89 | /* 90 | The outputTests() function can return: 91 | *KLineKWP1281Lib::SUCCESS - performed next output test 92 | *KLineKWP1281Lib::FAIL - the module doesn't support output tests / reached end of output tests 93 | *KLineKWP1281Lib::ERROR - communication error 94 | */ 95 | 96 | // Run the first output test and store the return value. 97 | KLineKWP1281Lib::executionStatus outputTests_status = diag.outputTests(current_output_test); 98 | 99 | // Continue running output tests until something other than SUCCESS is returned. 100 | while (outputTests_status == KLineKWP1281Lib::SUCCESS) 101 | { 102 | // Declare a character array and use it to store the description string. 103 | char description_string[32]; 104 | KLineKWP1281Lib::getOutputTestDescription(current_output_test, description_string, sizeof(description_string)); 105 | 106 | // Display the output test description. 107 | printf("Performing output test: %s", description_string); 108 | 109 | // Get the full length of the description string, to warn the user if the provided buffer wasn't large enough to store the entire string. 110 | size_t description_string_length = KLineKWP1281Lib::getOutputTestDescriptionLength(current_output_test); 111 | 112 | // If the buffer was too small, display an ellipsis and indicate how many characters would have been needed for the entire string. 113 | if (description_string_length > (sizeof(description_string) - 1)) 114 | { 115 | printf("... (%d/%d)", sizeof(description_string) - 1, description_string_length); 116 | } 117 | printf("\n"); 118 | 119 | // Wait as many milliseconds as specified by WAIT_TIME_MS. 120 | // The communication is kept alive by update() while waiting. 121 | unsigned long start_time = millis(); 122 | while (millis() - start_time <= WAIT_TIME_MS) 123 | { 124 | diag.update(); 125 | } 126 | 127 | // Run the next output test. 128 | outputTests_status = diag.outputTests(current_output_test); 129 | } 130 | 131 | // At this point, the loop above was stopped because either FAIL or ERROR was returned. 132 | 133 | // If FAIL was returned, the output test sequence is complete. 134 | if (outputTests_status == KLineKWP1281Lib::FAIL) 135 | { 136 | printf("End of output tests\n"); 137 | } 138 | // If ERROR was returned, a communication error probably occurred. 139 | else 140 | { 141 | printf("Error performing output tests\n"); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/fault_code_elaboration_EN.h: -------------------------------------------------------------------------------- 1 | #ifndef FAULT_CODE_ELABORATION_H 2 | #define FAULT_CODE_ELABORATION_H 3 | 4 | const char KWP_ELAB_00[] = "-"; 5 | const char KWP_ELAB_01[] = "Signal Shorted to Plus"; 6 | const char KWP_ELAB_02[] = "Signal Shorted to Ground"; 7 | const char KWP_ELAB_03[] = "No Signal"; 8 | const char KWP_ELAB_04[] = "Mechanical Malfunction"; 9 | const char KWP_ELAB_05[] = "Input Open"; 10 | const char KWP_ELAB_06[] = "Signal too High"; 11 | const char KWP_ELAB_07[] = "Signal too Low"; 12 | const char KWP_ELAB_08[] = "Control Limit Surpassed"; 13 | const char KWP_ELAB_09[] = "Adaptation Limit Surpassed"; 14 | const char KWP_ELAB_0A[] = "Adaptation Limit Not Reached"; 15 | const char KWP_ELAB_0B[] = "Control Limit Not Reached"; 16 | const char KWP_ELAB_0C[] = "Adaptation Limit (Mul) Exceeded"; 17 | const char KWP_ELAB_0D[] = "Adaptation Limit (Mul) Not Reached"; 18 | const char KWP_ELAB_0E[] = "Adaptation Limit (Add) Exceeded"; 19 | const char KWP_ELAB_0F[] = "Adaptation Limit (Add) Not Reached"; 20 | const char KWP_ELAB_10[] = "Signal Outside Specifications"; 21 | const char KWP_ELAB_11[] = "Control Difference"; 22 | const char KWP_ELAB_12[] = "Upper Limit"; 23 | const char KWP_ELAB_13[] = "Lower Limit"; 24 | const char KWP_ELAB_14[] = "Malfunction in Basic Setting"; 25 | const char KWP_ELAB_15[] = "Front Pressure Build-up Time too Long"; 26 | const char KWP_ELAB_16[] = "Front Pressure Reducing Time too Long"; 27 | const char KWP_ELAB_17[] = "Rear Pressure Build-up Time too Long"; 28 | const char KWP_ELAB_18[] = "Rear Pressure Reducing Time too Long"; 29 | const char KWP_ELAB_19[] = "Unknown Switch Condition"; 30 | const char KWP_ELAB_1A[] = "Output Open"; 31 | const char KWP_ELAB_1B[] = "Implausible Signal"; 32 | const char KWP_ELAB_1C[] = "Short to Plus"; 33 | const char KWP_ELAB_1D[] = "Short to Ground"; 34 | const char KWP_ELAB_1E[] = "Open or Short to Plus"; 35 | const char KWP_ELAB_1F[] = "Open or Short to Ground"; 36 | const char KWP_ELAB_20[] = "Resistance Too High"; 37 | const char KWP_ELAB_21[] = "Resistance Too Low"; 38 | const char KWP_ELAB_22[] = "No Elaboration Available"; 39 | const char KWP_ELAB_23[] = "-"; 40 | const char KWP_ELAB_24[] = "Open Circuit"; 41 | const char KWP_ELAB_25[] = "Faulty"; 42 | const char KWP_ELAB_26[] = "Output won't Switch or Short to Plus"; 43 | const char KWP_ELAB_27[] = "Output won't Switch or Short to Ground"; 44 | const char KWP_ELAB_28[] = "Short to Another Output"; 45 | const char KWP_ELAB_29[] = "Blocked or No Voltage"; 46 | const char KWP_ELAB_2A[] = "Speed Deviation too High"; 47 | const char KWP_ELAB_2B[] = "Closed"; 48 | const char KWP_ELAB_2C[] = "Short Circuit"; 49 | const char KWP_ELAB_2D[] = "Connector"; 50 | const char KWP_ELAB_2E[] = "Leaking"; 51 | const char KWP_ELAB_2F[] = "No Communications or Incorrectly Connected"; 52 | const char KWP_ELAB_30[] = "Supply voltage"; 53 | const char KWP_ELAB_31[] = "No Communications"; 54 | const char KWP_ELAB_32[] = "Setting (Early) Not Reached"; 55 | const char KWP_ELAB_33[] = "Setting (Late) Not Reached"; 56 | const char KWP_ELAB_34[] = "Supply Voltage Too High"; 57 | const char KWP_ELAB_35[] = "Supply Voltage Too Low"; 58 | const char KWP_ELAB_36[] = "Incorrectly Equipped"; 59 | const char KWP_ELAB_37[] = "Adaptation Not Successful"; 60 | const char KWP_ELAB_38[] = "In Limp-Home Mode"; 61 | const char KWP_ELAB_39[] = "Electric Circuit Failure"; 62 | const char KWP_ELAB_3A[] = "Can't Lock"; 63 | const char KWP_ELAB_3B[] = "Can't Unlock"; 64 | const char KWP_ELAB_3C[] = "Won't Safe"; 65 | const char KWP_ELAB_3D[] = "Won't De-Safe"; 66 | const char KWP_ELAB_3E[] = "No or Incorrect Adjustment"; 67 | const char KWP_ELAB_3F[] = "Temperature Shut-Down"; 68 | const char KWP_ELAB_40[] = "Not Currently Testable"; 69 | const char KWP_ELAB_41[] = "Unauthorized"; 70 | const char KWP_ELAB_42[] = "Not Matched"; 71 | const char KWP_ELAB_43[] = "Set-Point Not Reached"; 72 | const char KWP_ELAB_44[] = "Cylinder 1"; 73 | const char KWP_ELAB_45[] = "Cylinder 2"; 74 | const char KWP_ELAB_46[] = "Cylinder 3"; 75 | const char KWP_ELAB_47[] = "Cylinder 4"; 76 | const char KWP_ELAB_48[] = "Cylinder 5"; 77 | const char KWP_ELAB_49[] = "Cylinder 6"; 78 | const char KWP_ELAB_4A[] = "Cylinder 7"; 79 | const char KWP_ELAB_4B[] = "Cylinder 8"; 80 | const char KWP_ELAB_4C[] = "Terminal 30 missing"; 81 | const char KWP_ELAB_4D[] = "Internal Supply Voltage"; 82 | const char KWP_ELAB_4E[] = "Missing Messages"; 83 | const char KWP_ELAB_4F[] = "Please Check Fault Codes"; 84 | const char KWP_ELAB_50[] = "Single-Wire Operation"; 85 | const char KWP_ELAB_51[] = "Open"; 86 | const char KWP_ELAB_52[] = "Activated"; 87 | 88 | const char* const fault_elaboration_table[] = { 89 | KWP_ELAB_00, KWP_ELAB_01, KWP_ELAB_02, KWP_ELAB_03, KWP_ELAB_04, KWP_ELAB_05, 90 | KWP_ELAB_06, KWP_ELAB_07, KWP_ELAB_08, KWP_ELAB_09, KWP_ELAB_0A, KWP_ELAB_0B, 91 | KWP_ELAB_0C, KWP_ELAB_0D, KWP_ELAB_0E, KWP_ELAB_0F, KWP_ELAB_10, KWP_ELAB_11, 92 | KWP_ELAB_12, KWP_ELAB_13, KWP_ELAB_14, KWP_ELAB_15, KWP_ELAB_16, KWP_ELAB_17, 93 | KWP_ELAB_18, KWP_ELAB_19, KWP_ELAB_1A, KWP_ELAB_1B, KWP_ELAB_1C, KWP_ELAB_1D, 94 | KWP_ELAB_1E, KWP_ELAB_1F, KWP_ELAB_20, KWP_ELAB_21, KWP_ELAB_22, KWP_ELAB_23, 95 | KWP_ELAB_24, KWP_ELAB_25, KWP_ELAB_26, KWP_ELAB_27, KWP_ELAB_28, KWP_ELAB_29, 96 | KWP_ELAB_2A, KWP_ELAB_2B, KWP_ELAB_2C, KWP_ELAB_2D, KWP_ELAB_2E, KWP_ELAB_2F, 97 | KWP_ELAB_30, KWP_ELAB_31, KWP_ELAB_32, KWP_ELAB_33, KWP_ELAB_34, KWP_ELAB_35, 98 | KWP_ELAB_36, KWP_ELAB_37, KWP_ELAB_38, KWP_ELAB_39, KWP_ELAB_3A, KWP_ELAB_3B, 99 | KWP_ELAB_3C, KWP_ELAB_3D, KWP_ELAB_3E, KWP_ELAB_3F, KWP_ELAB_40, KWP_ELAB_41, 100 | KWP_ELAB_42, KWP_ELAB_43, KWP_ELAB_44, KWP_ELAB_45, KWP_ELAB_46, KWP_ELAB_47, 101 | KWP_ELAB_48, KWP_ELAB_49, KWP_ELAB_4A, KWP_ELAB_4B, KWP_ELAB_4C, KWP_ELAB_4D, 102 | KWP_ELAB_4E, KWP_ELAB_4F, KWP_ELAB_50, KWP_ELAB_51, KWP_ELAB_52, 103 | }; 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/fault_code_elaboration_DE.h: -------------------------------------------------------------------------------- 1 | #ifndef FAULT_CODE_ELABORATION_H 2 | #define FAULT_CODE_ELABORATION_H 3 | 4 | const char KWP_ELAB_00[] = "-"; 5 | const char KWP_ELAB_01[] = "Signal an Plus"; 6 | const char KWP_ELAB_02[] = "Signal an Masse"; 7 | const char KWP_ELAB_03[] = "kein Signal"; 8 | const char KWP_ELAB_04[] = "Mechanischer Fehler"; 9 | const char KWP_ELAB_05[] = "Eingang Offen"; 10 | const char KWP_ELAB_06[] = "Signal zu gro" "\xDF" ""; 11 | const char KWP_ELAB_07[] = "Signal zu klein"; 12 | const char KWP_ELAB_08[] = "Regelgrenze " "\xFC" "berschritten"; 13 | const char KWP_ELAB_09[] = "Adaptionsgrenze " "\xFC" "berschritten"; 14 | const char KWP_ELAB_0A[] = "Adaptionsgrenze unterschritten"; 15 | const char KWP_ELAB_0B[] = "Regelgrenze unterschritten"; 16 | const char KWP_ELAB_0C[] = "Adaptionsgrenze ( mul ) " "\xFC" "berschritten"; 17 | const char KWP_ELAB_0D[] = "Adaptionsgrenze ( mul ) unterschritten"; 18 | const char KWP_ELAB_0E[] = "Adaptionsgrenze ( add ) " "\xFC" "berschritten"; 19 | const char KWP_ELAB_0F[] = "Adaptionsgrenze ( add ) unterschritten"; 20 | const char KWP_ELAB_10[] = "Signal au" "\xDF" "erhalb der Toleranz"; 21 | const char KWP_ELAB_11[] = "Regeldifferenz"; 22 | const char KWP_ELAB_12[] = "oberer Anschlagwert"; 23 | const char KWP_ELAB_13[] = "unterer Anschlagwert"; 24 | const char KWP_ELAB_14[] = "Fehler in Grundeinstellung"; 25 | const char KWP_ELAB_15[] = "Zeit f" "\xFC" "r Druckaufbau vorn zu lang"; 26 | const char KWP_ELAB_16[] = "Zeit f" "\xFC" "r Druckabbau vorn zu lang"; 27 | const char KWP_ELAB_17[] = "Zeit f" "\xFC" "r Druckaufbau hinten zu lang"; 28 | const char KWP_ELAB_18[] = "Zeit f" "\xFC" "r Druckabbau hinten zu lang"; 29 | const char KWP_ELAB_19[] = "undefinierter Schalterzustand"; 30 | const char KWP_ELAB_1A[] = "Ausgang Offen"; 31 | const char KWP_ELAB_1B[] = "unplausibles Signal"; 32 | const char KWP_ELAB_1C[] = "Kurzschluss nach Plus"; 33 | const char KWP_ELAB_1D[] = "Kurzschluss nach Masse"; 34 | const char KWP_ELAB_1E[] = "Unterbrechung / Kurzschlu" "\xDF" " nach Plus"; 35 | const char KWP_ELAB_1F[] = "Unterbrechung / Kurzschlu" "\xDF" " nach Masse"; 36 | const char KWP_ELAB_20[] = "Widerstandswert zu gro" "\xDF" ""; 37 | const char KWP_ELAB_21[] = "Widerstandswert zu klein"; 38 | const char KWP_ELAB_22[] = "keine Fehlerart erkannt"; 39 | const char KWP_ELAB_23[] = "-"; 40 | const char KWP_ELAB_24[] = "Unterbrechung"; 41 | const char KWP_ELAB_25[] = "defekt"; 42 | const char KWP_ELAB_26[] = "Ausgang schaltet nicht / KS nach Plus"; 43 | const char KWP_ELAB_27[] = "Ausgang schaltet nicht / KS nach Masse"; 44 | const char KWP_ELAB_28[] = "Kurzschlu" "\xDF" " zu anderem Ventil"; 45 | const char KWP_ELAB_29[] = "Blockiert oder Spannungslos"; 46 | const char KWP_ELAB_2A[] = "Drehzahlabweichung zu gro" "\xDF" ""; 47 | const char KWP_ELAB_2B[] = "geschlossen"; 48 | const char KWP_ELAB_2C[] = "Kurzschluss"; 49 | const char KWP_ELAB_2D[] = "Steckverbindung"; 50 | const char KWP_ELAB_2E[] = "undicht"; 51 | const char KWP_ELAB_2F[] = "falsch Angeschlossen"; 52 | const char KWP_ELAB_30[] = "Spannungsversorgung"; 53 | const char KWP_ELAB_31[] = "keine Kommunikation"; 54 | const char KWP_ELAB_32[] = "Stellung fr" "\xFC" "h nicht erreicht"; 55 | const char KWP_ELAB_33[] = "Stellung sp" "\xE4" "t nicht erreicht"; 56 | const char KWP_ELAB_34[] = "Spannungsversorgung zu gro" "\xDF" ""; 57 | const char KWP_ELAB_35[] = "Spannungsversorgung zu klein"; 58 | const char KWP_ELAB_36[] = "falsche Ausstattung"; 59 | const char KWP_ELAB_37[] = "Adaption nicht erfolgt"; 60 | const char KWP_ELAB_38[] = "im Notlauf"; 61 | const char KWP_ELAB_39[] = "elektr. Fehler im Stromkreis"; 62 | const char KWP_ELAB_3A[] = "verriegelt nicht"; 63 | const char KWP_ELAB_3B[] = "entriegelt nicht"; 64 | const char KWP_ELAB_3C[] = "safed nicht"; 65 | const char KWP_ELAB_3D[] = "entsafed nicht"; 66 | const char KWP_ELAB_3E[] = "keine oder falsche Einstellung"; 67 | const char KWP_ELAB_3F[] = "Temperaturabschaltung"; 68 | const char KWP_ELAB_40[] = "zur Zeit nicht pr" "\xFC" "fbar"; 69 | const char KWP_ELAB_41[] = "nicht berechtigt"; 70 | const char KWP_ELAB_42[] = "Abgleich nicht durchgef" "\xFC" "hrt"; 71 | const char KWP_ELAB_43[] = "Sollwert nicht erreicht"; 72 | const char KWP_ELAB_44[] = "Zylinder 1"; 73 | const char KWP_ELAB_45[] = "Zylinder 2"; 74 | const char KWP_ELAB_46[] = "Zylinder 3"; 75 | const char KWP_ELAB_47[] = "Zylinder 4"; 76 | const char KWP_ELAB_48[] = "Zylinder 5"; 77 | const char KWP_ELAB_49[] = "Zylinder 6"; 78 | const char KWP_ELAB_4A[] = "Zylinder 7"; 79 | const char KWP_ELAB_4B[] = "Zylinder 8"; 80 | const char KWP_ELAB_4C[] = "Klemme 30 fehlt"; 81 | const char KWP_ELAB_4D[] = "interne Spannungsversorgung"; 82 | const char KWP_ELAB_4E[] = "fehlende Botschaften"; 83 | const char KWP_ELAB_4F[] = "bitte Fehlerspeicher auslesen"; 84 | const char KWP_ELAB_50[] = "im Eindrahtbetrieb"; 85 | const char KWP_ELAB_51[] = "offen"; 86 | const char KWP_ELAB_52[] = "ausgel" "\xF6" "st"; 87 | 88 | const char* const fault_elaboration_table[] = { 89 | KWP_ELAB_00, KWP_ELAB_01, KWP_ELAB_02, KWP_ELAB_03, KWP_ELAB_04, KWP_ELAB_05, 90 | KWP_ELAB_06, KWP_ELAB_07, KWP_ELAB_08, KWP_ELAB_09, KWP_ELAB_0A, KWP_ELAB_0B, 91 | KWP_ELAB_0C, KWP_ELAB_0D, KWP_ELAB_0E, KWP_ELAB_0F, KWP_ELAB_10, KWP_ELAB_11, 92 | KWP_ELAB_12, KWP_ELAB_13, KWP_ELAB_14, KWP_ELAB_15, KWP_ELAB_16, KWP_ELAB_17, 93 | KWP_ELAB_18, KWP_ELAB_19, KWP_ELAB_1A, KWP_ELAB_1B, KWP_ELAB_1C, KWP_ELAB_1D, 94 | KWP_ELAB_1E, KWP_ELAB_1F, KWP_ELAB_20, KWP_ELAB_21, KWP_ELAB_22, KWP_ELAB_23, 95 | KWP_ELAB_24, KWP_ELAB_25, KWP_ELAB_26, KWP_ELAB_27, KWP_ELAB_28, KWP_ELAB_29, 96 | KWP_ELAB_2A, KWP_ELAB_2B, KWP_ELAB_2C, KWP_ELAB_2D, KWP_ELAB_2E, KWP_ELAB_2F, 97 | KWP_ELAB_30, KWP_ELAB_31, KWP_ELAB_32, KWP_ELAB_33, KWP_ELAB_34, KWP_ELAB_35, 98 | KWP_ELAB_36, KWP_ELAB_37, KWP_ELAB_38, KWP_ELAB_39, KWP_ELAB_3A, KWP_ELAB_3B, 99 | KWP_ELAB_3C, KWP_ELAB_3D, KWP_ELAB_3E, KWP_ELAB_3F, KWP_ELAB_40, KWP_ELAB_41, 100 | KWP_ELAB_42, KWP_ELAB_43, KWP_ELAB_44, KWP_ELAB_45, KWP_ELAB_46, KWP_ELAB_47, 101 | KWP_ELAB_48, KWP_ELAB_49, KWP_ELAB_4A, KWP_ELAB_4B, KWP_ELAB_4C, KWP_ELAB_4D, 102 | KWP_ELAB_4E, KWP_ELAB_4F, KWP_ELAB_50, KWP_ELAB_51, KWP_ELAB_52, 103 | }; 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/fault_code_elaboration_RO.h: -------------------------------------------------------------------------------- 1 | #ifndef FAULT_CODE_ELABORATION_H 2 | #define FAULT_CODE_ELABORATION_H 3 | 4 | const char KWP_ELAB_00[] = ""; 5 | const char KWP_ELAB_01[] = "Semnal la plus"; 6 | const char KWP_ELAB_02[] = "Semnal la mas" "\xE3" ""; 7 | const char KWP_ELAB_03[] = "lips" "\xE3" " semnal"; 8 | const char KWP_ELAB_04[] = "defect mecanic"; 9 | const char KWP_ELAB_05[] = "Intrare deschis" "\xE3" ""; 10 | const char KWP_ELAB_06[] = "Semnal prea mare"; 11 | const char KWP_ELAB_07[] = "Semnal prea mic"; 12 | const char KWP_ELAB_08[] = "Limita superioar" "\xE3" " de reglaj dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 13 | const char KWP_ELAB_09[] = "Limita superioar" "\xE3" " de adaptare dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 14 | const char KWP_ELAB_0A[] = "Limita inferioar" "\xE3" " de adaptare dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 15 | const char KWP_ELAB_0B[] = "Limita de reglaj dep" "\xE3" "" "\xBA" "it" "\xE3" " " "\xEE" "n jos"; 16 | const char KWP_ELAB_0C[] = "Limita de adaptare (multiplicativ) dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 17 | const char KWP_ELAB_0D[] = "Limita de adaptare (multiplicativ) dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 18 | const char KWP_ELAB_0E[] = "Limita de adaptare (aditiv) dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 19 | const char KWP_ELAB_0F[] = "Limita de adaptare (aditiv) dep" "\xE3" "" "\xBA" "it" "\xE3" ""; 20 | const char KWP_ELAB_10[] = "Semnal " "\xEE" "n afara limitei de toleran" "\xFE" "" "\xE3" ""; 21 | const char KWP_ELAB_11[] = "Diferen" "\xFE" "" "\xE3" " de reglaj"; 22 | const char KWP_ELAB_12[] = "val.limitator superior curs" "\xE3" ""; 23 | const char KWP_ELAB_13[] = "val.limitator inferior curs" "\xE3" ""; 24 | const char KWP_ELAB_14[] = "Eroare " "\xEE" "n setare de baz" "\xE3" ""; 25 | const char KWP_ELAB_15[] = "Timp presurizare fa" "\xFE" "" "\xE3" " prea lung"; 26 | const char KWP_ELAB_16[] = "Timp reducere de presiune fa" "\xFE" "" "\xE3" " prea lung"; 27 | const char KWP_ELAB_17[] = "Timp de presurizare spate prea lung"; 28 | const char KWP_ELAB_18[] = "Timp reducere de presiune spate prea lung"; 29 | const char KWP_ELAB_19[] = "Stare comutator nedefinit" "\xE3" ""; 30 | const char KWP_ELAB_1A[] = "Ie" "\xBA" "ire deschis" "\xE3" ""; 31 | const char KWP_ELAB_1B[] = "Semnal neplauzibil"; 32 | const char KWP_ELAB_1C[] = "Scurt la plus"; 33 | const char KWP_ELAB_1D[] = "scurt la mas" "\xE3" ""; 34 | const char KWP_ELAB_1E[] = "" "\xCE" "ntrerupere/scurt la plus"; 35 | const char KWP_ELAB_1F[] = "" "\xCE" "ntrerupere/scurt la mas" "\xE3" ""; 36 | const char KWP_ELAB_20[] = "Valoare rezisten" "\xFE" "" "\xE3" " prea mare"; 37 | const char KWP_ELAB_21[] = "Valoare rezisten" "\xFE" "" "\xE3" " prea mic" "\xE3" ""; 38 | const char KWP_ELAB_22[] = "Nu este detectat tipul de eroare"; 39 | const char KWP_ELAB_23[] = ""; 40 | const char KWP_ELAB_24[] = "" "\xEE" "ntrerupere"; 41 | const char KWP_ELAB_25[] = "defect"; 42 | const char KWP_ELAB_26[] = "Ie" "\xBA" "irea nu comut" "\xE3" "/scurt la plus"; 43 | const char KWP_ELAB_27[] = "ie" "\xBA" "irea nu comut" "\xE3" "/scurt la mas" "\xE3" ""; 44 | const char KWP_ELAB_28[] = "Scurt la un alt ventil"; 45 | const char KWP_ELAB_29[] = "Blocat sau lips" "\xE3" " tensiune"; 46 | const char KWP_ELAB_2A[] = "Abaterea de tura" "\xFE" "ie prea mare"; 47 | const char KWP_ELAB_2B[] = "" "\xEE" "nchis"; 48 | const char KWP_ELAB_2C[] = "Scurtcircuit"; 49 | const char KWP_ELAB_2D[] = "Conector"; 50 | const char KWP_ELAB_2E[] = "inetan" "\xBA" ""; 51 | const char KWP_ELAB_2F[] = "conectat incorect"; 52 | const char KWP_ELAB_30[] = "Alimentare cu tensiune"; 53 | const char KWP_ELAB_31[] = "lips" "\xE3" " comunica" "\xFE" "ie"; 54 | const char KWP_ELAB_32[] = "Pozi" "\xFE" "ia reglaj '" "\xEE" "n avans' neatins" "\xE3" ""; 55 | const char KWP_ELAB_33[] = "Pozi" "\xFE" "ia reglaj '" "\xEE" "n devans' neatins" "\xE3" ""; 56 | const char KWP_ELAB_34[] = "Tensiune de alimentare prea mare"; 57 | const char KWP_ELAB_35[] = "Tensiune de alimentare prea mic" "\xE3" ""; 58 | const char KWP_ELAB_36[] = "echipare incorect" "\xE3" ""; 59 | const char KWP_ELAB_37[] = "Adaptarea nu a fost realizat" "\xE3" ""; 60 | const char KWP_ELAB_38[] = "" "\xCE" "n program de urgen" "\xFE" "" "\xE3" ""; 61 | const char KWP_ELAB_39[] = "Defect de circuit electric"; 62 | const char KWP_ELAB_3A[] = "nu se blocheaz" "\xE3" ""; 63 | const char KWP_ELAB_3B[] = "nu se deblocheaz" "\xE3" ""; 64 | const char KWP_ELAB_3C[] = "SAFE nu este conectat"; 65 | const char KWP_ELAB_3D[] = "SAFE nu este deconectat"; 66 | const char KWP_ELAB_3E[] = "Nu este reglat sau incorect reglat"; 67 | const char KWP_ELAB_3F[] = "Decuplare termic" "\xE3" ""; 68 | const char KWP_ELAB_40[] = "Momentan neaccesibil"; 69 | const char KWP_ELAB_41[] = "nepermis"; 70 | const char KWP_ELAB_42[] = "Adaptarea nu este efectuat" "\xE3" ""; 71 | const char KWP_ELAB_43[] = "Valoarea prescris" "\xE3" " nu este atins" "\xE3" ""; 72 | const char KWP_ELAB_44[] = "Cilindru 1"; 73 | const char KWP_ELAB_45[] = "Cilindru 2"; 74 | const char KWP_ELAB_46[] = "Cilindru 3"; 75 | const char KWP_ELAB_47[] = "Cilindru 4"; 76 | const char KWP_ELAB_48[] = "Cilindru 5"; 77 | const char KWP_ELAB_49[] = "Cilindru 6"; 78 | const char KWP_ELAB_4A[] = "Cilindru 7"; 79 | const char KWP_ELAB_4B[] = "Cilindru 8"; 80 | const char KWP_ELAB_4C[] = "Terminal 30 lips" "\xE3" ""; 81 | const char KWP_ELAB_4D[] = "alimentare intern" "\xE3" " cu tensiune"; 82 | const char KWP_ELAB_4E[] = "Lips" "\xE3" " mesaje"; 83 | const char KWP_ELAB_4F[] = "Citi" "\xFE" "i v" "\xE3" " rog memoria de defecte"; 84 | const char KWP_ELAB_50[] = "" "\xEE" "n regim 1fir"; 85 | const char KWP_ELAB_51[] = "deschis"; 86 | const char KWP_ELAB_52[] = "Declan" "\xBA" "at "; 87 | 88 | const char* const fault_elaboration_table[] = { 89 | KWP_ELAB_00, KWP_ELAB_01, KWP_ELAB_02, KWP_ELAB_03, KWP_ELAB_04, KWP_ELAB_05, 90 | KWP_ELAB_06, KWP_ELAB_07, KWP_ELAB_08, KWP_ELAB_09, KWP_ELAB_0A, KWP_ELAB_0B, 91 | KWP_ELAB_0C, KWP_ELAB_0D, KWP_ELAB_0E, KWP_ELAB_0F, KWP_ELAB_10, KWP_ELAB_11, 92 | KWP_ELAB_12, KWP_ELAB_13, KWP_ELAB_14, KWP_ELAB_15, KWP_ELAB_16, KWP_ELAB_17, 93 | KWP_ELAB_18, KWP_ELAB_19, KWP_ELAB_1A, KWP_ELAB_1B, KWP_ELAB_1C, KWP_ELAB_1D, 94 | KWP_ELAB_1E, KWP_ELAB_1F, KWP_ELAB_20, KWP_ELAB_21, KWP_ELAB_22, KWP_ELAB_23, 95 | KWP_ELAB_24, KWP_ELAB_25, KWP_ELAB_26, KWP_ELAB_27, KWP_ELAB_28, KWP_ELAB_29, 96 | KWP_ELAB_2A, KWP_ELAB_2B, KWP_ELAB_2C, KWP_ELAB_2D, KWP_ELAB_2E, KWP_ELAB_2F, 97 | KWP_ELAB_30, KWP_ELAB_31, KWP_ELAB_32, KWP_ELAB_33, KWP_ELAB_34, KWP_ELAB_35, 98 | KWP_ELAB_36, KWP_ELAB_37, KWP_ELAB_38, KWP_ELAB_39, KWP_ELAB_3A, KWP_ELAB_3B, 99 | KWP_ELAB_3C, KWP_ELAB_3D, KWP_ELAB_3E, KWP_ELAB_3F, KWP_ELAB_40, KWP_ELAB_41, 100 | KWP_ELAB_42, KWP_ELAB_43, KWP_ELAB_44, KWP_ELAB_45, KWP_ELAB_46, KWP_ELAB_47, 101 | KWP_ELAB_48, KWP_ELAB_49, KWP_ELAB_4A, KWP_ELAB_4B, KWP_ELAB_4C, KWP_ELAB_4D, 102 | KWP_ELAB_4E, KWP_ELAB_4F, KWP_ELAB_50, KWP_ELAB_51, KWP_ELAB_52, 103 | }; 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/fault_code_elaboration_PL.h: -------------------------------------------------------------------------------- 1 | #ifndef FAULT_CODE_ELABORATION_H 2 | #define FAULT_CODE_ELABORATION_H 3 | 4 | const char KWP_ELAB_00[] = "-"; 5 | const char KWP_ELAB_01[] = "Sygna" "\xB3" " zwarty z plusem"; 6 | const char KWP_ELAB_02[] = "Sygna" "\xB3" " zwarty z mas" "\xB9" ""; 7 | const char KWP_ELAB_03[] = "Brak sygna" "\xB3" "u"; 8 | const char KWP_ELAB_04[] = "Usterka mechaniczna"; 9 | const char KWP_ELAB_05[] = "Wej" "\x9C" "cie otwarte"; 10 | const char KWP_ELAB_06[] = "Za wysoki sygna" "\xB3" ""; 11 | const char KWP_ELAB_07[] = "Za niski sygna" "\xB3" ""; 12 | const char KWP_ELAB_08[] = "Granica regulacji przekroczona"; 13 | const char KWP_ELAB_09[] = "Warto" "\x9C" "" "\xE6" " skrajna adaptacji przekroczona"; 14 | const char KWP_ELAB_0A[] = "Warto" "\x9C" "" "\xE6" " skrajna adaptacji nieosi" "\xB9" "gni" "\xEA" "ta"; 15 | const char KWP_ELAB_0B[] = "Granica regulacji nieosi" "\xB9" "gni" "\xEA" "ta"; 16 | const char KWP_ELAB_0C[] = "Warto" "\x9C" "" "\xE6" " skrajna adaptacji przekroczona (przy obci" "\xB9" "" "\xBF" "eniu)"; 17 | const char KWP_ELAB_0D[] = "Warto" "\x9C" "" "\xE6" " skrajna adaptacji nieosi" "\xB9" "gni" "\xEA" "ta (przy obci" "\xB9" "" "\xBF" "eniu)"; 18 | const char KWP_ELAB_0E[] = "Warto" "\x9C" "" "\xE6" " skrajna adaptacji przekroczona (na biegu ja" "\xB3" "owym)"; 19 | const char KWP_ELAB_0F[] = "Warto" "\x9C" "" "\xE6" " skrajna adaptacji nieosi" "\xB9" "gni" "\xEA" "ta (na biegu ja" "\xB3" "owym)"; 20 | const char KWP_ELAB_10[] = "Sygna" "\xB3" " poza tolerancj" "\xB9" ""; 21 | const char KWP_ELAB_11[] = "R" "\xF3" "" "\xBF" "nica regulacji"; 22 | const char KWP_ELAB_12[] = "G" "\xF3" "rna warto" "\x9C" "" "\xE6" " skrajna"; 23 | const char KWP_ELAB_13[] = "Dolna warto" "\x9C" "" "\xE6" " skrajna"; 24 | const char KWP_ELAB_14[] = "B" "\xB3" "" "\xB9" "d w nastawach podstawowych"; 25 | const char KWP_ELAB_15[] = "Czas narastania ci" "\x9C" "nienia z przodu za d" "\xB3" "ugi"; 26 | const char KWP_ELAB_16[] = "Czas redukcji ci" "\x9C" "nienia z przodu za d" "\xB3" "ugi"; 27 | const char KWP_ELAB_17[] = "Czas narastania ci" "\x9C" "nienia z ty" "\xB3" "u za d" "\xB3" "ugi"; 28 | const char KWP_ELAB_18[] = "Czas redukcji ci" "\x9C" "nienia z ty" "\xB3" "u za d" "\xB3" "ugi"; 29 | const char KWP_ELAB_19[] = "Niezdefiniowany stan prze" "\xB3" "" "\xB9" "cznika"; 30 | const char KWP_ELAB_1A[] = "Wyj" "\x9C" "cie otwarte"; 31 | const char KWP_ELAB_1B[] = "Niezrozumia" "\xB3" "y sygna" "\xB3" ""; 32 | const char KWP_ELAB_1C[] = "Zwarcie z plusem"; 33 | const char KWP_ELAB_1D[] = "Zwarcie z mas" "\xB9" ""; 34 | const char KWP_ELAB_1E[] = "Przerwa / zwarcie z plusem"; 35 | const char KWP_ELAB_1F[] = "Przerwa / zwarcie z mas" "\xB9" ""; 36 | const char KWP_ELAB_20[] = "Za du" "\xBF" "a warto" "\x9C" "" "\xE6" " rezystancji"; 37 | const char KWP_ELAB_21[] = "Za ma" "\xB3" "a warto" "\x9C" "" "\xE6" " rezystancji"; 38 | const char KWP_ELAB_22[] = "Nie rozpoznano rodzaju usterki"; 39 | const char KWP_ELAB_23[] = "-"; 40 | const char KWP_ELAB_24[] = "Przerwa w obwodzie"; 41 | const char KWP_ELAB_25[] = "uszkodzony"; 42 | const char KWP_ELAB_26[] = "Wyj" "\x9C" "cie nie prze" "\xB3" "" "\xB9" "cza / zwarcie z plusem"; 43 | const char KWP_ELAB_27[] = "Wyj" "\x9C" "cie nie prze" "\xB3" "" "\xB9" "cza / zwarcie z mas" "\xB9" ""; 44 | const char KWP_ELAB_28[] = "Zwarcie do innego wyj" "\x9C" "cia"; 45 | const char KWP_ELAB_29[] = "Zablokowany lub bez napi" "\xEA" "cia"; 46 | const char KWP_ELAB_2A[] = "Za du" "\xBF" "e odchylenie pr" "\xEA" "dko" "\x9C" "ci"; 47 | const char KWP_ELAB_2B[] = "zamkni" "\xEA" "ty"; 48 | const char KWP_ELAB_2C[] = "zwarcie"; 49 | const char KWP_ELAB_2D[] = "Z" "\xB3" "" "\xB9" "cze wtykowe"; 50 | const char KWP_ELAB_2E[] = "Nieszczelno" "\x9C" "" "\xE6" ""; 51 | const char KWP_ELAB_2F[] = "Nieprawid" "\xB3" "owo pod" "\xB3" "" "\xB9" "czone"; 52 | const char KWP_ELAB_30[] = "Napi" "\xEA" "cie zasilaj" "\xB9" "ce"; 53 | const char KWP_ELAB_31[] = "Brak komunikacji"; 54 | const char KWP_ELAB_32[] = "Po" "\xB3" "o" "\xBF" "enie przyspieszone nieosi" "\xB9" "gni" "\xEA" "te"; 55 | const char KWP_ELAB_33[] = "Po" "\xB3" "o" "\xBF" "enie op" "\xF3" "" "\x9F" "nione nieosi" "\xB9" "gni" "\xEA" "te"; 56 | const char KWP_ELAB_34[] = "Za wysokie napi" "\xEA" "cie zasilaj" "\xB9" "ce"; 57 | const char KWP_ELAB_35[] = "Za ma" "\xB3" "a warto" "\x9C" "" "\xE6" " napi" "\xEA" "cia zasilaj" "\xB9" "cego"; 58 | const char KWP_ELAB_36[] = "Niew" "\xB3" "a" "\x9C" "ciwe wyposa" "\xBF" "enie"; 59 | const char KWP_ELAB_37[] = "Nieudana adaptacja"; 60 | const char KWP_ELAB_38[] = "W trybie awaryjnym (Limp-Home Mode)"; 61 | const char KWP_ELAB_39[] = "Usterka w obwodzie elektrycznym"; 62 | const char KWP_ELAB_3A[] = "Nie rygluje"; 63 | const char KWP_ELAB_3B[] = "Nie odryglowuje"; 64 | const char KWP_ELAB_3C[] = "Nie zabezpiecza"; 65 | const char KWP_ELAB_3D[] = "Nie odbezpiecza"; 66 | const char KWP_ELAB_3E[] = "Brak lub niepoprawne ustawienia"; 67 | const char KWP_ELAB_3F[] = "Wy" "\xB3" "" "\xB9" "czenie z powodu wysokiej temperatury"; 68 | const char KWP_ELAB_40[] = "Aktualnie nie mo" "\xBF" "na sprawdzi" "\xE6" ""; 69 | const char KWP_ELAB_41[] = "Nieuprawniony"; 70 | const char KWP_ELAB_42[] = "Niedopasowane"; 71 | const char KWP_ELAB_43[] = "Nieosi" "\xB9" "gni" "\xEA" "ta wymagana warto" "\x9C" "" "\xE6" ""; 72 | const char KWP_ELAB_44[] = "Cylinder 1"; 73 | const char KWP_ELAB_45[] = "Cylinder 2"; 74 | const char KWP_ELAB_46[] = "Cylinder 3"; 75 | const char KWP_ELAB_47[] = "Cylinder 4"; 76 | const char KWP_ELAB_48[] = "Cylinder 5"; 77 | const char KWP_ELAB_49[] = "Cylinder 6"; 78 | const char KWP_ELAB_4A[] = "Cylinder 7"; 79 | const char KWP_ELAB_4B[] = "Cylinder 8"; 80 | const char KWP_ELAB_4C[] = "Brak zacisku 30"; 81 | const char KWP_ELAB_4D[] = "Wewn" "\xEA" "trzne napi" "\xEA" "cie zasilaj" "\xB9" "ce"; 82 | const char KWP_ELAB_4E[] = "Brakuj" "\xB9" "ce informacje"; 83 | const char KWP_ELAB_4F[] = "Odczyta" "\xE6" " pami" "\xEA" "" "\xE6" " usterek"; 84 | const char KWP_ELAB_50[] = "W jednoprzewodowym trybie pracy"; 85 | const char KWP_ELAB_51[] = "otwarty"; 86 | const char KWP_ELAB_52[] = "Aktywny"; 87 | 88 | const char* const fault_elaboration_table[] = { 89 | KWP_ELAB_00, KWP_ELAB_01, KWP_ELAB_02, KWP_ELAB_03, KWP_ELAB_04, KWP_ELAB_05, 90 | KWP_ELAB_06, KWP_ELAB_07, KWP_ELAB_08, KWP_ELAB_09, KWP_ELAB_0A, KWP_ELAB_0B, 91 | KWP_ELAB_0C, KWP_ELAB_0D, KWP_ELAB_0E, KWP_ELAB_0F, KWP_ELAB_10, KWP_ELAB_11, 92 | KWP_ELAB_12, KWP_ELAB_13, KWP_ELAB_14, KWP_ELAB_15, KWP_ELAB_16, KWP_ELAB_17, 93 | KWP_ELAB_18, KWP_ELAB_19, KWP_ELAB_1A, KWP_ELAB_1B, KWP_ELAB_1C, KWP_ELAB_1D, 94 | KWP_ELAB_1E, KWP_ELAB_1F, KWP_ELAB_20, KWP_ELAB_21, KWP_ELAB_22, KWP_ELAB_23, 95 | KWP_ELAB_24, KWP_ELAB_25, KWP_ELAB_26, KWP_ELAB_27, KWP_ELAB_28, KWP_ELAB_29, 96 | KWP_ELAB_2A, KWP_ELAB_2B, KWP_ELAB_2C, KWP_ELAB_2D, KWP_ELAB_2E, KWP_ELAB_2F, 97 | KWP_ELAB_30, KWP_ELAB_31, KWP_ELAB_32, KWP_ELAB_33, KWP_ELAB_34, KWP_ELAB_35, 98 | KWP_ELAB_36, KWP_ELAB_37, KWP_ELAB_38, KWP_ELAB_39, KWP_ELAB_3A, KWP_ELAB_3B, 99 | KWP_ELAB_3C, KWP_ELAB_3D, KWP_ELAB_3E, KWP_ELAB_3F, KWP_ELAB_40, KWP_ELAB_41, 100 | KWP_ELAB_42, KWP_ELAB_43, KWP_ELAB_44, KWP_ELAB_45, KWP_ELAB_46, KWP_ELAB_47, 101 | KWP_ELAB_48, KWP_ELAB_49, KWP_ELAB_4A, KWP_ELAB_4B, KWP_ELAB_4C, KWP_ELAB_4D, 102 | KWP_ELAB_4E, KWP_ELAB_4F, KWP_ELAB_50, KWP_ELAB_51, KWP_ELAB_52, 103 | }; 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /examples/02.Fault_code_test/02.Fault_code_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Title: 3 | 02.Fault_code_test.ino 4 | 5 | Description: 6 | Demonstrates how to read a module's fault codes and their elaborations. 7 | 8 | Notes: 9 | *The connection will be stopped after the fault codes are read. 10 | *If you have fault code descriptions enabled (KWP1281_FAULT_CODE_DESCRIPTION_SUPPORTED/KWP1281_OBD_FAULT_CODE_DESCRIPTION_SUPPORTED), 11 | you can choose from a few different languages a bit further below in "KLineKWP1281Lib.h". 12 | *Please only choose one option. 13 | *If they are disabled, the descriptions will be replaced by "EN_dsc"/"EN_obd". 14 | *If you have fault code elaborations enabled (KWP1281_FAULT_CODE_ELABORATION_SUPPORTED), you can choose from a few different languages 15 | a bit further below in "KLineKWP1281Lib.h". 16 | *Please only choose one option. 17 | *If they are disabled, the elaborations will be replaced by "EN_elb". 18 | */ 19 | 20 | /* 21 | ***Uncomment the appropriate options in the "configuration.h" file! 22 | 23 | ESP32 24 | *has two additional serial ports 25 | *pins (they can be remapped, this is what they are configured to in these examples): 26 | *K-line TX -> RX pin 16 27 | *K-line RX -> TX pin 17 28 | 29 | ESP32-C6 30 | *has one additional serial port 31 | *pins (they can be remapped, this is what they are configured to in these examples): 32 | *K-line TX -> RX pin 16 33 | *K-line RX -> TX pin 17 34 | */ 35 | 36 | // Include the library. 37 | #include 38 | 39 | // Include the two files containing configuration options and the functions used for communication. 40 | #include "configuration.h" 41 | #include "communication.h" 42 | 43 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 44 | KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin); 45 | 46 | // Each fault code takes up 3 bytes. 47 | // You can increase the value below if you get "Too many faults for the given buffer size" during the test. 48 | #define DTC_BUFFER_MAX_FAULT_CODES 16 49 | uint8_t faults[3 * DTC_BUFFER_MAX_FAULT_CODES]; 50 | 51 | void setup() 52 | { 53 | // Initialize the Serial Monitor. 54 | Serial.begin(115200); 55 | delay(500); 56 | printf("Sketch started.\n"); 57 | 58 | // If debugging bus traffic was enabled, attach the debugging function. 59 | #if debug_traffic 60 | diag.KWP1281debugFunction(KWP1281debugFunction); 61 | #endif 62 | 63 | // Create the task that runs this demo, attaching the handle defined in "configuration.h". 64 | assert(xTaskCreate(demo_task, "demo", 4096, NULL, 5, &demo_task_handle)); 65 | } 66 | 67 | void loop() 68 | { 69 | // The Arduino loop is not used; delete its task. 70 | vTaskDelete(NULL); 71 | } 72 | 73 | void demo_task(void *args) 74 | { 75 | // Connect to the module. 76 | /* 77 | *There is an optional argument for connect()/attemptConnect(), which, if set to false, will make it so extra identification will not be requested 78 | if the module supports it. 79 | *This would save almost one second of the connection time, with the downside being that getExtraIdentification() will return an empty string. 80 | *It has no effect if the module doesn't support extra identification. 81 | */ 82 | diag.connect(connect_to_module, module_baud_rate, false); 83 | 84 | // Show the module's fault codes (procedure moved to a function). 85 | printf("Requesting fault codes.\n"); 86 | showDTCs(); 87 | 88 | // Disconnect from the module. 89 | /* 90 | *There is an optional argument for disconnect(), which, if set to false, will make it so the library doesn't wait for a response. 91 | *When disconnecting, some modules send a response, some don't. 92 | *If the module doesn't send a response, not waiting saves [diag.responseTimeout] milliseconds, by default 2 seconds. 93 | *If the module sends a response, not waiting for it shouldn't really have consequences. 94 | */ 95 | diag.disconnect(false); 96 | printf("Disconnected.\n"); 97 | 98 | // A task must either have an infinite loop, or delete itself. 99 | vTaskDelete(NULL); 100 | } 101 | 102 | void showDTCs() 103 | { 104 | // This will contain the amount of fault codes, after calling the readFaults() function. 105 | uint8_t amount_of_fault_codes = 0; 106 | 107 | /* 108 | Always check the return value of functions! 109 | 110 | The readFaults() function can return: 111 | *KLineKWP1281Lib::SUCCESS - received fault codes 112 | *KLineKWP1281Lib::FAIL - the module doesn't support fault codes 113 | *KLineKWP1281Lib::ERROR - communication error 114 | */ 115 | 116 | // If fault codes were read successfully, display them. 117 | if (diag.readFaults(amount_of_fault_codes, faults, sizeof(faults)) == KLineKWP1281Lib::SUCCESS) 118 | { 119 | // Display how many fault codes were received. 120 | printf("DTCs: %d\n", amount_of_fault_codes); 121 | 122 | // Each fault code takes up 3 bytes. 123 | // Check if the buffer was large enough for all fault codes. 124 | // If it wasn't, only attempt to display as many as fit. 125 | uint8_t available_fault_codes = amount_of_fault_codes; 126 | if (sizeof(faults) < amount_of_fault_codes * 3) 127 | { 128 | printf("Too many faults for the given buffer size"); 129 | available_fault_codes = sizeof(faults) / 3; 130 | } 131 | 132 | // Print each fault code. 133 | for (uint8_t i = 0; i < available_fault_codes; i++) 134 | { 135 | // Retrieve the "i"-th fault code and the elaboration code from the buffer. 136 | uint16_t fault_code = KLineKWP1281Lib::getFaultCode(i, available_fault_codes, faults, sizeof(faults)); 137 | uint8_t fault_elaboration_code = KLineKWP1281Lib::getFaultElaborationCode(i, available_fault_codes, faults, sizeof(faults)); 138 | 139 | // Declare a character array and use it to store the description string of the fault code. 140 | char description_string[32]; 141 | KLineKWP1281Lib::getFaultDescription(fault_code, description_string, sizeof(description_string)); 142 | 143 | // Print the fault code, description string. 144 | printf(" %05u - %s", fault_code, description_string); 145 | 146 | // Get the full length of the description string, to warn the user if the provided buffer wasn't large enough to store the entire string. 147 | size_t description_string_length = KLineKWP1281Lib::getFaultDescriptionLength(fault_code); 148 | 149 | // If the buffer was too small, display an ellipsis and indicate how many characters would have been needed for the entire string. 150 | if (description_string_length > (sizeof(description_string) - 1)) 151 | { 152 | printf("... (%d/%d)", sizeof(description_string) - 1, description_string_length); 153 | } 154 | printf("\n"); 155 | 156 | // Store the elaboration code of the fault (without the high bit) in a string. 157 | char fault_elaboration_code_string[8]; 158 | sprintf(fault_elaboration_code_string, "%02u-%02u", fault_elaboration_code & ~0x80, ((fault_elaboration_code & 0x80) ? 10 : 0)); 159 | 160 | // This will indicate whether or not the fault is intermittent, after calling the getFaultElaboration() function. 161 | bool is_intermittent; 162 | 163 | // Declare a character array and use it to store the elaboration string. 164 | char elaboration_string[32]; 165 | KLineKWP1281Lib::getFaultElaboration(is_intermittent, fault_elaboration_code, elaboration_string, sizeof(elaboration_string)); 166 | 167 | // A fault code may or may not be of "OBD" type. 168 | // OBD fault codes should be displayed formatted according to the standard, for example "P0001" instead of "16385". 169 | if (KLineKWP1281Lib::isOBDFaultCode(fault_code)) 170 | { 171 | // Declare a character array and use it to store the formatted fault code. 172 | char OBD_fault_code_string[6]; 173 | KLineKWP1281Lib::getOBDFaultCode(fault_code, OBD_fault_code_string, sizeof(OBD_fault_code_string)); 174 | 175 | // Print the formatted fault code and elaboration code. 176 | printf(" %s - %s", OBD_fault_code_string, fault_elaboration_code_string); 177 | 178 | // The elaboration produced by getFaultElaboration() may not be accurate for OBD fault codes. 179 | // The proper elaboration is contained in the description string, after the last ':' character. 180 | } 181 | else 182 | { 183 | // Print the fault elaboration code and elaboration text. 184 | printf(" %s - %s", fault_elaboration_code_string, elaboration_string); 185 | 186 | // Get the full length of the elaboration string, to warn the user if the provided buffer wasn't large enough to store the entire string. 187 | size_t elaboration_string_length = KLineKWP1281Lib::getFaultElaborationLength(i, available_fault_codes, faults, sizeof(faults)); 188 | 189 | // If the buffer was too small, display an ellipsis and indicate how many characters would have been needed for the entire string. 190 | if (elaboration_string_length > (sizeof(elaboration_string) - 1)) 191 | { 192 | printf("... (%d/%d)", sizeof(elaboration_string) - 1, elaboration_string_length); 193 | } 194 | } 195 | 196 | // If the fault is intermittent, display this info. 197 | if (is_intermittent) 198 | { 199 | printf(" - Intermittent"); 200 | } 201 | printf("\n"); 202 | } 203 | } 204 | // If fault codes were not read successfully, show an error. 205 | else 206 | { 207 | printf("Error reading DTCs\n"); 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /examples/05.Single_measurement_test/05.Single_measurement_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Title: 3 | 05.Single_measurement_test.ino 4 | 5 | Description: 6 | Demonstrates how to read a single measurement continuously. 7 | 8 | Notes: 9 | *You can change which group and which measurement index to read by modifying the #defines below. 10 | *This measurement will be read continuously while the sketch is running. 11 | *It is not necessary to maintain the connection with "diag.update();" in the loop, because any request will have the same effect (update() must 12 | be used in periods of inactivity). 13 | */ 14 | 15 | // Change which measurement to read, from which group. 16 | #define GROUP_TO_READ 1 // valid range: 0-255 17 | #define INDEX_TO_READ 0 // valid range: 0-3 18 | 19 | /* 20 | ***Uncomment the appropriate options in the "configuration.h" file! 21 | 22 | ESP32 23 | *has two additional serial ports 24 | *pins (they can be remapped, this is what they are configured to in these examples): 25 | *K-line TX -> RX pin 16 26 | *K-line RX -> TX pin 17 27 | 28 | ESP32-C6 29 | *has one additional serial port 30 | *pins (they can be remapped, this is what they are configured to in these examples): 31 | *K-line TX -> RX pin 16 32 | *K-line RX -> TX pin 17 33 | */ 34 | 35 | // Include the library. 36 | #include 37 | 38 | // Include the two files containing configuration options and the functions used for communication. 39 | #include "configuration.h" 40 | #include "communication.h" 41 | 42 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 43 | KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin); 44 | 45 | // Please find more information on the topic of KWP1281 measurement groups in the "03.Full_measurement_test" example. 46 | uint8_t measurement_buffer[80]; 47 | uint8_t measurement_body_buffer[4]; 48 | 49 | void setup() 50 | { 51 | // Initialize the Serial Monitor. 52 | Serial.begin(115200); 53 | delay(500); 54 | printf("Sketch started.\n"); 55 | 56 | // If debugging bus traffic was enabled, attach the debugging function. 57 | #if debug_traffic 58 | diag.KWP1281debugFunction(KWP1281debugFunction); 59 | #endif 60 | 61 | // Create the task that runs this demo, attaching the handle defined in "configuration.h". 62 | assert(xTaskCreate(demo_task, "demo", 4096, NULL, 5, &demo_task_handle)); 63 | } 64 | 65 | void loop() 66 | { 67 | // The Arduino loop is not used; delete its task. 68 | vTaskDelete(NULL); 69 | } 70 | 71 | void demo_task(void *args) 72 | { 73 | // Connect to the module. 74 | diag.connect(connect_to_module, module_baud_rate, false); 75 | 76 | // A task must either have an infinite loop, or delete itself. 77 | printf("Requesting group %d, measurement index %d continuously.\n", GROUP_TO_READ, INDEX_TO_READ); 78 | while (true) 79 | { 80 | // Show the requested parameter in the requested group. 81 | showSingleMeasurement(GROUP_TO_READ, INDEX_TO_READ); 82 | } 83 | } 84 | 85 | /* 86 | 87 | -The variables used for "header+body" groups are stored globally. 88 | -Normally, the flag `received_group_header` should be reset to false when requesting a new group. 89 | -This example only requests one group, so it isn't necessary; if the group was "header+body" once, it will always be like that. 90 | 91 | -You could also keep `received_group_header` local to the showSingleMeasurement() function, and do diag.update() before each request, 92 | to ensure you receive the [Header]. 93 | -Of course, this would negatively impact performance, since you need to receive more data from the module each time you read the group. 94 | 95 | */ 96 | 97 | // This flag keeps track if a [Header] was received for the current group, meaning it's of the "header+body" type. 98 | bool received_group_header = false; 99 | 100 | // This will contain the amount of measurements received in the [Header] of a "header+body" group. 101 | uint8_t amount_of_measurements_in_header = 0; 102 | 103 | void showSingleMeasurement(uint8_t group, uint8_t measurement_index) 104 | { 105 | // This will contain the amount of measurements in the current group, after calling the readGroup() function. 106 | uint8_t amount_of_measurements = 0; 107 | 108 | /* 109 | -For modules which report measuring groups in the "header+body" mode, it is important to do update() when requesting a new group, 110 | so the module sends the [Header]. 111 | -If it's the first request, it's not necessary; the module will always send the [Header] for the first time. 112 | -Since this example only requests one group, it would decrease performance (measurement rate) to do update() every time, since it would cause the module 113 | to send the [Header] over and over again, and it takes time to receive it, instead of only receiving it once, at the beginning. 114 | */ 115 | // diag.update(); 116 | 117 | /* 118 | The readGroup() function can return: 119 | KLineKWP1281Lib::FAIL - the requested group does not exist 120 | KLineKWP1281Lib::ERROR - communication error 121 | KLineKWP1281Lib::SUCCESS - received measurements 122 | KLineKWP1281Lib::GROUP_BASIC_SETTING - received a [Basic settings] measurement; the buffer contains 10 raw values 123 | KLineKWP1281Lib::GROUP_HEADER - received the [Header] for a "header+body" group; need to read again to get the [Body] 124 | KLineKWP1281Lib::GROUP_BODY - received the [Body] for a "header+body" group 125 | */ 126 | 127 | // Read the requested group and store the returned value. 128 | KLineKWP1281Lib::executionStatus readGroup_status; 129 | // If the group is not of "header+body" type, or if it is and this is the first request, we don't have a [Header] (yet), so `received_group_header=false`. 130 | // The response to this request will be stored in the larger array. 131 | // If it is in fact of "header+body" type, the [Header] will be stored in this array. 132 | if (!received_group_header) 133 | { 134 | readGroup_status = diag.readGroup(amount_of_measurements, group, measurement_buffer, sizeof(measurement_buffer)); 135 | } 136 | // If the group is of "header+body" type, and this is not the first request, it means we have a header, so `received_group_header=true`. 137 | // The response to this request will be stored in the smaller array, because it should be the [Body]. 138 | else 139 | { 140 | readGroup_status = diag.readGroup(amount_of_measurements, group, measurement_body_buffer, sizeof(measurement_body_buffer)); 141 | } 142 | 143 | // Check the returned value. 144 | switch (readGroup_status) 145 | { 146 | case KLineKWP1281Lib::ERROR: 147 | { 148 | printf("Error reading group!\n"); 149 | } 150 | // There is no reason to continue, exit the function. 151 | return; 152 | 153 | case KLineKWP1281Lib::FAIL: 154 | { 155 | printf("Group %d does not exist!\n", group); 156 | } 157 | // There is no reason to continue, exit the function. 158 | return; 159 | 160 | case KLineKWP1281Lib::GROUP_BASIC_SETTINGS: 161 | { 162 | // If we have a [Header], it means this group sends responses of "header+body" type. 163 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 164 | if (received_group_header) 165 | { 166 | printf("Error reading body! (got basic settings)\n"); 167 | return; 168 | } 169 | 170 | // We have 10 raw values in the `measurement_buffer` array. 171 | printf("Basic settings in group %d: ", group); 172 | for (uint8_t i = 0; i < 10; i++) 173 | { 174 | printf("%d ", measurement_buffer[i]); 175 | } 176 | printf("\n"); 177 | } 178 | // We have nothing else to display, exit the function. 179 | return; 180 | 181 | case KLineKWP1281Lib::GROUP_HEADER: 182 | { 183 | // If we have a [Header], it means this group sends responses of "header+body" type. 184 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 185 | if (received_group_header) 186 | { 187 | printf("Error reading body! (got header)\n"); 188 | return; 189 | } 190 | 191 | // Set the flag to indicate that a header was received, making this a "header+body" group response. 192 | received_group_header = true; 193 | 194 | // Store the number of measurements received in the header. 195 | amount_of_measurements_in_header = amount_of_measurements; 196 | } 197 | // We have nothing to display yet, the next readGroup() will get the actual data; exit the function. 198 | return; 199 | 200 | case KLineKWP1281Lib::GROUP_BODY: 201 | { 202 | // If we don't have a [Header], it doesn't make sense to receive a [Body]. 203 | if (!received_group_header) 204 | { 205 | printf("Error reading header! (got body)\n"); 206 | return; 207 | } 208 | } 209 | // If we have the [Header], now we also have the [Body]; execute the code after the switch(). 210 | break; 211 | 212 | // Execute the code after the switch(). 213 | case KLineKWP1281Lib::SUCCESS: 214 | break; 215 | } 216 | 217 | /* 218 | The getMeasurementType() function can return: 219 | *KLineKWP1281Lib::UNKNOWN - index out of range (measurement doesn't exist in group) 220 | *KLineKWP1281Lib::VALUE - regular measurement, with a value and units 221 | *KLineKWP1281Lib::TEXT - text measurement 222 | */ 223 | 224 | // Get the selected measurement's type. 225 | KLineKWP1281Lib::measurementType measurement_type; 226 | if (!received_group_header) 227 | { 228 | measurement_type = KLineKWP1281Lib::getMeasurementType(measurement_index, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)); 229 | } 230 | // For "header+body" measurements, you need to use this other function that specifically parses headers instead of regular responses. 231 | else 232 | { 233 | measurement_type = KLineKWP1281Lib::getMeasurementTypeFromHeader(measurement_index, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer)); 234 | } 235 | 236 | // Check the returned value. 237 | switch (measurement_type) 238 | { 239 | // "Value and units" type 240 | case KLineKWP1281Lib::VALUE: 241 | { 242 | // The measurement's units will be copied into this array, so they can be displayed. 243 | char units_string[16]; 244 | 245 | // Regular mode: 246 | if (!received_group_header) 247 | { 248 | // Determine how many decimal places are best suited to this measurement. 249 | uint8_t decimals = KLineKWP1281Lib::getMeasurementDecimals(measurement_index, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)); 250 | 251 | // Display the calculated value, with the recommended amount of decimals. 252 | printf("%.*lf %s\n", 253 | decimals, 254 | KLineKWP1281Lib::getMeasurementValue(measurement_index, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)), 255 | KLineKWP1281Lib::getMeasurementUnits(measurement_index, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer), units_string, sizeof(units_string))); 256 | } 257 | // "Header+body" mode: 258 | else 259 | { 260 | // Determine how many decimal places are best suited to this measurement. 261 | uint8_t decimals = KLineKWP1281Lib::getMeasurementDecimalsFromHeader(measurement_index, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer)); 262 | 263 | // Display the calculated value, with the recommended amount of decimals. 264 | printf("%.*lf %s\n", 265 | decimals, 266 | KLineKWP1281Lib::getMeasurementValueFromHeaderBody(measurement_index, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer)), 267 | KLineKWP1281Lib::getMeasurementUnitsFromHeaderBody(measurement_index, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer), units_string, sizeof(units_string))); 268 | } 269 | } 270 | break; 271 | 272 | // "Text" type 273 | case KLineKWP1281Lib::TEXT: 274 | { 275 | // The measurement's text will be copied into this array, so it can be displayed. 276 | char text_string[16]; 277 | 278 | // Display the text. 279 | if (!received_group_header) 280 | { 281 | // The function getMeasurementText() returns the same string that it's given. It's the same as text_string. 282 | printf("%s\n", KLineKWP1281Lib::getMeasurementText(measurement_index, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer), text_string, sizeof(text_string))); 283 | } 284 | else 285 | { 286 | // The function getMeasurementTextFromHeaderBody() returns the same string that it's given. It's the same as text_string. 287 | printf("%s\n", KLineKWP1281Lib::getMeasurementTextFromHeaderBody(measurement_index, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer), text_string, sizeof(text_string))); 288 | } 289 | } 290 | break; 291 | 292 | // Invalid measurement index 293 | case KLineKWP1281Lib::UNKNOWN: 294 | printf("N/A\n"); 295 | break; 296 | } 297 | } 298 | -------------------------------------------------------------------------------- /examples/04.Continuous_measurement_test/04.Continuous_measurement_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Title: 3 | 04.Continuous_measurement_test.ino 4 | 5 | Description: 6 | Demonstrates how to read a measurement group continuously. 7 | 8 | Notes: 9 | *You can change which group to read by modifying the #define below. 10 | *This group will be read continuously while the sketch is running. 11 | *It is not necessary to maintain the connection with "diag.update();" in the loop, because any request will have the same effect (update() must 12 | be used in periods of inactivity). 13 | */ 14 | 15 | // Change which group to read. 16 | #define GROUP_TO_READ 1 17 | 18 | /* 19 | ***Uncomment the appropriate options in the "configuration.h" file! 20 | 21 | ESP32 22 | *has two additional serial ports 23 | *pins (they can be remapped, this is what they are configured to in these examples): 24 | *K-line TX -> RX pin 16 25 | *K-line RX -> TX pin 17 26 | 27 | ESP32-C6 28 | *has one additional serial port 29 | *pins (they can be remapped, this is what they are configured to in these examples): 30 | *K-line TX -> RX pin 16 31 | *K-line RX -> TX pin 17 32 | */ 33 | 34 | // Include the library. 35 | #include 36 | 37 | // Include the two files containing configuration options and the functions used for communication. 38 | #include "configuration.h" 39 | #include "communication.h" 40 | 41 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 42 | KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin); 43 | 44 | // Please find more information on the topic of KWP1281 measurement groups in the "03.Full_measurement_test" example. 45 | uint8_t measurement_buffer[80]; 46 | uint8_t measurement_body_buffer[4]; 47 | 48 | void setup() 49 | { 50 | // Initialize the Serial Monitor. 51 | Serial.begin(115200); 52 | delay(500); 53 | printf("Sketch started.\n"); 54 | 55 | // If debugging bus traffic was enabled, attach the debugging function. 56 | #if debug_traffic 57 | diag.KWP1281debugFunction(KWP1281debugFunction); 58 | #endif 59 | 60 | // Create the task that runs this demo, attaching the handle defined in "configuration.h". 61 | assert(xTaskCreate(demo_task, "demo", 4096, NULL, 5, &demo_task_handle)); 62 | } 63 | 64 | void loop() 65 | { 66 | // The Arduino loop is not used; delete its task. 67 | vTaskDelete(NULL); 68 | } 69 | 70 | void demo_task(void *args) 71 | { 72 | // Connect to the module. 73 | diag.connect(connect_to_module, module_baud_rate, false); 74 | 75 | // A task must either have an infinite loop, or delete itself. 76 | printf("Requesting group %d continuously.\n", GROUP_TO_READ); 77 | while (true) 78 | { 79 | // Show the parameters in the requested group. 80 | showMeasurements(GROUP_TO_READ); 81 | } 82 | } 83 | 84 | /* 85 | 86 | -The variables used for "header+body" groups are stored globally. 87 | -Normally, the flag `received_group_header` should be reset to false when requesting a new group. 88 | -This example only requests one group, so it isn't necessary; if the group was "header+body" once, it will always be like that. 89 | 90 | -You could also keep `received_group_header` local to the showMeasurements() function, and do diag.update() before each request, 91 | to ensure you receive the [Header]. 92 | -Of course, this would negatively impact performance, since you need to receive more data from the module each time you read the group. 93 | 94 | */ 95 | 96 | // This flag keeps track if a [Header] was received for the current group, meaning it's of the "header+body" type. 97 | bool received_group_header = false; 98 | 99 | // This will contain the amount of measurements received in the [Header] of a "header+body" group. 100 | uint8_t amount_of_measurements_in_header = 0; 101 | 102 | 103 | void showMeasurements(uint8_t group) 104 | { 105 | // This will contain the amount of measurements in the current group, after calling the readGroup() function. 106 | uint8_t amount_of_measurements = 0; 107 | 108 | /* 109 | -For modules which report measuring groups in the "header+body" mode, it is important to do update() when requesting a new group, 110 | so the module sends the [Header]. 111 | -If it's the first request, it's not necessary; the module will always send the [Header] for the first time. 112 | -Since this example only requests one group, it would decrease performance (measurement rate) to do update() every time, since it would cause the module 113 | to send the [Header] over and over again, and it takes time to receive it, instead of only receiving it once, at the beginning. 114 | */ 115 | // diag.update(); 116 | 117 | /* 118 | The readGroup() function can return: 119 | KLineKWP1281Lib::FAIL - the requested group does not exist 120 | KLineKWP1281Lib::ERROR - communication error 121 | KLineKWP1281Lib::SUCCESS - received measurements 122 | KLineKWP1281Lib::GROUP_BASIC_SETTING - received a [Basic settings] measurement; the buffer contains 10 raw values 123 | KLineKWP1281Lib::GROUP_HEADER - received the [Header] for a "header+body" group; need to read again to get the [Body] 124 | KLineKWP1281Lib::GROUP_BODY - received the [Body] for a "header+body" group 125 | */ 126 | 127 | // Read the requested group and store the returneded value. 128 | KLineKWP1281Lib::executionStatus readGroup_status; 129 | // If the group is not of "header+body" type, or if it is and this is the first request, we don't have a [Header] (yet), so `received_group_header=false`. 130 | // The response to this request will be stored in the larger array. 131 | // If it is in fact of "header+body" type, the [Header] will be stored in this array. 132 | if (!received_group_header) 133 | { 134 | readGroup_status = diag.readGroup(amount_of_measurements, group, measurement_buffer, sizeof(measurement_buffer)); 135 | } 136 | // If the group is of "header+body" type, and this is not the first request, it means we have a header, so `received_group_header=true`. 137 | // The response to this request will be stored in the smaller array, because it should be the [Body]. 138 | else 139 | { 140 | readGroup_status = diag.readGroup(amount_of_measurements, group, measurement_body_buffer, sizeof(measurement_body_buffer)); 141 | } 142 | 143 | // Check the returned value. 144 | switch (readGroup_status) 145 | { 146 | case KLineKWP1281Lib::ERROR: 147 | { 148 | printf("Error reading group!\n"); 149 | } 150 | // There is no reason to continue, exit the function. 151 | return; 152 | 153 | case KLineKWP1281Lib::FAIL: 154 | { 155 | printf("Group %d does not exist!", group); 156 | } 157 | // There is no reason to continue, exit the function. 158 | return; 159 | 160 | case KLineKWP1281Lib::GROUP_BASIC_SETTINGS: 161 | { 162 | // If we have a [Header], it means this group sends responses of "header+body" type. 163 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 164 | if (received_group_header) 165 | { 166 | printf("Error reading body! (got basic settings)\n"); 167 | return; 168 | } 169 | 170 | // We have 10 raw values in the `measurement_buffer` array. 171 | printf("Basic settings in group %d: ", group); 172 | for (uint8_t i = 0; i < 10; i++) 173 | { 174 | printf("%d ", measurement_buffer[i]); 175 | } 176 | printf("\n"); 177 | } 178 | // We have nothing else to display, exit the function. 179 | return; 180 | 181 | case KLineKWP1281Lib::GROUP_HEADER: 182 | { 183 | // If we have a [Header], it means this group sends responses of "header+body" type. 184 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 185 | if (received_group_header) 186 | { 187 | printf("Error reading body! (got header)\n"); 188 | return; 189 | } 190 | 191 | // Set the flag to indicate that a header was received, making this a "header+body" group response. 192 | received_group_header = true; 193 | 194 | // Store the number of measurements received in the header. 195 | amount_of_measurements_in_header = amount_of_measurements; 196 | } 197 | // We have nothing to display yet, the next readGroup() will get the actual data; exit the function. 198 | return; 199 | 200 | case KLineKWP1281Lib::GROUP_BODY: 201 | { 202 | // If we don't have a [Header], it doesn't make sense to receive a [Body]. 203 | if (!received_group_header) 204 | { 205 | printf("Error reading header! (got body)\n"); 206 | return; 207 | } 208 | } 209 | // If we have the [Header], now we also have the [Body]; execute the code after the switch(). 210 | break; 211 | 212 | // Execute the code after the switch(). 213 | case KLineKWP1281Lib::SUCCESS: 214 | break; 215 | } 216 | 217 | // If the group was read successfully, display its measurements. 218 | printf("Group %d:\n", group); 219 | 220 | // Display each measurement. 221 | for (uint8_t i = 0; i < 4; i++) 222 | { 223 | // Format the values with a leading tab. 224 | printf(" "); 225 | 226 | /* 227 | The getMeasurementType() function can return: 228 | KLineKWP1281Lib::UNKNOWN - index out of range (the measurement doesn't exist in the group) or the formula is invalid/not-applicable 229 | KLineKWP1281Lib::VALUE - regular measurement, with a value and units 230 | KLineKWP1281Lib::TEXT - text measurement 231 | */ 232 | 233 | // Get the current measurement's type. 234 | KLineKWP1281Lib::measurementType measurement_type; 235 | if (!received_group_header) 236 | { 237 | measurement_type = KLineKWP1281Lib::getMeasurementType(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)); 238 | } 239 | // For "header+body" measurements, you need to use this other function that specifically parses headers instead of regular responses. 240 | else 241 | { 242 | measurement_type = KLineKWP1281Lib::getMeasurementTypeFromHeader(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer)); 243 | } 244 | 245 | // Check the returned value. 246 | switch (measurement_type) 247 | { 248 | // "Value and units" type 249 | case KLineKWP1281Lib::VALUE: 250 | { 251 | // The measurement's units will be copied into this array, so they can be displayed. 252 | char units_string[16]; 253 | 254 | // Regular mode: 255 | if (!received_group_header) 256 | { 257 | // Display the calculated value, with the recommended amount of decimals. 258 | // The function getMeasurementUnits() returns the same string that it's given. It's the same as units_string. 259 | printf("%.*lf %s\n", 260 | KLineKWP1281Lib::getMeasurementDecimals(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)), 261 | KLineKWP1281Lib::getMeasurementValue(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)), 262 | KLineKWP1281Lib::getMeasurementUnits(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer), units_string, sizeof(units_string))); 263 | } 264 | // "Header+body" mode: 265 | else 266 | { 267 | // Display the calculated value, with the recommended amount of decimals. 268 | // The function getMeasurementUnitsFromHeaderBody() returns the same string that it's given. It's the same as units_string. 269 | printf("%.*lf %s\n", 270 | KLineKWP1281Lib::getMeasurementDecimalsFromHeader(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer)), 271 | KLineKWP1281Lib::getMeasurementValueFromHeaderBody(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer)), 272 | KLineKWP1281Lib::getMeasurementUnitsFromHeaderBody(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer), units_string, sizeof(units_string))); 273 | } 274 | } 275 | break; 276 | 277 | // "Text" type 278 | case KLineKWP1281Lib::TEXT: 279 | { 280 | // The measurement's text will be copied into this array, so it can be displayed. 281 | char text_string[16]; 282 | 283 | if (!received_group_header) 284 | { 285 | // The function getMeasurementText() returns the same string that it's given. It's the same as text_string. 286 | printf("%s\n", KLineKWP1281Lib::getMeasurementText(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer), text_string, sizeof(text_string))); 287 | } 288 | else 289 | { 290 | // The function getMeasurementTextFromHeaderBody() returns the same string that it's given. It's the same as text_string. 291 | printf("%s\n", KLineKWP1281Lib::getMeasurementTextFromHeaderBody(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer), text_string, sizeof(text_string))); 292 | } 293 | } 294 | break; 295 | 296 | // Invalid measurement index 297 | case KLineKWP1281Lib::UNKNOWN: 298 | printf("N/A\n"); 299 | break; 300 | } 301 | } 302 | 303 | // Leave an empty line. 304 | printf("\n"); 305 | } 306 | -------------------------------------------------------------------------------- /examples/07.Dual_K-line_test/07.Dual_K-line_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Title: 3 | 07.Dual_K-Line_test.ino 4 | 5 | Description: 6 | Demonstrates how to connect to 2 K-Lines at once and read measurements. 7 | 8 | Notes: 9 | *This demo is made specifically for the ESP32, as native multitasking is needed. Also, it has 2 hardware serial ports. 10 | *It doesn't make sense to have 2 transceivers on the same K-Line! This is only useful if you decide to separate a specific module from the 11 | car's K-Line in order to be able to access it asyncronously. 12 | 13 | *You can change which measurements to read by modifying the arrays below. 14 | *These measurements will be read continuously while the sketch is running. 15 | *It is not necessary to maintain the connection with "diag.update();" in the loop, because any request will have the same effect (update() must 16 | be used in periods of inactivity). 17 | */ 18 | 19 | /* 20 | ***Uncomment the appropriate options in the "configuration.h" file! 21 | 22 | ESP32 23 | *has two additional serial ports 24 | *pins (they can be remapped, this is what they are configured to in this example): 25 | K-Line 1 TX -> RX pin 16 26 | K-Line 1 RX -> TX pin 17 27 | K-Line 2 TX -> RX pin 26 28 | K-Line 2 RX -> TX pin 27 29 | */ 30 | 31 | // Change which measurement to read from each K-Line. 32 | uint8_t groups_to_read[] = {1, 1}; // K-Line 1, K-Line 2; valid range: 0-255 33 | uint8_t indexes_to_read[] = {0, 0}; // K-Line 1, K-Line 2; valid range: 0-3 34 | 35 | // Include the library. 36 | #include 37 | 38 | // Include the two files containing configuration options and the functions used for communication. 39 | #include "configuration.h" 40 | #include "communication.h" 41 | 42 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 43 | KLineKWP1281Lib diag1(beginFunction1, endFunction1, sendFunction1, receiveFunction1, TX1_pin); 44 | KLineKWP1281Lib diag2(beginFunction2, endFunction2, sendFunction2, receiveFunction2, TX2_pin); 45 | 46 | // Put pointers to both instances in an array, to be able to use them by indexing into the array. 47 | KLineKWP1281Lib *diags[] = {&diag1, &diag2}; 48 | 49 | // Structure to hold all data necessary for processing a measurement 50 | struct kline_measurement_structure 51 | { 52 | // Which K-Line the data was read from 53 | uint8_t K_line_index; 54 | 55 | // Whether or not the measurement is coming from a "header+body" group 56 | bool is_from_header_body; 57 | 58 | // The 3 bytes used for most types of measurements 59 | uint8_t formula; 60 | uint8_t NWb; 61 | uint8_t MWb; 62 | 63 | // Some regular groups have measurements which need more than 3 bytes. 64 | // Some "header+body" groups have measurements which have a 17-byte table. 65 | // There are cases where more than 17 bytes are needed, but this is very rare. 66 | uint8_t data[17]; 67 | uint8_t data_length; 68 | }; 69 | 70 | // Queue which will contain structures like the one defined above 71 | // Through it, both K-Line tasks will send their data to the main processing task. 72 | QueueHandle_t kline_measurement_queue; 73 | 74 | void setup() 75 | { 76 | // Initialize the Serial Monitor. 77 | Serial.begin(115200); 78 | delay(500); 79 | printf("Sketch started.\n"); 80 | 81 | // If debugging bus traffic was enabled, attach the debugging functions. 82 | #if debug_traffic 83 | diag1.KWP1281debugFunction(KWP1281debugFunction1); 84 | diag2.KWP1281debugFunction(KWP1281debugFunction2); 85 | #endif 86 | 87 | // Create the queue, able to hold 10 measurement structures. 88 | assert(kline_measurement_queue = xQueueCreate(10, sizeof(kline_measurement_structure))); 89 | 90 | // Create the task which will display the data coming from the two modules. 91 | // It has a higher priority than the K-Line tasks, because it mostly waits for data. 92 | // Having a higher priority means it will be able to wake up and process the data right when it arrives. 93 | xTaskCreate(receiver_task, "recv", 4096, NULL, 2, NULL); 94 | 95 | printf("Requesting group %d, measurement %d from K-Line 1 and group %d, measurement %d from K-Line 2 continuously.\n", 96 | groups_to_read[0], indexes_to_read[0], 97 | groups_to_read[1], indexes_to_read[1]); 98 | 99 | // Create a task for each K-Line. 100 | // The two handles defined in "configuration.h" are attached to the tasks, so the Serial onReceive functions 101 | // can send notifications to them. 102 | xTaskCreate(kline_task, "K1", 4096, (void*)0, 1, &demo_task1_handle); 103 | xTaskCreate(kline_task, "K2", 4096, (void*)1, 1, &demo_task2_handle); 104 | // Both tasks share the same code, but they are differentiated through the argument passed to the function, 105 | // which indicates the index to be used for accessing the arrays defined at the top of the sketch. 106 | } 107 | 108 | void loop() 109 | { 110 | // The Arduino loop is not used; delete its task. 111 | vTaskDelete(NULL); 112 | } 113 | 114 | void receiver_task(void *args) 115 | { 116 | // This structure will hold data, either from K-Line 1 or K-Line 2. 117 | kline_measurement_structure measurement_structure; 118 | 119 | // A task must have an infinite loop. 120 | while (true) 121 | { 122 | // Wait for data to be available on the queue. 123 | // The timeout of portMAX_DELAY means this will wait however necessary (possibly indefinitely) for data to be available. 124 | xQueueReceive(kline_measurement_queue, &measurement_structure, portMAX_DELAY); 125 | 126 | // Display the data. 127 | displayMeasurement(&measurement_structure); 128 | 129 | // The loop will repeat, waiting again. 130 | } 131 | } 132 | 133 | void kline_task(void *args) 134 | { 135 | // This function is shared by both tasks; they are differentiated through the number stored in the "args" variable. 136 | 137 | // Retrieve the K-Line index (0/1), which was passed through the parameter. 138 | uint8_t K_line_index = (uint32_t)args; 139 | 140 | // Retrieve a pointer to the correct diag instance. 141 | KLineKWP1281Lib *diag = diags[K_line_index]; 142 | 143 | // Connect to the desired module. 144 | // "diag" is a pointer, so "->" is used for accessing its functions, instead of ".". 145 | diag->connect(connect_to_modules[K_line_index], module_baud_rates[K_line_index]); 146 | 147 | // Please find more information on the topic of KWP1281 measurement groups in the "03.Full_measurement_test" example. 148 | uint8_t measurement_buffer[80]; 149 | uint8_t measurement_body_buffer[4]; 150 | 151 | // This flag keeps track if a [Header] was received for the current group, meaning it's of the "header+body" type. 152 | bool received_group_header = false; 153 | 154 | // This will contain the amount of measurements received in the [Header] of a "header+body" group. 155 | uint8_t amount_of_measurements_in_header = 0; 156 | 157 | // A task must have an infinite loop. 158 | while (true) 159 | { 160 | // This function needs the K-Line index, because it puts it in the struct that gets sent on the queue. 161 | // It is given the arrays directly, and it retrieves the objects itself using the index. 162 | getSingleMeasurement(K_line_index, groups_to_read, indexes_to_read, diags, measurement_buffer, sizeof(measurement_buffer), measurement_body_buffer, sizeof(measurement_body_buffer), received_group_header, amount_of_measurements_in_header); 163 | } 164 | } 165 | 166 | /* 167 | K_line_index -> which K-Line to send the request on (0/1) 168 | groups -> array of 2 group IDs; which group to request from each K-Line 169 | indexes -> array of 2 measurement indexes; which index to retrieve from the group requested from each K-Line 170 | diags -> array of 2 pointers to diag instances 171 | measurement_buffer -> array to give to readGroup() by default 172 | measurement_buffer_size -> array size to give to readGroup() by default 173 | measurement_body_buffer -> array to give to readGroup() after getting the [Header] of a "header+body" group 174 | measurement_body_buffer_size -> array size to give to readGroup() after getting the [Header] of a "header+body" group 175 | received_group_header -> flag, to be changed if the [Header] of a "header+body" group is received 176 | amount_of_measurements_in_header -> number, to be changed if the [Header] of a "header+body" group is received 177 | */ 178 | void getSingleMeasurement(uint8_t K_line_index, uint8_t *groups, uint8_t *indexes, KLineKWP1281Lib **diags, uint8_t *measurement_buffer, size_t measurement_buffer_size, uint8_t *measurement_body_buffer, size_t measurement_body_buffer_size, bool &received_group_header, uint8_t &amount_of_measurements_in_header) 179 | { 180 | // Retrieve each object from the arrays, using the index. 181 | uint8_t group = groups[K_line_index]; 182 | uint8_t index = indexes[K_line_index]; 183 | KLineKWP1281Lib *diag = diags[K_line_index]; 184 | 185 | // This structure will contain the received measurement; set its index, to indicate which K-Line the measurement was requested from. 186 | kline_measurement_structure measurement_structure; 187 | measurement_structure.K_line_index = K_line_index; 188 | 189 | // This will contain the amount of measurements in the current group, after calling the readGroup() function. 190 | uint8_t amount_of_measurements = 0; 191 | 192 | /* 193 | The readGroup() function can return: 194 | KLineKWP1281Lib::FAIL - the requested group does not exist 195 | KLineKWP1281Lib::ERROR - communication error 196 | KLineKWP1281Lib::SUCCESS - received measurements 197 | KLineKWP1281Lib::GROUP_BASIC_SETTING - received a [Basic settings] measurement; the buffer contains 10 raw values 198 | KLineKWP1281Lib::GROUP_HEADER - received the [Header] for a "header+body" group; need to read again to get the [Body] 199 | KLineKWP1281Lib::GROUP_BODY - received the [Body] for a "header+body" group 200 | */ 201 | 202 | // Read the requested group and store the returned value. 203 | // "diag" is a pointer, so "->" is used for accessing its functions, instead of ".". 204 | KLineKWP1281Lib::executionStatus readGroup_status; 205 | // If the group is not of "header+body" type, or if it is and this is the first request, we don't have a [Header] (yet), so `received_group_header=false`. 206 | // The response to this request will be stored in the larger array. 207 | // If it is in fact of "header+body" type, the [Header] will be stored in this array. 208 | if (!received_group_header) 209 | { 210 | readGroup_status = diag->readGroup(amount_of_measurements, group, measurement_buffer, measurement_buffer_size); 211 | } 212 | // If the group is of "header+body" type, and this is not the first request, it means we have a header, so `received_group_header=true`. 213 | // The response to this request will be stored in the smaller array, because it should be the [Body]. 214 | else 215 | { 216 | readGroup_status = diag->readGroup(amount_of_measurements, group, measurement_body_buffer, measurement_body_buffer_size); 217 | } 218 | 219 | switch (readGroup_status) 220 | { 221 | case KLineKWP1281Lib::ERROR: 222 | { 223 | printf("Error reading group on K%d!\n", K_line_index + 1); 224 | } 225 | // There is no reason to continue, exit the function. 226 | return; 227 | 228 | case KLineKWP1281Lib::FAIL: 229 | { 230 | printf("Group %d does not exist on K%d!\n", group, K_line_index + 1); 231 | } 232 | // There is no reason to continue, exit the function. 233 | return; 234 | 235 | case KLineKWP1281Lib::GROUP_BASIC_SETTINGS: 236 | { 237 | // If we have a [Header], it means this group sends responses of "header+body" type. 238 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 239 | if (received_group_header) 240 | { 241 | printf("Error reading body! (got basic settings)\n"); 242 | return; 243 | } 244 | 245 | printf("Basic settings not supported!\n"); 246 | } 247 | // We have nothing else to display, exit the function. 248 | return; 249 | 250 | case KLineKWP1281Lib::GROUP_HEADER: 251 | { 252 | // If we have a [Header], it means this group sends responses of "header+body" type. 253 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 254 | if (received_group_header) 255 | { 256 | printf("Error reading body! (got header)\n"); 257 | return; 258 | } 259 | 260 | // Set the flag to indicate that a header was received, making this a "header+body" group response. 261 | received_group_header = true; 262 | 263 | // Store the number of measurements received in the header. 264 | amount_of_measurements_in_header = amount_of_measurements; 265 | } 266 | // We have nothing to display yet, the next readGroup() will get the actual data; exit the function. 267 | return; 268 | 269 | case KLineKWP1281Lib::GROUP_BODY: 270 | { 271 | // If we don't have a [Header], it doesn't make sense to receive a [Body]. 272 | if (!received_group_header) 273 | { 274 | printf("Error reading header! (got body)\n"); 275 | return; 276 | } 277 | } 278 | // If we have the [Header], now we also have the [Body]; execute the code after the switch(). 279 | break; 280 | 281 | //Execute the code after the switch(). 282 | case KLineKWP1281Lib::SUCCESS: 283 | break; 284 | } 285 | 286 | // If a group header was received, this measurement is coming from a "header+body" group. 287 | measurement_structure.is_from_header_body = received_group_header; 288 | 289 | // Retrieve the 3 bytes into the structure. 290 | if (measurement_structure.is_from_header_body) 291 | { 292 | measurement_structure.formula = KLineKWP1281Lib::getFormulaFromHeader(index, amount_of_measurements_in_header, measurement_buffer, measurement_buffer_size); 293 | measurement_structure.NWb = KLineKWP1281Lib::getNWbFromHeader(index, amount_of_measurements_in_header, measurement_buffer, measurement_buffer_size); 294 | measurement_structure.MWb = KLineKWP1281Lib::getMWbFromBody(index, amount_of_measurements, measurement_body_buffer, measurement_body_buffer_size); 295 | } 296 | else 297 | { 298 | measurement_structure.formula = KLineKWP1281Lib::getFormula(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 299 | measurement_structure.NWb = KLineKWP1281Lib::getNWb(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 300 | measurement_structure.MWb = KLineKWP1281Lib::getMWb(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 301 | } 302 | 303 | // Retrieve the data buffer and its length, because some measurements may need it. 304 | if (measurement_structure.is_from_header_body) 305 | { 306 | uint8_t data_length = KLineKWP1281Lib::getDataTableLengthFromHeader(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 307 | uint8_t *data = KLineKWP1281Lib::getDataTableFromHeader(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 308 | 309 | // Copy the data buffer and its length into the structure. 310 | measurement_structure.data_length = data_length; 311 | memcpy(measurement_structure.data, data, data_length); 312 | } 313 | else 314 | { 315 | uint8_t data_length = KLineKWP1281Lib::getMeasurementDataLength(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 316 | uint8_t *data = KLineKWP1281Lib::getMeasurementData(index, amount_of_measurements, measurement_buffer, measurement_buffer_size); 317 | 318 | // Copy the data buffer and its length into the structure. 319 | measurement_structure.data_length = data_length; 320 | memcpy(measurement_structure.data, data, data_length); 321 | } 322 | 323 | // Put the structure on the queue; don't wait for the queue to free up if it is full. 324 | xQueueSend(kline_measurement_queue, &measurement_structure, 0); 325 | } 326 | 327 | void displayMeasurement(kline_measurement_structure *measurement_structure) 328 | { 329 | // Show which K-Line the measurement was requested from. 330 | // "measurement_structure" is a pointer, so "->" is used for accessing its members, instead of ".". 331 | printf("K%d: ", measurement_structure->K_line_index + 1); 332 | 333 | /* 334 | The getMeasurementType() function can return: 335 | KLineKWP1281Lib::UNKNOWN - index out of range (measurement doesn't exist in group) 336 | KLineKWP1281Lib::VALUE - regular measurement, with a value and units 337 | KLineKWP1281Lib::TEXT - text measurement 338 | */ 339 | 340 | // Get the measurement's type and check the returned value. 341 | switch (KLineKWP1281Lib::getMeasurementType(measurement_structure->formula)) 342 | { 343 | // "Value and units" type 344 | case KLineKWP1281Lib::VALUE: 345 | { 346 | // This will hold the measurement's units. 347 | char units_string[16]; 348 | 349 | // Display the calculated value, with the recommended amount of decimals. 350 | if (measurement_structure->is_from_header_body) 351 | { 352 | printf("%.*lf %s\n", 353 | KLineKWP1281Lib::getMeasurementDecimalsFromHeader(measurement_structure->formula), 354 | KLineKWP1281Lib::getMeasurementValueFromHeaderBody(measurement_structure->formula, measurement_structure->NWb, measurement_structure->MWb, measurement_structure->data, measurement_structure->data_length), 355 | KLineKWP1281Lib::getMeasurementUnitsFromHeaderBody(measurement_structure->formula, measurement_structure->NWb, measurement_structure->MWb, units_string, sizeof(units_string))); 356 | } 357 | else 358 | { 359 | printf("%.*lf %s\n", 360 | KLineKWP1281Lib::getMeasurementDecimals(measurement_structure->formula), 361 | KLineKWP1281Lib::getMeasurementValue(measurement_structure->formula, measurement_structure->NWb, measurement_structure->MWb), 362 | KLineKWP1281Lib::getMeasurementUnits(measurement_structure->formula, measurement_structure->NWb, measurement_structure->MWb, units_string, sizeof(units_string))); 363 | } 364 | } 365 | break; 366 | 367 | // "Text" type 368 | case KLineKWP1281Lib::TEXT: 369 | { 370 | // This will hold the measurement's text. 371 | char text_string[16]; 372 | 373 | // Display the text. 374 | if (measurement_structure->is_from_header_body) 375 | { 376 | printf("%s\n", KLineKWP1281Lib::getMeasurementTextFromHeaderBody(measurement_structure->formula, measurement_structure->NWb, measurement_structure->MWb, measurement_structure->data, measurement_structure->data_length, text_string, sizeof(text_string))); 377 | } 378 | else 379 | { 380 | printf("%s\n", KLineKWP1281Lib::getMeasurementText(measurement_structure->formula, measurement_structure->data, measurement_structure->data_length, text_string, sizeof(text_string))); 381 | } 382 | } 383 | break; 384 | 385 | // Invalid measurement index 386 | case KLineKWP1281Lib::UNKNOWN: 387 | printf("N/A\n"); 388 | break; 389 | } 390 | } 391 | -------------------------------------------------------------------------------- /examples/03.Full_measurement_test/03.Full_measurement_test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Title: 3 | 03.Full_measurement_test.ino 4 | 5 | Description: 6 | Demonstrates how to read a module's measuring groups. 7 | 8 | Notes: 9 | *Measuring groups 0-255 will be read, after which the connection will be stopped. 10 | *Each group will be read 5 times. 11 | *If you have the text table enabled (KWP1281_TEXT_TABLE_SUPPORTED), you can choose from a few different languages a bit further below 12 | in "KLineKWP1281Lib.h". 13 | *Please only choose one option. 14 | *If it is disabled, the value of some parameters will be replaced by "EN_f25". 15 | */ 16 | 17 | /* 18 | ***Uncomment the appropriate options in the "configuration.h" file! 19 | 20 | ESP32 21 | *has two additional serial ports 22 | *pins (they can be remapped, this is what they are configured to in these examples): 23 | *K-line TX -> RX pin 16 24 | *K-line RX -> TX pin 17 25 | 26 | ESP32-C6 27 | *has one additional serial port 28 | *pins (they can be remapped, this is what they are configured to in these examples): 29 | *K-line TX -> RX pin 16 30 | *K-line RX -> TX pin 17 31 | */ 32 | 33 | // Include the library. 34 | #include 35 | 36 | // Include the two files containing configuration options and the functions used for communication. 37 | #include "configuration.h" 38 | #include "communication.h" 39 | 40 | // Debugging can be enabled in the "Tools>Core Debug Level" menu in the IDE, in order to print connection-related info on the Serial Monitor. 41 | KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin); 42 | 43 | /* 44 | -The KWP1281 protocol may respond to a "get measurement group" request in 6 ways: 45 | 1. NAK - the group does not exist 46 | (readGroup() returns KLineKWP1281Lib::FAIL) 47 | 2. ACK - the group exists, but it's empty 48 | (readGroup() returns KLineKWP1281Lib::FAIL) 49 | 3. Basic settings - the group contains 10 raw values 50 | (readGroup() returns KLineKWP1281Lib::GROUP_BASIC_SETTINGS) 51 | 4. Standard - this is how almost all modules report measurement groups 52 | (readGroup() returns KLineKWP1281Lib::SUCCESS) 53 | 5. Header - the group is of, what I call, "header+body" type, this header only contains a sort of template 54 | (readGroup() returns KLineKWP1281Lib::GROUP_HEADER) 55 | 6. Body - this is the part of a "header+body" group that contains the actual data read from sensors 56 | (readGroup() returns KLineKWP1281Lib::GROUP_BODY) 57 | 58 | --- 59 | -You can treat cases 1 and 2 as meaning that the requested group doesn't exist. 60 | --- 61 | 62 | --- 63 | -For case 3 [Basic settings], the buffer which you used for readGroup() contains 10 bytes starting from byte 0. 64 | -The amount_of_measurements variable which you passed to readGroup() contains 0. 65 | -There aren't any functions that use this buffer, you should just access the array like normal. 66 | --- 67 | 68 | --- 69 | -For case 4 [Standard], the buffer which you used for readGroup() contains the group. 70 | -The amount_of_measurements variable which you passed to readGroup() contains the amount of measurements received, usually between 0 and 4. 71 | 72 | -You can interpret the data using the following functions: 73 | +getMeasurementType() - tells if the measurement is a VALUE or TEXT 74 | +getMeasurementValue() - get the value for a measurement of type VALUE 75 | (for some measurements of type TEXT, it might return a sort of code, but usually it returns nan) 76 | +getMeasurementUnits() - get the units of the measurement of type VALUE 77 | (for measurements of type TEXT, or upon other errors, it clears the given character array and returns an invalid string (nullptr)) 78 | +getMeasurementText() - get the value for a measurement of type TEXT 79 | (for measurements of type VALUE, or upon other errors, it clears the given character array and returns an invalid string (nullptr)) 80 | +getMeasurementTextLength() - get the character length of the value for a measurement of type TEXT 81 | (for measurements of type VALUE, or upon other errors, it returns 0) 82 | +getMeasurementDecimals() - get the recommended amount of decimal places for a measurement 83 | 84 | -For more advanced use, the functions mentioned above can also take values extracted from the measurement buffer, instead of being given the entire thing. 85 | -There are also functions that extract these important bytes from the measurement buffer: 86 | +getFormula() - gets the FORMULA byte 87 | +getNWb() - gets the BYTE_A 88 | +getMWb() - gets the BYTE_B 89 | +getMeasurementData() - returns an array containing a measurement's bytes 90 | (most often, this array just contains BYTE_A and BYTE_B, but there are some measurements with more bytes) 91 | (technically, what it returns is the same measurement buffer, but indexed so it starts right at the measurement's data) 92 | +getMeasurementDataLength() - returns the length of the array containing a measurement's bytes 93 | - It is very rarely necessary to use these advanced functions, so don't overcomplicate your code with them. 94 | --- 95 | 96 | --- 97 | -For case 5 [Header], the buffer which you used for readGroup() contains the group's header. 98 | -You are supposed to save this response somewhere, or at least protect it from being overwritten. 99 | -When you get the [Body] response, you will use it together with the [Header] response for calculating measurements. 100 | 101 | -The amount_of_measurements variable which you passed to readGroup() contains the amount of measurements in the header. 102 | -Although not exactly necessary, the functions used for calculating values from "header+body" responses also needs this from the header. 103 | -Just save it somewhere, and use it when you get the [Body]. 104 | 105 | -Take special care, because, for modules which respond in this way, you have to call update() right before you request a new group! 106 | -Otherwise, you will only get a [Body], and it's useless without a [Header]. 107 | --- 108 | 109 | --- 110 | -For case 6 [Body], the buffer which you used for readGroup() contains the group's body. 111 | -You should already have this group's header. 112 | -The amount_of_measurements variable which you passed to readGroup() contains the amount of measurements received, usually between 0 and 4. 113 | 114 | -You can interpret the data using the following functions: 115 | +getMeasurementTypeFromHeader() - needs only header 116 | +getMeasurementValueFromHeaderBody() - needs header and body 117 | +getMeasurementUnitsFromHeaderBody() - needs header and body 118 | +getMeasurementTextFromHeaderBody() - needs header and body 119 | +getMeasurementTextLengthFromHeaderBody() - needs header and body 120 | +getMeasurementDecimalsFromHeader() - needs only header 121 | -You can see that some functions only need the header, most also need the body. 122 | -Apart from that, they work exactly like the functions for [Standard] responses. 123 | 124 | -There are also functions that extract important bytes from the header buffer, but they are not very useful, because you can't use them properly. 125 | -They are mainly used internally. 126 | +getFormulaFromHeader() - gets the FORMULA byte 127 | +getNWbFromHeader() - gets the BYTE_A, which stays constant 128 | +getDataTableFromHeader() - returns an array containing a measurement's attached table 129 | +getDataTableLengthFromHeader() - returns the length of the array containing a measurement's attached table 130 | - It is pretty much never necessary to use these advanced functions, so don't overcomplicate your code with them. 131 | --- 132 | */ 133 | 134 | /* 135 | -Normally, each measurement takes 3 bytes, and a group can store up to 4 measurements. 136 | -This means that a good size for the measurement buffer is 12, and it will work for most modules. 137 | -There is a formula that takes 6 bytes, and some formulas that contain an arbitrary number of bytes. 138 | -Even if a group returns a [Basic settings], it will contain 10 bytes, which will fit. 139 | 140 | -There are also some modules which respond with "header+body", as mentioned in a comment above. 141 | -A realistic worst-case-scenario is 80 bytes, if all 4 measurements have a 17-byte table attached. 142 | -There is a formula for which this table can also be longer than 17 bytes, further complicating things. 143 | 144 | -If you see "Error reading group!" during runtime, please enable debugging. 145 | -If you see "The given buffer is too small for the incoming message" debug messages, you need to increase the size of the buffers below. 146 | */ 147 | 148 | // [Regular] measurements, [Basic settings] measurements and [Header] responses will be stored in this array. 149 | uint8_t measurement_buffer[80]; 150 | 151 | // [Body] responses will be stored in this array. 152 | // It must be separate from the other array, because the [Header] needs to remain intact in order to be able to calculate values. 153 | uint8_t measurement_body_buffer[4]; 154 | 155 | void setup() 156 | { 157 | // Initialize the Serial Monitor. 158 | Serial.begin(115200); 159 | delay(500); 160 | printf("Sketch started.\n"); 161 | 162 | // If debugging bus traffic was enabled, attach the debugging function. 163 | #if debug_traffic 164 | diag.KWP1281debugFunction(KWP1281debugFunction); 165 | #endif 166 | 167 | // Create the task that runs this demo, attaching the handle defined in "configuration.h". 168 | assert(xTaskCreate(demo_task, "demo", 4096, NULL, 5, &demo_task_handle)); 169 | } 170 | 171 | void loop() 172 | { 173 | // The Arduino loop is not used; delete its task. 174 | vTaskDelete(NULL); 175 | } 176 | 177 | void demo_task(void *args) 178 | { 179 | // Connect to the module. 180 | diag.connect(connect_to_module, module_baud_rate, false); 181 | 182 | // Read all groups (000-255). 183 | printf("Requesting measuring groups 000-255.\n"); 184 | for (uint16_t i = 0; i <= 255; i++) 185 | { 186 | showMeasurements(i); 187 | } 188 | 189 | // Disconnect from the module. 190 | diag.disconnect(false); 191 | printf("Disconnected.\n"); 192 | 193 | // A task must either have an infinite loop, or delete itself. 194 | vTaskDelete(NULL); 195 | } 196 | 197 | void showMeasurements(uint8_t group) 198 | { 199 | // This will contain the amount of measurements in the current group, after calling the readGroup() function. 200 | uint8_t amount_of_measurements = 0; 201 | 202 | // This flag keeps track if a [Header] was received for the current group, meaning it's of the "header+body" type. 203 | bool received_group_header = false; 204 | 205 | // In case of the "header+body" groups, the [Body] response is the one that actually contains live data. 206 | // In theory, the module should report the same number of measurements in the [Header] and in the [Body]. 207 | // Still, the number of measurements received in the [Header] will be stored separately, for correctness. 208 | uint8_t amount_of_measurements_in_header = 0; 209 | 210 | // For modules which report measuring groups in the "header+body" mode, it is important to do update() when requesting a new group, so the module sends the [Header]. 211 | diag.update(); 212 | 213 | // The requested group will be read 5 times, if it exists and no error occurs. 214 | // If a "header+body" group is encountered, we will do one more step, so that the actual data requests are 5. 215 | for (uint8_t attempt = 1; attempt <= 5; attempt++) 216 | { 217 | /* 218 | The readGroup() function can return: 219 | KLineKWP1281Lib::FAIL - the requested group does not exist 220 | KLineKWP1281Lib::ERROR - communication error 221 | KLineKWP1281Lib::SUCCESS - received measurements 222 | KLineKWP1281Lib::GROUP_BASIC_SETTING - received a [Basic settings] measurement; the buffer contains 10 raw values 223 | KLineKWP1281Lib::GROUP_HEADER - received the [Header] for a "header+body" group; need to read again to get the [Body] 224 | KLineKWP1281Lib::GROUP_BODY - received the [Body] for a "header+body" group 225 | */ 226 | 227 | // Read the requested group and store the returned value. 228 | KLineKWP1281Lib::executionStatus readGroup_status; 229 | // If the group is not of "header+body" type, or if it is and this is the first request, we don't have a [Header] (yet), so `received_group_header=false`. 230 | // The response to this request will be stored in the larger array. 231 | // If it is in fact of "header+body" type, the [Header] will be stored in this array. 232 | if (!received_group_header) 233 | { 234 | readGroup_status = diag.readGroup(amount_of_measurements, group, measurement_buffer, sizeof(measurement_buffer)); 235 | } 236 | // If the group is of "header+body" type, and this is not the first request, it means we have a header, so `received_group_header=true`. 237 | // The response to this request will be stored in the smaller array, because it should be the [Body]. 238 | else 239 | { 240 | readGroup_status = diag.readGroup(amount_of_measurements, group, measurement_body_buffer, sizeof(measurement_body_buffer)); 241 | } 242 | 243 | // Check the returned value. 244 | switch (readGroup_status) 245 | { 246 | case KLineKWP1281Lib::ERROR: 247 | { 248 | printf("Error reading group!\n"); 249 | } 250 | // There is no reason to continue, exit the function. 251 | return; 252 | 253 | case KLineKWP1281Lib::FAIL: 254 | { 255 | printf("Group %d does not exist!\n", group); 256 | } 257 | // There is no reason to continue, exit the function. 258 | return; 259 | 260 | case KLineKWP1281Lib::GROUP_BASIC_SETTINGS: 261 | { 262 | // If we have a [Header], it means this group sends responses of "header+body" type. 263 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 264 | if (received_group_header) 265 | { 266 | printf("Error reading body! (got basic settings)\n"); 267 | return; 268 | } 269 | 270 | // We have 10 raw values in the `measurement_buffer` array. 271 | printf("Basic settings in group %d: ", group); 272 | for (uint8_t i = 0; i < 10; i++) 273 | { 274 | printf("%d ", measurement_buffer[i]); 275 | } 276 | printf("\n"); 277 | } 278 | // We have nothing else to display (yet); skip this step of the loop. 279 | continue; 280 | 281 | case KLineKWP1281Lib::GROUP_HEADER: 282 | { 283 | // If we have a [Header], it means this group sends responses of "header+body" type. 284 | // So, at this point, it doesn't make sense to receive something other than a [Body]. 285 | if (received_group_header) 286 | { 287 | printf("Error reading body! (got header)\n"); 288 | return; 289 | } 290 | 291 | // Set the flag to indicate that a header was received, making this a "header+body" group response. 292 | received_group_header = true; 293 | 294 | // Store the number of measurements received in the header. 295 | amount_of_measurements_in_header = amount_of_measurements; 296 | 297 | // Add an extra step to the loop, because this one shouldn't count, as it doesn't contain live data. 298 | attempt--; 299 | } 300 | // We have nothing to display yet, the next readGroup() will get the actual data; skip this step of the loop. 301 | continue; 302 | 303 | case KLineKWP1281Lib::GROUP_BODY: 304 | { 305 | // If we don't have a [Header], it doesn't make sense to receive a [Body]. 306 | if (!received_group_header) 307 | { 308 | printf("Error reading header! (got body)\n"); 309 | return; 310 | } 311 | } 312 | // If we have the [Header], now we also have the [Body]; execute the code after the switch(). 313 | break; 314 | 315 | // Execute the code after the switch(). 316 | case KLineKWP1281Lib::SUCCESS: 317 | break; 318 | } 319 | 320 | // If the group was read successfully, display its measurements. 321 | printf("Group %d:\n", group); 322 | 323 | // Display each measurement. 324 | for (uint8_t i = 0; i < amount_of_measurements; i++) 325 | { 326 | // Format the values with a leading tab. 327 | printf(" "); 328 | 329 | /* 330 | The getMeasurementType() function can return: 331 | KLineKWP1281Lib::UNKNOWN - index out of range (the measurement doesn't exist in the group) or the formula is invalid/not-applicable 332 | KLineKWP1281Lib::VALUE - regular measurement, with a value and units 333 | KLineKWP1281Lib::TEXT - text measurement 334 | */ 335 | 336 | // Get the current measurement's type. 337 | KLineKWP1281Lib::measurementType measurement_type; 338 | if (!received_group_header) 339 | { 340 | measurement_type = KLineKWP1281Lib::getMeasurementType(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)); 341 | } 342 | // For "header+body" measurements, you need to use this other function that specifically parses headers instead of regular responses. 343 | else 344 | { 345 | measurement_type = KLineKWP1281Lib::getMeasurementTypeFromHeader(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer)); 346 | } 347 | 348 | // Check the returned value. 349 | switch (measurement_type) 350 | { 351 | // "Value and units" type 352 | case KLineKWP1281Lib::VALUE: 353 | { 354 | // The measurement's units will be copied into this array, so they can be displayed. 355 | char units_string[16]; 356 | 357 | // This variable will contain the calculated value of the measurement. 358 | double value; 359 | 360 | // This variable will contain the recommended amount of decimal places for the measurement. 361 | uint8_t decimals; 362 | 363 | // Regular mode: 364 | if (!received_group_header) 365 | { 366 | // Calculate the value. 367 | value = KLineKWP1281Lib::getMeasurementValue (i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)); 368 | 369 | // Get the units. 370 | // It's not necessary to use the returned value of this function (character pointer), the units will appear in the given character array. 371 | KLineKWP1281Lib::getMeasurementUnits (i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer), units_string, sizeof(units_string)); 372 | 373 | // Get the recommended amount of decimal places. 374 | decimals = KLineKWP1281Lib::getMeasurementDecimals (i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer)); 375 | } 376 | // "Header+body" mode: 377 | else 378 | { 379 | // Calculate the value; both the header and body are needed. 380 | value = KLineKWP1281Lib::getMeasurementValueFromHeaderBody (i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer)); 381 | 382 | // Get the units; both the header and body are needed. 383 | // It's not necessary to use the returned value of this function (character pointer), the units will appear in the given character array. 384 | KLineKWP1281Lib::getMeasurementUnitsFromHeaderBody (i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer), units_string, sizeof(units_string)); 385 | 386 | // Get the recommended amount of decimal places; only the header is needed. 387 | decimals = KLineKWP1281Lib::getMeasurementDecimalsFromHeader (i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer)); 388 | } 389 | 390 | // Display the calculated value, with the recommended amount of decimals. 391 | printf("%.*lf %s\n", decimals, value, units_string); 392 | } 393 | break; 394 | 395 | // "Text" type 396 | case KLineKWP1281Lib::TEXT: 397 | { 398 | // The measurement's text will be copied into this array, so it can be displayed. 399 | char text_string[16]; 400 | 401 | if (!received_group_header) 402 | { 403 | // Get the text. 404 | // It's not necessary to use the returned value of this function (character pointer), the text will appear in the given character array. 405 | KLineKWP1281Lib::getMeasurementText(i, amount_of_measurements, measurement_buffer, sizeof(measurement_buffer), text_string, sizeof(text_string)); 406 | } 407 | else 408 | { 409 | // Get the text; both the header and body are needed. 410 | // It's not necessary to use the returned value of this function (character pointer), the text will appear in the given character array. 411 | KLineKWP1281Lib::getMeasurementTextFromHeaderBody(i, amount_of_measurements_in_header, measurement_buffer, sizeof(measurement_buffer), amount_of_measurements, measurement_body_buffer, sizeof(measurement_body_buffer), text_string, sizeof(text_string)); 412 | } 413 | 414 | // Display the text. 415 | printf("%s\n", text_string); 416 | } 417 | break; 418 | 419 | // Invalid measurement index 420 | case KLineKWP1281Lib::UNKNOWN: 421 | printf("N/A\n"); 422 | break; 423 | } 424 | } 425 | 426 | // Leave an empty line. 427 | printf("\n"); 428 | } 429 | } 430 | -------------------------------------------------------------------------------- /src/KLineKWP1281Lib_ESP32.h: -------------------------------------------------------------------------------- 1 | #ifndef KLineKWP1281Lib_H 2 | #define KLineKWP1281Lib_H 3 | 4 | // If the following line is commented out, all debug procedures are removed from the library (to save memory): 5 | #define KWP1281_DEBUG_SUPPORTED 6 | 7 | // There are some formulas whose units string comes from a large table. If the following line is commented out, that table is removed: 8 | #define KWP1281_TEXT_TABLE_SUPPORTED 9 | // (if disabled, the string given to getMeasurementUnits() will contain "EN_f25" if that formula is encountered) 10 | 11 | // If the following line is commented out, fault code description strings are removed from the library (to save memory): 12 | #define KWP1281_FAULT_CODE_DESCRIPTION_SUPPORTED 13 | // (if disabled, the string given to getFaultDescription() will contain "EN_dsc") 14 | 15 | // If the following line is commented out, OBD fault code description strings are removed from the library (to save memory): 16 | #define KWP1281_OBD_FAULT_CODE_DESCRIPTION_SUPPORTED 17 | // (if disabled, the string given to getFaultDescription() will contain "EN_obd") 18 | 19 | // If the following line is commented out, fault code elaboration strings are removed from the library (to save memory): 20 | #define KWP1281_FAULT_CODE_ELABORATION_SUPPORTED 21 | // (if disabled, the string given to getFaultElaboration() will contain "EN_elb") 22 | 23 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 24 | 25 | #include 26 | #include "units.h" // measurement unit strings 27 | #include "decimals.h" // measurement decimals 28 | #include "keyed_struct.h" // storage type for text 29 | 30 | // Logging functions 31 | #ifdef KWP1281_DEBUG_SUPPORTED 32 | #include "esp_log.h" 33 | #define K_DEBUG_TAG "KWP" 34 | #define K_LOG_ERROR(str, ...) ESP_LOGE(K_DEBUG_TAG, str, ##__VA_ARGS__) 35 | #define K_LOG_WARNING(str, ...) ESP_LOGW(K_DEBUG_TAG, str, ##__VA_ARGS__) 36 | #define K_LOG_INFO(str, ...) ESP_LOGI(K_DEBUG_TAG, str, ##__VA_ARGS__) 37 | #define K_LOG_DEBUG(str, ...) ESP_LOGD(K_DEBUG_TAG, str, ##__VA_ARGS__) 38 | #else 39 | #define K_LOG_ERROR(str, ...) 40 | #define K_LOG_WARNING(str, ...) 41 | #define K_LOG_INFO(str, ...) 42 | #define K_LOG_DEBUG(str, ...) 43 | #endif 44 | 45 | #ifdef KWP1281_TEXT_TABLE_SUPPORTED 46 | // Choose text table language, if enabled: 47 | #include "text_table_EN.h" 48 | //#include "text_table_DE.h" 49 | //#include "text_table_PL.h" 50 | //#include "text_table_RO.h" 51 | #endif 52 | 53 | #ifdef KWP1281_FAULT_CODE_DESCRIPTION_SUPPORTED 54 | // Choose fault code description text language, if enabled: 55 | #include "fault_code_description_EN.h" 56 | //#include "fault_code_description_DE.h" 57 | //#include "fault_code_description_PL.h" 58 | //#include "fault_code_description_RO.h" 59 | #endif 60 | 61 | #ifdef KWP1281_OBD_FAULT_CODE_DESCRIPTION_SUPPORTED 62 | // Choose OBD fault code description text language, if enabled: 63 | #include "OBD_fault_code_description_EN.h" 64 | //#include "OBD_fault_code_description_DE.h" 65 | //#include "OBD_fault_code_description_PL.h" 66 | //#include "OBD_fault_code_description_RO.h" 67 | #endif 68 | 69 | #ifdef KWP1281_FAULT_CODE_ELABORATION_SUPPORTED 70 | // Choose fault code elaboration text language, if enabled: 71 | #include "fault_code_elaboration_EN.h" 72 | //#include "fault_code_elaboration_DE.h" 73 | //#include "fault_code_elaboration_PL.h" 74 | //#include "fault_code_elaboration_RO.h" 75 | #endif 76 | 77 | // Helper for determining the amount of elements in an array 78 | #define ARRAYSIZE(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) 79 | 80 | class KLineKWP1281Lib 81 | { 82 | public: 83 | ///VARIABLES/TYPES 84 | 85 | // Function pointer types for callbacks 86 | using customErrorFunction_type = void (*)(uint8_t module, unsigned long baud); // errorFunction 87 | using beginFunction_type = void (*)(unsigned long baud); // beginFunction 88 | using endFunction_type = void (*)(void); // endFunction 89 | using sendFunction_type = void (*)(uint8_t data); // sendFunction 90 | using receiveFunction_type = bool (*)(uint8_t *data, unsigned long timeout_ticks); // receiveFunction 91 | using KWP1281debugFunction_type = void (*)(bool direction, uint8_t message_sequence, uint8_t message_type, uint8_t *data, size_t length); // debugFunction 92 | 93 | // Return types for functions 94 | enum executionStatus { 95 | FAIL, 96 | SUCCESS, 97 | ERROR, 98 | 99 | // Only used for readGroup(): 100 | GROUP_BASIC_SETTINGS, // A "basic settings" response type is encountered instead of regular measurements 101 | GROUP_HEADER, // The header of a "header+body" response type is encountered 102 | GROUP_BODY // The body of a "header+body" response type is encountered 103 | }; 104 | 105 | // Measurement types 106 | enum measurementType { 107 | UNKNOWN, //Invalid measurement requested 108 | VALUE, //Measurement has value and units 109 | TEXT //Measurement has text 110 | }; 111 | 112 | // Create an instance of the library 113 | KLineKWP1281Lib( 114 | beginFunction_type beginFunction, endFunction_type endFunction, 115 | sendFunction_type sendFunction, receiveFunction_type receiveFunction, 116 | uint8_t tx_pin, bool full_duplex = true 117 | ); 118 | 119 | // How many milliseconds to wait before sending a complement to the initialization bytes received from the module 120 | unsigned long initComplementDelay = 40; 121 | // How many milliseconds to wait before sending a complement to a byte received from the module 122 | unsigned long complementDelay = 2; 123 | // How many milliseconds to wait before sending a byte in a block, after receiving a complement from the module 124 | unsigned long byteDelay = 2; 125 | // How many milliseconds to wait before sending a block (request) 126 | unsigned long blockDelay = 10; 127 | 128 | // How many milliseconds to wait for receiving a response during initialization 129 | unsigned long initResponseTimeout = 1000; 130 | // How many milliseconds to wait for receiving a complement when sending bytes in a block 131 | unsigned long complementResponseTimeout = 50; 132 | // How many milliseconds to wait for receiving the echo to a byte that was sent 133 | unsigned long echoTimeout = 1000; 134 | // How many milliseconds to wait for receiving a response to a request 135 | unsigned long responseTimeout = 2000; 136 | // How many milliseconds to wait for receiving a byte in a block 137 | unsigned long byteTimeout = 100; 138 | 139 | ///FUNCTIONS 140 | 141 | // Attach a function to be called for debugging KWP1281 messages 142 | void KWP1281debugFunction(KWP1281debugFunction_type debug_function); 143 | 144 | // Define a custom function to execute if an error occurs and the connection is terminated 145 | void customErrorFunction(customErrorFunction_type function); 146 | 147 | // Attempt to connect to a module 148 | executionStatus attemptConnect(uint8_t module, unsigned long baud_rate, bool request_extra_identification = true); 149 | // Connect to a module 150 | void connect(uint8_t module, unsigned long baud_rate, bool request_extra_identification = true); 151 | // Maintain the connection 152 | void update(); 153 | // Stop the connection 154 | void disconnect(bool wait_for_response = true); 155 | 156 | // Get the module's VAG part number 157 | char* getPartNumber(); 158 | // Get the module's name 159 | char* getIdentification(); 160 | // Get the module's extra identification 161 | char* getExtraIdentification(); 162 | // Get the module's coding value 163 | uint16_t getCoding(); 164 | // Get the module's workshop code 165 | uint32_t getWorkshopCode(); 166 | 167 | // Perform a login operation 168 | executionStatus login(uint16_t login_code, uint32_t workshop_code); 169 | 170 | // Change the coding of a module 171 | executionStatus recode(uint16_t coding, uint32_t workshop_code); 172 | 173 | // Get the module's fault codes 174 | executionStatus readFaults(uint8_t &amount_of_fault_codes, uint8_t* fault_code_buffer = nullptr, size_t fault_code_buffer_size = 0); 175 | // Get a fault code from a fault code reading 176 | static uint16_t getFaultCode(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size); 177 | // Determine whether or not a fault is of standard OBD type 178 | static bool isOBDFaultCode(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size); 179 | static bool isOBDFaultCode(uint16_t fault_code); 180 | // Get a string containing the formatted code of a standard OBD fault code 181 | static char* getOBDFaultCode(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size, char* str, size_t string_size); 182 | static char* getOBDFaultCode(uint16_t fault_code, char* str, size_t string_size); 183 | // Get the fault description string from a fault code reading 184 | static char* getFaultDescription(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size, char* str, size_t string_size); 185 | static char* getFaultDescription(uint16_t fault_code, char* str, size_t string_size); 186 | // Get the length of the description string from a fault code reading 187 | static size_t getFaultDescriptionLength(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size); 188 | static size_t getFaultDescriptionLength(uint16_t fault_code); 189 | // Get a fault elaboration code from a fault code reading 190 | static uint8_t getFaultElaborationCode(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size); 191 | // Get the fault elaboration string from a fault code reading 192 | static char* getFaultElaboration(bool &is_intermittent, uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size, char* str, size_t string_size); 193 | static char* getFaultElaboration(bool &is_intermittent, uint8_t elaboration_code, char* str, size_t string_size); 194 | // Get the length of the elaboration string from a fault code reading 195 | static size_t getFaultElaborationLength(uint8_t fault_code_index, uint8_t amount_of_fault_codes, uint8_t* fault_code_buffer, size_t fault_code_buffer_size); 196 | static size_t getFaultElaborationLength(uint8_t elaboration_code); 197 | // Clear the module's fault codes 198 | executionStatus clearFaults(); 199 | 200 | // Read the value of an adaptation channel 201 | executionStatus readAdaptation(uint8_t channel, uint16_t &value); 202 | // Test if a value would work on an adaptation channel 203 | executionStatus testAdaptation(uint8_t channel, uint16_t value); 204 | // Change the value of an adaptation channel 205 | executionStatus adapt(uint8_t channel, uint16_t value, uint32_t workshop_code); 206 | 207 | // Perform a basic setting 208 | executionStatus basicSetting(uint8_t &amount_of_values, uint8_t group, uint8_t* basic_setting_buffer = nullptr, size_t basic_setting_buffer_size = 0); 209 | // Get a value from a basic setting reading 210 | static uint8_t getBasicSettingValue(uint8_t value_index, uint8_t amount_of_values, uint8_t* basic_setting_buffer, size_t basic_setting_buffer_size); 211 | 212 | // Read a group (block) of measurements 213 | executionStatus readGroup(uint8_t &amount_of_measurements, uint8_t group, uint8_t* measurement_buffer, size_t measurement_buffer_size); 214 | 215 | // Get the 3 significant bytes of a measurement (enough for most measurements of type VALUE) 216 | static uint8_t getFormula(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *measurement_buffer, size_t measurement_buffer_size); 217 | static uint8_t getNWb(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *measurement_buffer, size_t measurement_buffer_size); 218 | static uint8_t getMWb(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *measurement_buffer, size_t measurement_buffer_size); 219 | // Get the data and length of a measurement (necessary for some measurements of type TEXT and some of type VALUE) 220 | static uint8_t *getMeasurementData(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *measurement_buffer, size_t measurement_buffer_size); 221 | static uint8_t getMeasurementDataLength(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *measurement_buffer, size_t measurement_buffer_size); 222 | 223 | // Get a measurement's type from a group reading (either VALUE or TEXT) - only formula byte needed 224 | static measurementType getMeasurementType(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t* measurement_buffer, size_t measurement_buffer_size); 225 | static measurementType getMeasurementType(uint8_t formula); 226 | // Get the calculated value of a measurement of type VALUE - formula, NWb and MWb bytes needed 227 | static double getMeasurementValue(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t* measurement_buffer, size_t measurement_buffer_size); 228 | static double getMeasurementValue(uint8_t formula, uint8_t *measurement_data, uint8_t measurement_data_length); 229 | static double getMeasurementValue(uint8_t formula, uint8_t NWb, uint8_t MWb); 230 | // Get the units of a measurement of type VALUE - formula, NWb and MWb bytes needed 231 | static char* getMeasurementUnits(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t* measurement_buffer, size_t measurement_buffer_size, char* str, size_t string_size); 232 | static char* getMeasurementUnits(uint8_t formula, uint8_t *measurement_data, uint8_t measurement_data_length, char* str, size_t string_size); 233 | static char* getMeasurementUnits(uint8_t formula, uint8_t NWb, uint8_t MWb, char* str, size_t string_size); 234 | // Get the text of a measurement of type TEXT - measurement data and length needed 235 | static char* getMeasurementText(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t* measurement_buffer, size_t measurement_buffer_size, char* str, size_t string_size); 236 | static char* getMeasurementText(uint8_t formula, uint8_t *measurement_data, uint8_t measurement_data_length, char* str, size_t string_size); 237 | // Get the length of the text of a measurement of type TEXT - measurement data and length needed 238 | static size_t getMeasurementTextLength(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t* measurement_buffer, size_t measurement_buffer_size); 239 | static size_t getMeasurementTextLength(uint8_t formula, uint8_t *measurement_data, uint8_t measurement_data_length); 240 | // Get the recommended decimal places of a measurement (of type VALUE) - only formula byte needed 241 | static uint8_t getMeasurementDecimals(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t* measurement_buffer, size_t measurement_buffer_size); 242 | static uint8_t getMeasurementDecimals(uint8_t formula); 243 | 244 | // Get the formula of a measurement from a header of a header+body response 245 | static uint8_t getFormulaFromHeader(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *header_buffer, size_t header_buffer_size); 246 | // Get the constant byte of a measurement from a header of a header+body response 247 | static uint8_t getNWbFromHeader(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *header_buffer, size_t header_buffer_size); 248 | // Get the live data byte of a measurement from a body of a header+body response 249 | static uint8_t getMWbFromBody(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *body_buffer, size_t body_buffer_size); 250 | // Get the data table and its length of a measurement from a header of a header+body response. 251 | static uint8_t *getDataTableFromHeader(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *header_buffer, size_t header_buffer_size); 252 | static uint8_t getDataTableLengthFromHeader(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *header_buffer, size_t header_buffer_size); 253 | 254 | // Get a measurement's type from a group reading of type header+body - needed: [header array] / [formula] 255 | static measurementType getMeasurementTypeFromHeader(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *header_buffer, size_t header_buffer_size); 256 | static measurementType getMeasurementTypeFromHeader(uint8_t formula); 257 | // Get the calculated value of a measurement of type VALUE from a group reading of type header+body - needed: [header array, body array] / [formula, NWb, MWb, data table, length] 258 | static double getMeasurementValueFromHeaderBody(uint8_t measurement_index, uint8_t amount_of_measurements_in_header, uint8_t *header_buffer, size_t header_buffer_size, uint8_t amount_of_measurements_in_body, uint8_t *body_buffer, size_t body_buffer_size); 259 | static double getMeasurementValueFromHeaderBody(uint8_t formula, uint8_t NWb, uint8_t MWb, uint8_t *data_table, uint8_t data_table_length); 260 | // Get the units of a measurement of type VALUE from a group reading of type header+body - needed: [header array, body array] / [formula, NWb, MWb] 261 | static char* getMeasurementUnitsFromHeaderBody(uint8_t measurement_index, uint8_t amount_of_measurements_in_header, uint8_t *header_buffer, size_t header_buffer_size, uint8_t amount_of_measurements_in_body, uint8_t *body_buffer, size_t body_buffer_size, char* str, size_t string_size); 262 | static char* getMeasurementUnitsFromHeaderBody(uint8_t formula, uint8_t NWb, uint8_t MWb, char* str, size_t string_size); 263 | // Get the text of a measurement of type TEXT from a group reading of type header+body - needed: [header array, body array] / [formula, NWb, MWb, data table, length] 264 | static char* getMeasurementTextFromHeaderBody(uint8_t measurement_index, uint8_t amount_of_measurements_in_header, uint8_t *header_buffer, size_t header_buffer_size, uint8_t amount_of_measurements_in_body, uint8_t *body_buffer, size_t body_buffer_size, char* str, size_t string_size); 265 | static char* getMeasurementTextFromHeaderBody(uint8_t formula, uint8_t NWb, uint8_t MWb, uint8_t *data_table, uint8_t data_table_length, char* str, size_t string_size); 266 | // Get the length of the text of a measurement of type TEXT from a group reading of type header+body - needed: [header array, body array] / [formula, NWb, MWb, data table, length] 267 | static size_t getMeasurementTextLengthFromHeaderBody(uint8_t measurement_index, uint8_t amount_of_measurements_in_header, uint8_t *header_buffer, size_t header_buffer_size, uint8_t amount_of_measurements_in_body, uint8_t *body_buffer, size_t body_buffer_size); 268 | static size_t getMeasurementTextLengthFromHeaderBody(uint8_t formula, uint8_t NWb, uint8_t MWb, uint8_t *data_table, uint8_t data_table_length); 269 | // Get the recommended decimal places of a measurement from a group reading of type header+body - needed: [header array] / [formula] 270 | static uint8_t getMeasurementDecimalsFromHeader(uint8_t measurement_index, uint8_t amount_of_measurements, uint8_t *header_buffer, size_t header_buffer_size); 271 | static uint8_t getMeasurementDecimalsFromHeader(uint8_t formula); 272 | 273 | // Read a chunk of ROM/EEPROM 274 | executionStatus readROM(uint8_t chunk_size, uint16_t start_address, size_t &bytes_received, uint8_t* memory_buffer, uint8_t memory_buffer_size); 275 | 276 | // Perform output tests 277 | executionStatus outputTests(uint16_t ¤t_output_test); 278 | // Get the description string for the currently running output test 279 | static char* getOutputTestDescription(uint16_t output_test, char* str, size_t string_size); 280 | // Get the length of the description string for the currently running output test 281 | static size_t getOutputTestDescriptionLength(uint16_t output_test); 282 | 283 | private: 284 | // Formula A0 needs 8 negative powers of 10 for calculation. 285 | // They are defined at the bottom of the .cpp file. 286 | static const double negative_pow_10[8]; 287 | 288 | ///VARIABLES/TYPES 289 | static const uint8_t KWP_ACKNOWLEDGE = 0x09; // the module has no more data to send 290 | static const uint8_t KWP_REFUSE = 0x0A; // the module can not fulfill a request 291 | static const uint8_t KWP_DISCONNECT = 0x06; // the tester wants to disconnect from the module 292 | 293 | static const uint8_t KWP_REQUEST_EXTRA_ID = 0x00; // response: KWP_RECEIVE_ID_DATA (data available) / KWP_ACKNOWLEDGE (no data available) 294 | static const uint8_t KWP_REQUEST_LOGIN = 0x2B; // response: KWP_ACKNOWLEDGE (login successful) / KWP_REFUSE (login not successful) 295 | static const uint8_t KWP_REQUEST_RECODE = 0x10; // response: KWP_REQUEST_EXTRA_ID... 296 | static const uint8_t KWP_REQUEST_FAULT_CODES = 0x07; // response: KWP_RECEIVE_FAULT_CODES (ok) / KWP_REFUSE (fault codes not supported) 297 | static const uint8_t KWP_REQUEST_CLEAR_FAULTS = 0x05; // response: KWP_ACKNOWLEDGE (ok) / KWP_REFUSE (clearing fault codes not supported) 298 | static const uint8_t KWP_REQUEST_ADAPTATION = 0x21; // response: KWP_RECEIVE_ADAPTATION (ok) / KWP_REFUSE (invalid channel) 299 | static const uint8_t KWP_REQUEST_ADAPTATION_TEST = 0x22; // response: KWP_RECEIVE_ADAPTATION (ok) / KWP_REFUSE (invalid channel) 300 | static const uint8_t KWP_REQUEST_ADAPTATION_SAVE = 0x2A; // response: KWP_RECEIVE_ADAPTATION (ok) / KWP_REFUSE (invalid channel or value) 301 | static const uint8_t KWP_REQUEST_GROUP_READING = 0x29; // response: KWP_RECEIVE_GROUP_READING (ok) / KWP_ACKNOWLEDGE (empty group) / KWP_REFUSE (invalid group) 302 | static const uint8_t KWP_REQUEST_GROUP_READING_0 = 0x12; // response: KWP_RECEIVE_GROUP_READING (ok) / KWP_ACKNOWLEDGE (empty group) / KWP_REFUSE (invalid group) 303 | static const uint8_t KWP_REQUEST_READ_RAM = 0x01; // not implemented 304 | static const uint8_t KWP_REQUEST_READ_ROM = 0x03; // response: KWP_RECEIVE_ROM (ok) / KWP_REFUSE (reading ROM not supported or invalid parameters) 305 | static const uint8_t KWP_REQUEST_READ_EEPROM = 0x19; // not implemented 306 | static const uint8_t KWP_REQUEST_OUTPUT_TEST = 0x04; // response: KWP_RECEIVE_OUTPUT_TEST (ok) / KWP_REFUSE (output tests not supported) 307 | static const uint8_t KWP_REQUEST_BASIC_SETTING = 0x28; // response: KWP_RECEIVE_BASIC_SETTING (ok) / KWP_ACKNOWLEDGE (empty group) / KWP_REFUSE (invalid channel or not supported) 308 | static const uint8_t KWP_REQUEST_BASIC_SETTING_0 = 0x11; // response: KWP_RECEIVE_BASIC_SETTING (ok) / KWP_ACKNOWLEDGE (empty group) / KWP_REFUSE (invalid channel or not supported) 309 | 310 | static const uint8_t KWP_RECEIVE_ID_DATA = 0xF6; // request: connect/KWP_REQUEST_EXTRA_ID/KWP_REQUEST_RECODE 311 | static const uint8_t KWP_RECEIVE_FAULT_CODES = 0xFC; // request: KWP_REQUEST_FAULT_CODES 312 | static const uint8_t KWP_RECEIVE_ADAPTATION = 0xE6; // request: KWP_REQUEST_ADAPTATION/KWP_REQUEST_ADAPTATION_TEST/KWP_REQUEST_ADAPTATION_SAVE 313 | static const uint8_t KWP_RECEIVE_GROUP_HEADER = 0x02; // request: KWP_REQUEST_GROUP_READING 314 | static const uint8_t KWP_RECEIVE_GROUP_READING = 0xE7; // request: KWP_REQUEST_GROUP_READING 315 | static const uint8_t KWP_RECEIVE_ROM = 0xFD; // request: KWP_REQUEST_READ_ROM 316 | static const uint8_t KWP_RECEIVE_OUTPUT_TEST = 0xF5; // request: KWP_REQUEST_OUTPUT_TEST 317 | static const uint8_t KWP_RECEIVE_BASIC_SETTING = 0xF4; // request: KWP_REQUEST_BASIC_SETTING 318 | 319 | // Callback functions 320 | beginFunction_type _beginFunction; 321 | endFunction_type _endFunction; 322 | sendFunction_type _sendFunction; 323 | receiveFunction_type _receiveFunction; 324 | KWP1281debugFunction_type _debugFunction; 325 | 326 | // Selected transmit pin 327 | uint8_t _tx_pin; 328 | 329 | // Flag to indicate whether or not the used Serial port can receive at the same time as sending 330 | bool _full_duplex; 331 | 332 | // Return types for some functions 333 | enum RETURN_TYPE { 334 | ERROR_TIMEOUT, 335 | ERROR_OK, 336 | TYPE_ID, 337 | TYPE_ACK, 338 | TYPE_REFUSE, 339 | TYPE_FAULT_CODES, 340 | TYPE_ADAPTATION, 341 | TYPE_GROUP_HEADER, 342 | TYPE_GROUP_READING, 343 | TYPE_ROM, 344 | TYPE_OUTPUT_TEST, 345 | TYPE_BASIC_SETTING 346 | }; 347 | 348 | // The currently connected module's identification strings and values 349 | struct module_identification { 350 | char part_number[13]; 351 | char identification[25]; 352 | char extra_identification[37]; 353 | uint32_t coding; 354 | uint32_t workshop_code; 355 | } identification_data; 356 | 357 | // Types of debugging events for printing info 358 | enum DEBUG_TYPE { 359 | UNEXPECTED_RESPONSE, 360 | SEND_ERROR, 361 | ARRAY_NOT_LARGE_ENOUGH, 362 | 363 | LOGIN_ACCEPTED, 364 | LOGIN_NOT_ACCEPTED, 365 | 366 | FAULT_CODES_NOT_SUPPORTED, 367 | CLEARING_FAULT_CODES_NOT_SUPPORTED, 368 | CLEARING_FAULT_CODES_ACCEPTED, 369 | 370 | INVALID_ADAPTATION_CHANNEL, 371 | INVALID_ADAPTATION_CHANNEL_OR_VALUE, 372 | ADAPTATION_RECEIVED, 373 | ADAPTATION_ACCEPTED, 374 | ADAPTATION_NOT_ACCEPTED, 375 | 376 | INVALID_BASIC_SETTING_GROUP, 377 | RECEIVED_EMPTY_BASIC_SETTING_GROUP, 378 | RECEIVED_BASIC_SETTING, 379 | 380 | INVALID_MEASUREMENT_GROUP, 381 | RECEIVED_EMPTY_GROUP, 382 | RECEIVED_GROUP_HEADER, 383 | INVALID_GROUP_HEADER_MAP_LENGTH, 384 | RECEIVED_GROUP_BODY_OR_BASIC_SETTING, 385 | INVALID_GROUP_BODY_OR_BASIC_SETTING_LENGTH, 386 | RECEIVED_GROUP, 387 | 388 | READ_ROM_NOT_SUPPORTED, 389 | RECEIVED_ROM, 390 | 391 | NO_PART_NUMBER_AVAILABLE, 392 | RECEIVED_PART_NUMBER, 393 | NO_ID_PART1_AVAILABLE, 394 | RECEIVED_ID_PART1, 395 | NO_ID_PART2_AVAILABLE, 396 | RECEIVED_ID_PART2, 397 | NO_CODING_WSC_AVAILABLE, 398 | RECEIVED_CODING_WSC, 399 | EXTRA_ID_AVAILABLE, 400 | NO_EXTRA_ID_AVAILABLE, 401 | NO_EXTRA_ID_PART1_AVAILABLE, 402 | RECEIVED_EXTRA_ID_PART1, 403 | NO_EXTRA_ID_PART2_AVAILABLE, 404 | RECEIVED_EXTRA_ID_PART2, 405 | NO_EXTRA_ID_PART3_AVAILABLE, 406 | RECEIVED_EXTRA_ID_PART3, 407 | 408 | OUTPUT_TESTS_NOT_SUPPORTED, 409 | RECEIVED_OUTPUT_TEST, 410 | END_OF_OUTPUT_TESTS, 411 | 412 | DISCONNECT_INFO 413 | }; 414 | 415 | // Current parameters 416 | unsigned long _current_baud_rate; 417 | uint8_t _current_module; 418 | 419 | // Pointers to custom functions 420 | customErrorFunction_type _errorFunction; 421 | bool error_function_allowed = false; 422 | 423 | // Timers 424 | TickType_t last_byte_ticks; 425 | 426 | // Buffers 427 | uint8_t keyword_buffer[2]; 428 | uint8_t receive_buffer[16]; 429 | uint8_t receive_byte; 430 | 431 | // Flags 432 | bool may_send_protocol_parameters_again; 433 | 434 | // Current message sequence number 435 | uint8_t message_sequence; 436 | 437 | ///FUNCTIONS 438 | 439 | // Initialize the control module 440 | void bitbang_5baud(uint8_t module); 441 | 442 | // Read the module's identification strings and values 443 | executionStatus read_identification(bool request_extra_identification = true); 444 | 445 | // Receive parameters during initialization 446 | bool receive_keywords(uint8_t *buffer); 447 | // Receive a single byte 448 | bool read_byte(uint8_t *byte_out, unsigned long timeout_ms, bool must_send_complement = true); 449 | // Read the echo generated when sending a byte, if the interface can receive while sending 450 | void consume_UART_echo(uint8_t byte_in); 451 | // Send the complement of a byte 452 | void send_complement(uint8_t byte_in); 453 | // Receive a block 454 | RETURN_TYPE receive_message(size_t *bytes_received_out, uint8_t *buffer_out, size_t buffer_size, unsigned long timeout_ms); 455 | // Confirm receiving a block and request sending the next 456 | bool acknowledge(); 457 | 458 | // Send a single byte 459 | bool send_byte(uint8_t byte_in, bool wait_for_complement = true); 460 | // Send a block 461 | bool send_message(uint8_t message_type, uint8_t *parameters = nullptr, size_t parameter_count = 0); 462 | 463 | // Reconnect in case of a timeout error 464 | void error_function(); 465 | 466 | // Show debug information 467 | void show_debug_info(DEBUG_TYPE type, uint8_t parameter = 0); 468 | 469 | // Describe the received command in a block 470 | void show_debug_command_description(bool direction, uint8_t command); 471 | 472 | // Helper functions 473 | static uint8_t get_measurement_length(uint8_t *buffer, uint8_t bytes_received, uint8_t buffer_index); 474 | static double ToSigned(double MW); 475 | static double ToSigned(double NW, double MW); 476 | static double To16Bit(double NW, double MW); 477 | static int compare_keyed_structs(const void *a, const void *b); 478 | }; 479 | 480 | #endif 481 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------