Add CAN example
This commit is contained in:
parent
c59e8b96be
commit
2c44eb3bba
1 changed files with 30 additions and 0 deletions
|
@ -0,0 +1,30 @@
|
|||
#include <mcp2515.h>
|
||||
|
||||
MCP2515 mcp2515(10);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
mcp2515.reset();
|
||||
mcp2515.setBitrate(CAN_50KBPS);
|
||||
mcp2515.setNormalMode();
|
||||
Serial.println("begin");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
char message[9];
|
||||
message[8] = '\0';
|
||||
struct can_frame send_frame;
|
||||
struct can_frame receive_frame;
|
||||
int read = Serial.readBytesUntil('\n', send_frame.data, 8);
|
||||
if (read > 0) {
|
||||
send_frame.can_id = 0x001;
|
||||
send_frame.can_dlc = read;
|
||||
mcp2515.sendMessage(&send_frame);
|
||||
Serial.println("sent");
|
||||
}
|
||||
if (mcp2515.readMessage(&receive_frame) == MCP2515::ERROR_OK) {
|
||||
memcpy(message, receive_frame.data, 8);
|
||||
Serial.println(message);
|
||||
}
|
||||
Serial.println("Waiting for next message\n");
|
||||
}
|
Loading…
Reference in a new issue