-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patharduino-can-reader.ino
57 lines (41 loc) · 1.07 KB
/
arduino-can-reader.ino
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
#include <SPI.h>
#include "mcp_can.h"
///////////////////
// Configuration //
///////////////////
// CS pin for CAN bus shield.
// The default pin depends on the shield's version:
// - 1.0: digital pin 10
// - 1.1 and newer: digital pin 9
const int CS_PIN = 9;
// Serial port data rate
const long SERIAL_SPEED = 115200;
// CAN bus data rate
const byte CAN_SPEED = CAN_125KBPS;
///////////////////
MCP_CAN CAN(CS_PIN);
unsigned char len = 0;
byte buffer[8];
void setup() {
Serial.begin(SERIAL_SPEED);
byte canSpeed = CAN_SPEED;
while (CAN.begin(canSpeed) != CAN_OK) {
delay(100);
}
}
void loop() {
if (CAN.checkReceive() == CAN_MSGAVAIL) {
CAN.readMsgBuf(&len, buffer);
Serial.print("FRAME:ID=");
Serial.print(CAN.getCanId());
Serial.print(":LEN=");
Serial.print(len);
char tmp[3];
for(int i = 0; i<len; i++) {
Serial.print(":");
snprintf(tmp, 3, "%02X", buffer[i]);
Serial.print(tmp);
}
Serial.println();
}
}