From c796b761e53893687c963e291b3e3f03dc8f0eec Mon Sep 17 00:00:00 2001 From: stephen1874 Date: Sat, 8 Jan 2022 17:18:03 +0800 Subject: [PATCH 1/3] update obd demo --- examples/OBDII_PIDs/OBDII_PIDs.ino | 155 ------------------ examples/OBDII_PIDs/getRpm/getRpm.ino | 120 ++++++++++++++ examples/OBDII_PIDs/getSpeed/getSpeed.ino | 116 +++++++++++++ examples/recv/recv.ino | 21 +-- examples/send/send.ino | 20 +-- .../set_mask_filter_recv.ino | 22 +-- .../set_mask_filter_send.ino | 23 ++- 7 files changed, 279 insertions(+), 198 deletions(-) delete mode 100644 examples/OBDII_PIDs/OBDII_PIDs.ino create mode 100644 examples/OBDII_PIDs/getRpm/getRpm.ino create mode 100644 examples/OBDII_PIDs/getSpeed/getSpeed.ino diff --git a/examples/OBDII_PIDs/OBDII_PIDs.ino b/examples/OBDII_PIDs/OBDII_PIDs.ino deleted file mode 100644 index 94770bb..0000000 --- a/examples/OBDII_PIDs/OBDII_PIDs.ino +++ /dev/null @@ -1,155 +0,0 @@ -/************************************************************************************************* - OBD-II_PIDs TEST CODE - LOOVEE @ JUN24, 2017 - - Query - send id: 0x7df - dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 - - Response - From id: 0x7E9 or 0x7EA or 0x7EB - dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) - - https://en.wikipedia.org/wiki/OBD-II_PIDs - - Input a PID, then you will get reponse from vehicle, the input should be end with '\n' -***************************************************************************************************/ -#include -#include "mcp_can.h" - -// Please modify SPI_CS_PIN to adapt to different baords. - -const int SPI_CS_PIN = 17; // CANBed V1 -// const int SPI_CS_PIN = 3; // CANBed M0 -/* - CAN Bus Shield - CANBed 2040 - CANBed Dual - OBD-2G Dev Kit - OBD-II Hud Dev Kit -*/ -//const int SPI_CS_PIN = 9; // CAN Bus Shield - -MCP_CAN CAN(SPI_CS_PIN); // Set CS pin - -#define PID_ENGIN_PRM 0x0C -#define PID_VEHICLE_SPEED 0x0D -#define PID_COOLANT_TEMP 0x05 - -#define CAN_ID_PID 0x7DF - - -unsigned char PID_INPUT; -unsigned char getPid = 0; - -void set_mask_filt() -{ - /* - * set mask, set both the mask to 0x3ff - */ - CAN.init_Mask(0, 0, 0x7FC); - CAN.init_Mask(1, 0, 0x7FC); - - /* - * set filter, we can receive id from 0x04 ~ 0x09 - */ - CAN.init_Filt(0, 0, 0x7E8); - CAN.init_Filt(1, 0, 0x7E8); - - CAN.init_Filt(2, 0, 0x7E8); - CAN.init_Filt(3, 0, 0x7E8); - CAN.init_Filt(4, 0, 0x7E8); - CAN.init_Filt(5, 0, 0x7E8); -} - -void sendPid(unsigned char __pid) -{ - unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; - Serial.print("SEND PID: 0x"); - Serial.println(__pid, HEX); - CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); -} - -void setup() -{ - Serial.begin(115200); - while(!Serial); - - // below code need for OBD-II GPS Dev Kit - // pinMode(A3, OUTPUT); - // digitalWrite(A3, HIGH); - - while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k - { - Serial.println("CAN BUS FAIL!"); - delay(100); - } - Serial.println("CAN BUS OK!"); - set_mask_filt(); -} - - -void loop() -{ - taskCanRecv(); - taskDbg(); - - if(getPid) // GET A PID - { - getPid = 0; - sendPid(PID_INPUT); - PID_INPUT = 0; - } -} - -void taskCanRecv() -{ - unsigned char len = 0; - unsigned char buf[8]; - - if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data - { - CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf - - Serial.println("\r\n------------------------------------------------------------------"); - Serial.print("Get Data From id: "); - Serial.println(CAN.getCanId(), HEX); - for(int i = 0; i='0' && c<='9') - { - PID_INPUT *= 0x10; - PID_INPUT += c-'0'; - - } - else if(c>='A' && c<='F') - { - PID_INPUT *= 0x10; - PID_INPUT += 10+c-'A'; - } - else if(c>='a' && c<='f') - { - PID_INPUT *= 0x10; - PID_INPUT += 10+c-'a'; - } - else if(c == '\n') // END - { - getPid = 1; - } - } -} -// END FILE diff --git a/examples/OBDII_PIDs/getRpm/getRpm.ino b/examples/OBDII_PIDs/getRpm/getRpm.ino new file mode 100644 index 0000000..345e97e --- /dev/null +++ b/examples/OBDII_PIDs/getRpm/getRpm.ino @@ -0,0 +1,120 @@ +/************************************************************************************************* + OBD-II_PIDs TEST CODE + Loovee, Longan Labs 2022 + + Query + send id: 0x7df + dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 + + Response + From id: 0x7E9 or 0x7EA or 0x7EB + dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) + + https://en.wikipedia.org/wiki/OBD-II_PIDs +***************************************************************************************************/ + +#include +#include "mcp_can.h" + +/* Please modify SPI_CS_PIN to adapt to different baords. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +#define PID_ENGIN_PRM 0x0C +#define PID_VEHICLE_SPEED 0x0D +#define PID_COOLANT_TEMP 0x05 + +#define CAN_ID_PID 0x7DF + +void set_mask_filt() +{ + // set mask, set both the mask to 0x3ff + + CAN.init_Mask(0, 0, 0x7FC); + CAN.init_Mask(1, 0, 0x7FC); + + // set filter, we can receive id from 0x04 ~ 0x09 + + CAN.init_Filt(0, 0, 0x7E8); + CAN.init_Filt(1, 0, 0x7E8); + + CAN.init_Filt(2, 0, 0x7E8); + CAN.init_Filt(3, 0, 0x7E8); + CAN.init_Filt(4, 0, 0x7E8); + CAN.init_Filt(5, 0, 0x7E8); +} + +void sendPid(unsigned char __pid) { + unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; + CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); +} + +bool getRPM(int *r) +{ + sendPid(PID_ENGIN_PRM); + unsigned long __timeout = millis(); + + while(millis()-__timeout < 1000) // 1s time out + { + unsigned char len = 0; + unsigned char buf[8]; + + if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + if(buf[1] == 0x41) + { + *r = (256*buf[3]+buf[4])/4; + return 1; + } + } + } + + return 0; +} + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k + Serial.println("CAN init fail, retry..."); + delay(100); + } + Serial.println("CAN init ok!"); + set_mask_filt(); +} + +void loop() { + + int __rpm = 0; + + int ret = getRPM(&__rpm); + + if(ret) + { + Serial.print("Engin Speed: "); + Serial.print(__rpm); + Serial.println(" rpm"); + }else Serial.println("get Engin Speed Fail..."); + + delay(500); +} + +// END FILE \ No newline at end of file diff --git a/examples/OBDII_PIDs/getSpeed/getSpeed.ino b/examples/OBDII_PIDs/getSpeed/getSpeed.ino new file mode 100644 index 0000000..b178915 --- /dev/null +++ b/examples/OBDII_PIDs/getSpeed/getSpeed.ino @@ -0,0 +1,116 @@ +/************************************************************************************************* + OBD-II_PIDs TEST CODE + Loovee, Longan Labs 2022 + + Query + send id: 0x7df + dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 + + Response + From id: 0x7E9 or 0x7EA or 0x7EB + dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) + + https://en.wikipedia.org/wiki/OBD-II_PIDs +***************************************************************************************************/ + +#include +#include "mcp_can.h" + +/* Please modify SPI_CS_PIN to adapt to different baords. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +#define PID_ENGIN_PRM 0x0C +#define PID_VEHICLE_SPEED 0x0D +#define PID_COOLANT_TEMP 0x05 + +#define CAN_ID_PID 0x7DF + +void set_mask_filt() +{ + // set mask, set both the mask to 0x3ff + + CAN.init_Mask(0, 0, 0x7FC); + CAN.init_Mask(1, 0, 0x7FC); + + // set filter, we can receive id from 0x04 ~ 0x09 + + CAN.init_Filt(0, 0, 0x7E8); + CAN.init_Filt(1, 0, 0x7E8); + + CAN.init_Filt(2, 0, 0x7E8); + CAN.init_Filt(3, 0, 0x7E8); + CAN.init_Filt(4, 0, 0x7E8); + CAN.init_Filt(5, 0, 0x7E8); +} + +void sendPid(unsigned char __pid) { + unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; + CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); +} + +bool getSpeed(int *s) +{ + sendPid(PID_ENGIN_PRM); + unsigned long __timeout = millis(); + + while(millis()-__timeout < 1000) // 1s time out + { + unsigned char len = 0; + unsigned char buf[8]; + + if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + if(buf[1] == 0x41) + { + *s = buf[3]; + return 1; + } + } + } + + return 0; +} + +void setup() { + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k + Serial.println("CAN init fail, retry..."); + delay(100); + } + Serial.println("CAN init ok!"); + set_mask_filt(); +} + +void loop() { + + int __speed = 0; + int ret = getSpeed(&__speed); + if(ret) + { + Serial.print("Vehicle Speed: "); + Serial.print(__speed); + Serial.println(" kmh"); + } + delay(500); +} + +// END FILE \ No newline at end of file diff --git a/examples/recv/recv.ino b/examples/recv/recv.ino index 1a47feb..b04126b 100644 --- a/examples/recv/recv.ino +++ b/examples/recv/recv.ino @@ -31,18 +31,19 @@ #include #include "mcp_can.h" -// Please modify SPI_CS_PIN to adapt to different baords. +/* Please modify SPI_CS_PIN to adapt to different baords. -const int SPI_CS_PIN = 17; // CANBed V1 -// const int SPI_CS_PIN = 3; // CANBed M0 -/* - CAN Bus Shield - CANBed 2040 - CANBed Dual - OBD-2G Dev Kit - OBD-II Hud Dev Kit + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 */ -//const int SPI_CS_PIN = 9; + + +#define SPI_CS_PIN 9 MCP_CAN CAN(SPI_CS_PIN); // Set CS pin diff --git a/examples/send/send.ino b/examples/send/send.ino index 89370cb..8788856 100644 --- a/examples/send/send.ino +++ b/examples/send/send.ino @@ -31,18 +31,18 @@ #include #include -// Please modify SPI_CS_PIN to adapt to different baords. +/* Please modify SPI_CS_PIN to adapt to different baords. -const int SPI_CS_PIN = 17; // CANBed V1 -// const int SPI_CS_PIN = 3; // CANBed M0 -/* - CAN Bus Shield - CANBed 2040 - CANBed Dual - OBD-2G Dev Kit - OBD-II Hud Dev Kit + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 */ -//const int SPI_CS_PIN = 9; + +#define SPI_CS_PIN 9 MCP_CAN CAN(SPI_CS_PIN); // Set CS pin diff --git a/examples/set_mask_filter_recv/set_mask_filter_recv.ino b/examples/set_mask_filter_recv/set_mask_filter_recv.ino index ed09cf1..8845bd6 100644 --- a/examples/set_mask_filter_recv/set_mask_filter_recv.ino +++ b/examples/set_mask_filter_recv/set_mask_filter_recv.ino @@ -34,18 +34,18 @@ #include #include "mcp_can.h" -// Please modify SPI_CS_PIN to adapt to different baords. - -const int SPI_CS_PIN = 17; // CANBed V1 -// const int SPI_CS_PIN = 3; // CANBed M0 -/* - CAN Bus Shield - CANBed 2040 - CANBed Dual - OBD-2G Dev Kit - OBD-II Hud Dev Kit +/* Please modify SPI_CS_PIN to adapt to different baords. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 */ -//const int SPI_CS_PIN = 9; + +#define SPI_CS_PIN 9 MCP_CAN CAN(SPI_CS_PIN); // Set CS pin diff --git a/examples/set_mask_filter_send/set_mask_filter_send.ino b/examples/set_mask_filter_send/set_mask_filter_send.ino index db2059a..a120bac 100644 --- a/examples/set_mask_filter_send/set_mask_filter_send.ino +++ b/examples/set_mask_filter_send/set_mask_filter_send.ino @@ -26,25 +26,24 @@ CANBed V1: https://www.longan-labs.cc/1030008.html CANBed M0: https://www.longan-labs.cc/1030014.html CAN Bus Shield: https://www.longan-labs.cc/1030016.html - OBD-II CAN Bus GPS Dev Kit: https://www.longan-labs.cc/1030003.html - + OBD-II CAN Bus GPS Dev Kit: https://www.longan-labs.cc/1030003.html */ #include #include -// Please modify SPI_CS_PIN to adapt to different baords. +/* Please modify SPI_CS_PIN to adapt to different baords. -const int SPI_CS_PIN = 17; // CANBed V1 -// const int SPI_CS_PIN = 3; // CANBed M0 -/* - CAN Bus Shield - CANBed 2040 - CANBed Dual - OBD-2G Dev Kit - OBD-II Hud Dev Kit + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 */ -//const int SPI_CS_PIN = 9; + +#define SPI_CS_PIN 9 MCP_CAN CAN(SPI_CS_PIN); // Set CS pin From 9bce67968d1b6597b98b506cb48efca62d8b1648 Mon Sep 17 00:00:00 2001 From: stephen1874 Date: Sat, 8 Jan 2022 18:45:14 +0800 Subject: [PATCH 2/3] update obd demo --- examples/OBDII_PIDs/getSpeed/getSpeed.ino | 2 +- examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak | 116 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak diff --git a/examples/OBDII_PIDs/getSpeed/getSpeed.ino b/examples/OBDII_PIDs/getSpeed/getSpeed.ino index b178915..294af79 100644 --- a/examples/OBDII_PIDs/getSpeed/getSpeed.ino +++ b/examples/OBDII_PIDs/getSpeed/getSpeed.ino @@ -62,7 +62,7 @@ void sendPid(unsigned char __pid) { bool getSpeed(int *s) { - sendPid(PID_ENGIN_PRM); + sendPid(PID_VEHICLE_SPEED); unsigned long __timeout = millis(); while(millis()-__timeout < 1000) // 1s time out diff --git a/examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak b/examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak new file mode 100644 index 0000000..b178915 --- /dev/null +++ b/examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak @@ -0,0 +1,116 @@ +/************************************************************************************************* + OBD-II_PIDs TEST CODE + Loovee, Longan Labs 2022 + + Query + send id: 0x7df + dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 + + Response + From id: 0x7E9 or 0x7EA or 0x7EB + dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) + + https://en.wikipedia.org/wiki/OBD-II_PIDs +***************************************************************************************************/ + +#include +#include "mcp_can.h" + +/* Please modify SPI_CS_PIN to adapt to different baords. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + Hud Dev Kit - 9 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +#define PID_ENGIN_PRM 0x0C +#define PID_VEHICLE_SPEED 0x0D +#define PID_COOLANT_TEMP 0x05 + +#define CAN_ID_PID 0x7DF + +void set_mask_filt() +{ + // set mask, set both the mask to 0x3ff + + CAN.init_Mask(0, 0, 0x7FC); + CAN.init_Mask(1, 0, 0x7FC); + + // set filter, we can receive id from 0x04 ~ 0x09 + + CAN.init_Filt(0, 0, 0x7E8); + CAN.init_Filt(1, 0, 0x7E8); + + CAN.init_Filt(2, 0, 0x7E8); + CAN.init_Filt(3, 0, 0x7E8); + CAN.init_Filt(4, 0, 0x7E8); + CAN.init_Filt(5, 0, 0x7E8); +} + +void sendPid(unsigned char __pid) { + unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; + CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); +} + +bool getSpeed(int *s) +{ + sendPid(PID_ENGIN_PRM); + unsigned long __timeout = millis(); + + while(millis()-__timeout < 1000) // 1s time out + { + unsigned char len = 0; + unsigned char buf[8]; + + if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + if(buf[1] == 0x41) + { + *s = buf[3]; + return 1; + } + } + } + + return 0; +} + +void setup() { + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k + Serial.println("CAN init fail, retry..."); + delay(100); + } + Serial.println("CAN init ok!"); + set_mask_filt(); +} + +void loop() { + + int __speed = 0; + int ret = getSpeed(&__speed); + if(ret) + { + Serial.print("Vehicle Speed: "); + Serial.print(__speed); + Serial.println(" kmh"); + } + delay(500); +} + +// END FILE \ No newline at end of file From a0fab5fcbab4ebe1cf81e6d09dc1c843c58de0d3 Mon Sep 17 00:00:00 2001 From: stephen1874 Date: Sat, 8 Jan 2022 18:45:35 +0800 Subject: [PATCH 3/3] update obd demo --- examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak | 116 ------------------ 1 file changed, 116 deletions(-) delete mode 100644 examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak diff --git a/examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak b/examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak deleted file mode 100644 index b178915..0000000 --- a/examples/OBDII_PIDs/getSpeed/getSpeed.ino.bak +++ /dev/null @@ -1,116 +0,0 @@ -/************************************************************************************************* - OBD-II_PIDs TEST CODE - Loovee, Longan Labs 2022 - - Query - send id: 0x7df - dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 - - Response - From id: 0x7E9 or 0x7EA or 0x7EB - dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) - - https://en.wikipedia.org/wiki/OBD-II_PIDs -***************************************************************************************************/ - -#include -#include "mcp_can.h" - -/* Please modify SPI_CS_PIN to adapt to different baords. - - CANBed V1 - 17 - CANBed M0 - 3 - CAN Bus Shield - 9 - CANBed 2040 - 9 - CANBed Dual - 9 - OBD-2G Dev Kit - 9 - Hud Dev Kit - 9 -*/ - -#define SPI_CS_PIN 9 - -MCP_CAN CAN(SPI_CS_PIN); // Set CS pin - -#define PID_ENGIN_PRM 0x0C -#define PID_VEHICLE_SPEED 0x0D -#define PID_COOLANT_TEMP 0x05 - -#define CAN_ID_PID 0x7DF - -void set_mask_filt() -{ - // set mask, set both the mask to 0x3ff - - CAN.init_Mask(0, 0, 0x7FC); - CAN.init_Mask(1, 0, 0x7FC); - - // set filter, we can receive id from 0x04 ~ 0x09 - - CAN.init_Filt(0, 0, 0x7E8); - CAN.init_Filt(1, 0, 0x7E8); - - CAN.init_Filt(2, 0, 0x7E8); - CAN.init_Filt(3, 0, 0x7E8); - CAN.init_Filt(4, 0, 0x7E8); - CAN.init_Filt(5, 0, 0x7E8); -} - -void sendPid(unsigned char __pid) { - unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; - CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); -} - -bool getSpeed(int *s) -{ - sendPid(PID_ENGIN_PRM); - unsigned long __timeout = millis(); - - while(millis()-__timeout < 1000) // 1s time out - { - unsigned char len = 0; - unsigned char buf[8]; - - if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data - CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf - - if(buf[1] == 0x41) - { - *s = buf[3]; - return 1; - } - } - } - - return 0; -} - -void setup() { - Serial.begin(115200); - while(!Serial); - - // below code need for OBD-II GPS Dev Kit - // pinMode(A3, OUTPUT); - // digitalWrite(A3, HIGH); - - while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k - Serial.println("CAN init fail, retry..."); - delay(100); - } - Serial.println("CAN init ok!"); - set_mask_filt(); -} - -void loop() { - - int __speed = 0; - int ret = getSpeed(&__speed); - if(ret) - { - Serial.print("Vehicle Speed: "); - Serial.print(__speed); - Serial.println(" kmh"); - } - delay(500); -} - -// END FILE \ No newline at end of file