-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino canbus
57 lines (51 loc) · 1.82 KB
/
arduino canbus
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
//OMOWANAI MIT//
#include <SPI.h>
#include <mcp_can.h>
MCP_CAN CAN(17); // Initialize MCP2515 CANbus module on SPI pin 17
void setup() {
Serial.begin(9600);
while (!Serial) {} // Wait for serial connection
if (CAN_OK == CAN.begin(CAN_500KBPS)) { // Initialize CANbus communication at 500 kbps
Serial.println("CANbus Initialized!");
} else {
Serial.println("CANbus Initialization Failed :(");
while (1);
}
}
void loop() {
// Broadcast a list of CANbus messages with different message IDs and data
byte data1[8] = {0x00, 0x9A, 0x23, 0x43, 0x00, 0x00, 0x09, 0x21};
CAN.sendMsgBuf(0x140, 0, 8, data1);
Serial.print("ID: 0x140 Data: ");
for (int i = 0; i < 8; i++) {
Serial.print(data1[i], HEX); // Print each byte of the message data in hexadecimal format
Serial.print(" ");
}
Serial.println();
byte data2[8] = {0x84, 0x26, 0xA3, 0x27, 0x00, 0x80, 0x2F, 0x00};
CAN.sendMsgBuf(0x141, 0, 8, data2);
Serial.print("ID: 0x141 Data: ");
for (int i = 0; i < 8; i++) {
Serial.print(data2[i], HEX); // Print each byte of the message data in hexadecimal format
Serial.print(" ");
}
Serial.println();
byte data3[8] = {0x84, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
CAN.sendMsgBuf(0x142, 0, 8, data3);
Serial.print("ID: 0x142 Data: ");
for (int i = 0; i < 8; i++) {
Serial.print(data3[i], HEX); // Print each byte of the message data in hexadecimal format
Serial.print(" ");
}
Serial.println();
byte data4[8] = {0x00, 0x2B, 0x00, 0x46, 0x00, 0x00, 0x00, 0xD9};
CAN.sendMsgBuf(0x18, 0, 8, data4);
Serial.print("ID: 0x18 Data: ");
for (int i = 0; i < 8; i++) {
Serial.print(data4[i], HEX); // Print each byte of the message data in hexadecimal format
Serial.print(" ");
}
Serial.println();
// Delay for 10 ms before sending the next batch of messages
delay(10);
}