-
Notifications
You must be signed in to change notification settings - Fork 125
/
ELMduino.cpp
511 lines (377 loc) · 8.72 KB
/
ELMduino.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include "ELMduino.h"
/*
bool ELM327::begin(Stream &stream)
Description:
------------
* Constructor for the ELM327 Class; initializes ELM327
Inputs:
-------
* Stream &stream - Pointer to Serial port connected
to ELM327
Return:
-------
* bool - Whether or not the ELM327 was propperly
initialized
*/
bool ELM327::begin(Stream &stream)
{
elm_port = &stream;
while (!elm_port);
// wait 3 sec for the ELM327 to initialize
delay(3000);
// try to connect
while (!initializeELM())
delay(1000);
delay(100);
return true;
}
/*
bool ELM327::initializeELM()
Description:
------------
* Initializes ELM327
Inputs:
-------
* void
Return:
-------
* bool - Whether or not the ELM327 was propperly
initialized
Notes:
------
* Protocol - Description
* 0 - Automatic
* 1 - SAE J1850 PWM (41.6 kbaud)
* 2 - SAE J1850 PWM (10.4 kbaud)
* 4 - ISO 9141-2 (5 baud init)
* 5 - ISO 14230-4 KWP (5 baud init)
* 6 - ISO 14230-4 KWP (fast init)
* 7 - ISO 15765-4 CAN (11 bit ID, 500 kbaud)
* 8 - ISO 15765-4 CAN (29 bit ID, 500 kbaud)
* 9 - ISO 15765-4 CAN (11 bit ID, 250 kbaud)
* A - ISO 15765-4 CAN (29 bit ID, 250 kbaud)
* B - User1 CAN (11* bit ID, 125* kbaud)
* C - User2 CAN (11* bit ID, 50* kbaud)
* --> *user adjustable
*/
bool ELM327::initializeELM()
{
char *match;
sendCommand(ECHO_OFF);
delay(100);
if (sendCommand("AT SP 0") == ELM_SUCCESS) // automatic protocol
{
match = strstr(payload, "OK");
if (match != NULL)
connected = true;
else
connected = false;
}
else
connected = false;
return connected;
}
/*
void ELM327::formatQueryArray(uint16_t service, uint16_t pid)
Description:
------------
* Creates a query stack to be sent to ELM327
Inputs:
-------
* uint16_t service - Service number of the queried PID
* uint16_t pid - PID number of the queried PID
Return:
-------
* void
*/
void ELM327::formatQueryArray(uint16_t service, uint16_t pid)
{
query[0] = ((service >> 8) & 0xFF) + '0';
query[1] = (service & 0xFF) + '0';
query[2] = ((pid >> 8) & 0xFF) + '0';
query[3] = (pid & 0xFF) + '0';
query[4] = '\n';
query[5] = '\r';
upper(query, 6);
}
/*
void ELM327::upper(char string[],
uint8_t buflen)
Description:
------------
* Converts all elements of char array string[] to
uppercase ascii
Inputs:
-------
* uint8_t string[] - Char array
* uint8_t buflen - Length of char array
Return:
-------
* void
*/
void ELM327::upper(char string[],
uint8_t buflen)
{
for (uint8_t i = 0; i < buflen; i++)
{
if (string[i] > 'Z')
string[i] -= 32;
else if ((string[i] > '9') && (string[i] < 'A'))
string[i] += 7;
}
}
/*
bool ELM327::timeout()
Description:
------------
* Determines if a time-out has occurred
Inputs:
-------
* uint8_t string[] - Char array
* uint8_t buflen - Length of char array
Return:
-------
* bool - whether or not a time-out has occurred
*/
bool ELM327::timeout()
{
currentTime = millis();
if ((currentTime - previousTime) >= timeout_ms)
return true;
return false;
}
/*
uint8_t ELM327::ctoi(uint8_t value)
Description:
------------
* converts a decimal or hex char to an int
Inputs:
-------
* uint8_t value - char to be converted
Return:
-------
* uint8_t - int value of parameter "value"
*/
uint8_t ELM327::ctoi(uint8_t value)
{
if (value >= 'A')
return value - 'A' + 10;
else
return value - '0';
}
/*
void ELM327::flushInputBuff()
Description:
------------
* Flushes input serial buffer
Inputs:
-------
* void
Return:
-------
* void
*/
void ELM327::flushInputBuff()
{
while (elm_port->available())
elm_port->read();
}
/*
bool ELM327::queryPID(uint8_t service,
uint8_t PID,
uint8_t payloadSize,
float &value)
Description:
------------
* Queries ELM327 for a specific type of vehicle telemetry data
Inputs:
-------
* uint8_t service - Service number
* uint8_t PID - PID number
Return:
-------
* bool - Whether or not the query was submitted successfully
*/
bool ELM327::queryPID(uint16_t service,
uint16_t pid)
{
if (connected)
{
// determine the string needed to be passed to the OBD scanner to make the query
formatQueryArray(service, pid);
// make the query
status = sendCommand(query);
return true;
}
return false;
}
/*
int32_t ELM327::kph()
Description:
------------
* Queries and parses received message for/returns vehicle speed data (kph)
Inputs:
-------
* void
Return:
-------
* int32_t - Vehicle speed in kph
*/
int32_t ELM327::kph()
{
if (queryPID(SERVICE_01, VEHICLE_SPEED))
return findResponse(false);
return ELM_GENERAL_ERROR;
}
/*
float ELM327::mph()
Description:
------------
* Queries and parses received message for/returns vehicle speed data (mph)
Inputs:
-------
* void
Return:
-------
* float - Vehicle speed in mph
*/
float ELM327::mph()
{
float mph = kph();
if (status == ELM_SUCCESS)
return mph * KPH_MPH_CONVERT;
return ELM_GENERAL_ERROR;
}
/*
float ELM327::rpm()
Description:
------------
* Queries and parses received message for/returns vehicle RMP data
Inputs:
-------
* void
Return:
-------
* uint32_t - Vehicle RPM
*/
float ELM327::rpm()
{
if (queryPID(SERVICE_01, ENGINE_RPM))
return (findResponse(true) / 4);
return ELM_GENERAL_ERROR;
}
/*
int8_t ELM327::sendCommand(const char *cmd)
Description:
------------
* Sends a command/query and reads/buffers the ELM327's response
Inputs:
-------
* const char *cmd - Command/query to send to ELM327
Return:
-------
* int8_t - Response status
*/
int8_t ELM327::sendCommand(const char *cmd)
{
uint8_t counter = 0;
// flush the input buffer
flushInputBuff();
// send the command with carriage return
elm_port->print(cmd);
elm_port->print('\r');
// prime the timer
previousTime = millis();
currentTime = previousTime;
for (byte i = 0; i < PAYLOAD_LEN; i++)
payload[i] = '\0';
// buffer the response of the ELM327 until either the
//end marker is read or a timeout has occurred
while ((counter < (PAYLOAD_LEN + 1)) && !timeout())
{
if (elm_port->available())
{
payload[counter] = elm_port->read();
if (payload[counter] == '>')
break;
else
counter++;
}
}
if (timeout())
return ELM_UNABLE_TO_CONNECT;
char *match;
match = strstr(payload, "UNABLE TO CONNECT");
if (match != NULL)
{
for (byte i = 0; i < PAYLOAD_LEN; i++)
payload[i] = '\0';
return ELM_UNABLE_TO_CONNECT;
}
match = strstr(payload, "NO DATA");
if (match != NULL)
{
for (byte i = 0; i < PAYLOAD_LEN; i++)
payload[i] = '\0';
return ELM_NO_DATA;
}
return ELM_SUCCESS;
}
/*
int8_t ELM327::findResponse(bool longResponse)
Description:
------------
* Parses the buffered ELM327's response and returns the queried data
Inputs:
-------
* bool longResponse - Some responses are 2 hex digits wide and others are 4.
If the response is expected to be 2 hex digits wide, set longResponse to false,
else set longResponse to true
Return:
-------
* int8_t - Response status
*/
int ELM327::findResponse(bool longResponse)
{
byte maxIndex = 1;
int dataLoc = 0;
int response = 0;
char header[5];
char data[4];
header[0] = '4';
header[1] = query[1];
header[2] = ' ';
header[3] = query[2];
header[4] = query[3];
Serial.print("Received: ");
Serial.write((const uint8_t*)payload, PAYLOAD_LEN);
Serial.println();
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
for (byte i = 0; i < (PAYLOAD_LEN + 5); i++)
{
if (payload[i] == header[0] &&
payload[i + 1] == header[1] &&
payload[i + 2] == header[2] &&
payload[i + 3] == header[3] &&
payload[i + 4] == header[4])
dataLoc = i + 6;
}
#pragma GCC diagnostic pop
if (dataLoc > 0)
{
data[0] = ctoi(payload[dataLoc]);
data[1] = ctoi(payload[dataLoc + 1]);
if (longResponse)
{
data[2] = ctoi(payload[dataLoc + 3]);
data[3] = ctoi(payload[dataLoc + 4]);
maxIndex = 3;
}
for (int i = maxIndex; i >= 0; i--)
response += data[i] * pow(16, (maxIndex - i));
}
else
Serial.println("Header NOT found");
return response;
}