Skip to content

Commit f624890

Browse files
committed
Add example, improve Basic Setting
1 parent 3dad696 commit f624890

8 files changed

Lines changed: 437 additions & 78 deletions

File tree

examples/02.Fault_code_test/02.Fault_code_test.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,16 @@ void setup() {
108108

109109
//Change these according to your module, in configuration.h.
110110
diag.connect(connect_to_module, module_baud_rate);
111+
112+
Serial.println("Requesting fault codes.");
111113

112114
//Show the module's fault codes (procedure moved to a function).
113115
showDTCs();
114116

115117
//Disconnect from the module.
116118
diag.disconnect();
119+
120+
Serial.println("Disconnected.");
117121
}
118122

119123
void loop() {

examples/03.Full_measurement_test/03.Full_measurement_test.ino

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
03.Full_measurement_test.ino
44
55
Description:
6-
Demonstrates how to read a module's measurement blocks.
6+
Demonstrates how to read a module's measuring blocks.
77
88
Notes:
9-
*Measurement blocks 1-255 will be read, after which the connection will be stopped.
9+
*Measuring blocks 0-255 will be read, after which the connection will be stopped.
1010
*/
1111

1212
/*
@@ -109,14 +109,18 @@ void setup() {
109109

110110
//Change these according to your module, in configuration.h.
111111
diag.connect(connect_to_module, module_baud_rate);
112+
113+
Serial.println("Requesting measuring blocks 000-255.");
112114

113-
//Read all groups (001-255).
114-
for (uint8_t i = 1; i != 0; i++) {
115+
//Read all groups (000-255).
116+
for (uint16_t i = 0; i <= 255; i++) {
115117
showMeasurements(i);
116118
}
117119

118120
//Disconnect from the module.
119121
diag.disconnect();
122+
123+
Serial.println("Disconnected.");
120124
}
121125

122126
void loop() {
@@ -133,17 +137,17 @@ void showMeasurements(uint8_t block) {
133137
uint8_t amount_of_measurements = 0;
134138
switch (diag.readGroup(amount_of_measurements, block, measurements, sizeof(measurements))) {
135139
case KLineKWP1281Lib::ERROR:
136-
Serial.println(F("Error reading measurements!"));
140+
Serial.println("Error reading measurements!");
137141
break;
138142

139143
case KLineKWP1281Lib::FAIL:
140-
Serial.print(F("Block "));
144+
Serial.print("Block ");
141145
Serial.print(block);
142-
Serial.println(F(" does not exist!"));
146+
Serial.println(" does not exist!");
143147
break;
144148

145149
case KLineKWP1281Lib::SUCCESS:
146-
Serial.print(F("Block "));
150+
Serial.print("Block ");
147151
Serial.print(block);
148152
Serial.println(':');
149153

examples/04.Continuous_measurement_test/04.Continuous_measurement_test.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
04.Continuous_measurement_test.ino
44
55
Description:
6-
Demonstrates how to read a module's measurement block(s) continuously.
6+
Demonstrates how to read a module's measuring block continuously.
77
88
Notes:
99
*You can change which block to read by modifying the #define below.
@@ -115,6 +115,10 @@ void setup() {
115115

116116
//Change these according to your module, in configuration.h.
117117
diag.connect(connect_to_module, module_baud_rate);
118+
119+
Serial.print("Requesting block ");
120+
Serial.print(BLOCK_TO_READ);
121+
Serial.println(" continuously.");
118122
}
119123

120124
void loop() {
@@ -131,17 +135,17 @@ void showMeasurements(uint8_t block) {
131135
uint8_t amount_of_measurements = 0;
132136
switch (diag.readGroup(amount_of_measurements, block, measurements, sizeof(measurements))) {
133137
case KLineKWP1281Lib::ERROR:
134-
Serial.println(F("Error reading measurements!"));
138+
Serial.println("Error reading measurements!");
135139
break;
136140

137141
case KLineKWP1281Lib::FAIL:
138-
Serial.print(F("Block "));
142+
Serial.print("Block ");
139143
Serial.print(block);
140-
Serial.println(F(" does not exist!"));
144+
Serial.println(" does not exist!");
141145
break;
142146

143147
case KLineKWP1281Lib::SUCCESS:
144-
Serial.print(F("Block "));
148+
Serial.print("Block ");
145149
Serial.print(block);
146150
Serial.println(':');
147151

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
Title:
3+
05.Single_measurement_test.ino
4+
5+
Description:
6+
Demonstrates how to read a single measurement from a module's measuring blocks continuously.
7+
8+
Notes:
9+
*You can change which block and which measurement 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 be used in
12+
periods of inactivity).
13+
*/
14+
15+
//Change which measurement to read, from which block.
16+
#define BLOCK_TO_READ 1
17+
#define MEASUREMENT_TO_READ 0 //valid range: 0-3
18+
19+
/*
20+
*This sketch displays information in the Serial Monitor, so another serial port is required for the K-line.
21+
*If another port is not available on the selected board, the regular port "Serial" can be used, although no information can be printed to it (good for
22+
projects with external displays or for data logging).
23+
24+
***Uncomment the appropriate options in the configuration.h file!
25+
26+
Arduino UNO
27+
*no additional serial port, software serial is used
28+
*library of choice: AltSoftSerial (better than SoftwareSerial because it can also receive while sending)
29+
*pins:
30+
*K-line TX -> RX pin 8
31+
*K-line RX -> TX pin 9
32+
33+
Arduino MEGA
34+
*has three additional serial ports
35+
*pins:
36+
*K-line TX -> Serial1 - RX pin 19 / Serial2 - RX pin 17 / Serial3 - RX pin 15
37+
*K-line RX -> Serial1 - TX pin 18 / Serial2 - TX pin 16 / Serial3 - TX pin 14
38+
39+
ESP32
40+
*has one additional serial port
41+
*pins:
42+
*K-line TX -> RX pin 16
43+
*K-line RX -> TX pin 17
44+
45+
ESP8266
46+
*has no additional serial ports
47+
*library of choice: SoftwareSerial (the ESP version of SoftwareSerial can also receive while sending)
48+
*pins: any unused
49+
*chosen for this demo:
50+
*K-line TX -> RX pin 4 (D2)
51+
*K-line RX -> TX pin 5 (D1)
52+
53+
***If using the first hardware serial port (Serial) (with other sketches), the interface must be disconnected during code upload, and no "Serial.print"s
54+
should be used.
55+
56+
*This sketch will not fit on an Arduino UNO by default! To fix this, open the library's /src/KLineKWP1281Lib.h file in a text editor and comment out the line
57+
"#define KWP1281_TEXT_TABLE_SUPPORTED" at the top, as explained in the comments.
58+
*/
59+
60+
//Include the library.
61+
#include <KLineKWP1281Lib.h>
62+
63+
//Include the two files containing configuration options and the functions used for communication.
64+
#include "configuration.h"
65+
#include "communication.h"
66+
67+
//Debugging can be enabled in configuration.h in order to print connection-related info on the Serial Monitor.
68+
#if debug_info
69+
KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin, is_full_duplex, &Serial);
70+
#else
71+
KLineKWP1281Lib diag(beginFunction, endFunction, sendFunction, receiveFunction, TX_pin, is_full_duplex);
72+
#endif
73+
74+
//Debugging can be enabled in configuration.h in order to print bus traffic on the Serial Monitor.
75+
#if debug_traffic
76+
void KWP1281debugFunction(bool type, uint8_t sequence, uint8_t command, uint8_t* data, uint8_t length) {
77+
Serial.println();
78+
79+
Serial.println(type ? "RECEIVE:" : "SEND:");
80+
81+
Serial.print("*command: ");
82+
if (command < 0x10) Serial.print(0);
83+
Serial.println(command, HEX);
84+
85+
Serial.print("*sequence: ");
86+
if (sequence < 0x10) Serial.print(0);
87+
Serial.println(sequence, HEX);
88+
89+
if (length) {
90+
Serial.print("*data bytes: ");
91+
Serial.println(length);
92+
93+
Serial.print("*data: ");
94+
for (uint16_t i = 0; i < length; i++) { //iterate through the message's contents
95+
if (data[i] < 0x10) Serial.print(0); //print a leading 0 where necessary to display 2-digit HEX
96+
Serial.print(data[i], HEX); //print the byte in HEX
97+
Serial.print(' ');
98+
}
99+
Serial.println();
100+
}
101+
}
102+
#endif
103+
104+
uint8_t measurements[3 * 4]; //buffer to store the measurements; each measurement takes 3 bytes; one block contains 4 measurements
105+
106+
void setup() {
107+
//Initialize the Serial Monitor.
108+
Serial.begin(115200);
109+
delay(500);
110+
Serial.println("Sketch started.");
111+
112+
//If debugging bus traffic was enabled, attach the debugging function.
113+
#if debug_traffic
114+
diag.KWP1281debugFunction(KWP1281debugFunction);
115+
#endif
116+
117+
//Change these according to your module, in configuration.h.
118+
diag.connect(connect_to_module, module_baud_rate);
119+
120+
Serial.print("Requesting block ");
121+
Serial.print(BLOCK_TO_READ);
122+
Serial.print(", measurement index ");
123+
Serial.print(MEASUREMENT_TO_READ);
124+
Serial.println(" continuously.");
125+
}
126+
127+
void loop() {
128+
showSingleMeasurement(BLOCK_TO_READ, MEASUREMENT_TO_READ);
129+
}
130+
131+
void showSingleMeasurement(uint8_t block, uint8_t measurement_index) {
132+
/*
133+
The readGroup() function can return:
134+
*KLineKWP1281Lib::SUCCESS - received measurements
135+
*KLineKWP1281Lib::FAIL - the requested block does not exist
136+
*KLineKWP1281Lib::ERROR - communication error
137+
*/
138+
uint8_t amount_of_measurements = 0;
139+
switch (diag.readGroup(amount_of_measurements, block, measurements, sizeof(measurements))) {
140+
case KLineKWP1281Lib::ERROR:
141+
Serial.println("Error reading measurements!");
142+
break;
143+
144+
case KLineKWP1281Lib::FAIL:
145+
Serial.print("Block ");
146+
Serial.print(block);
147+
Serial.println(" does not exist!");
148+
break;
149+
150+
case KLineKWP1281Lib::SUCCESS:
151+
//Will hold the measurement's units
152+
char units_string[16];
153+
154+
//Display the selected measurement.
155+
156+
/*
157+
The getMeasurementType() function can return:
158+
*KLineKWP1281Lib::UNKNOWN - index out of range (measurement doesn't exist in block)
159+
*KLineKWP1281Lib::UNITS - the measurement contains human-readable text in the units string
160+
*KLineKWP1281Lib::VALUE - "regular" measurement, with a value and units
161+
*/
162+
switch (KLineKWP1281Lib::getMeasurementType(measurement_index, amount_of_measurements, measurements, sizeof(measurements))) {
163+
//Value and units
164+
case KLineKWP1281Lib::VALUE:
165+
Serial.print(KLineKWP1281Lib::getMeasurementValue(measurement_index, amount_of_measurements, measurements, sizeof(measurements)));
166+
Serial.print(' ');
167+
Serial.println(KLineKWP1281Lib::getMeasurementUnits(measurement_index, amount_of_measurements, measurements, sizeof(measurements), units_string, sizeof(units_string)));
168+
break;
169+
170+
//Units string containing text
171+
case KLineKWP1281Lib::UNITS:
172+
Serial.println(KLineKWP1281Lib::getMeasurementUnits(measurement_index, amount_of_measurements, measurements, sizeof(measurements), units_string, sizeof(units_string)));
173+
break;
174+
175+
//Invalid measurement index
176+
case KLineKWP1281Lib::UNKNOWN:
177+
Serial.println("N/A");
178+
break;
179+
}
180+
break;
181+
}
182+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Initializes the serial port
2+
void beginFunction(unsigned long baud) {
3+
K_line.begin(baud);
4+
}
5+
6+
//Stops communication on the serial port
7+
void endFunction() {
8+
K_line.end();
9+
}
10+
11+
//Sends a byte
12+
void sendFunction(uint8_t data) {
13+
K_line.write(data);
14+
}
15+
16+
//Receives a byte
17+
bool receiveFunction(uint8_t &data) {
18+
if (K_line.available()) {
19+
data = K_line.read();
20+
return true;
21+
}
22+
return false;
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Select your target module address:
2+
#define connect_to_module 0x01
3+
4+
//Select your target module speed:
5+
#define module_baud_rate 9600
6+
7+
//Enable/disable printing library debug information on the Serial Monitor.
8+
#define debug_info false
9+
//Enable/disable printing bus traffic on the Serial Monitor.
10+
#define debug_traffic false
11+
12+
//Select whether or not your serial interface can receive at the same time as sending (or whether or not your K-line interface has echo as it should).
13+
//Most software serial libraries for AVR microcontrollers are half-duplex (can't receive while sending), so if using such library this should be false.
14+
#define is_full_duplex true
15+
16+
//Uncomment one of the following options:
17+
18+
//Arduino UNO (no additional hardware serial ports, must use software serial; AltSoftSerial was chosen as it is full-duplex)
19+
/*
20+
#include <AltSoftSerial.h>
21+
AltSoftSerial software_serial_port; //pins cannot be configured
22+
#define K_line software_serial_port
23+
#define TX_pin 9
24+
*/
25+
26+
//Arduino Mega (can use Serial1-Serial3)
27+
/*
28+
#define K_line Serial1
29+
#define TX_pin 18
30+
*/
31+
/*
32+
#define K_line Serial2
33+
#define TX_pin 16
34+
*/
35+
/*
36+
#define K_line Serial3
37+
#define TX_pin 14
38+
*/
39+
40+
//ESP32 (can use Serial2)
41+
/*
42+
#define K_line Serial2
43+
#define TX_pin 17
44+
*/
45+
46+
//ESP8266 (no additional hardware serial ports, must use software serial)
47+
//You can change the RX and TX pins to any unused pin, don't forget to also change the TX_pin define below.
48+
/*
49+
#include "SoftwareSerial.h"
50+
SoftwareSerial software_serial_port(4, 5); //D2, D1
51+
#define K_line software_serial_port
52+
#define TX_pin 5 //D1
53+
*/
54+
55+
#ifndef K_line
56+
#error Please select an option in configuration.h!
57+
#endif

0 commit comments

Comments
 (0)