Advertisement
pleasedontcode

CAN message receiver

Jul 27th, 2024
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 0.58 KB | Source Code | 0 0
  1. #include <mcp_can.h>
  2. #include <SPI.h>
  3.  
  4. const int SPI_CS_PIN = 10;
  5. MCP_CAN CAN(SPI_CS_PIN);
  6.  
  7. void setup() {
  8.   Serial.begin(115200);
  9.   while (CAN_OK != CAN.begin(CAN_500KBPS)) {
  10.     Serial.println("CAN BUS Shield init fail");
  11.     delay(100);
  12.   }
  13.   Serial.println("CAN BUS Shield init ok!");
  14. }
  15.  
  16. void loop() {
  17.   unsigned char len = 0;
  18.   unsigned char buf[8];
  19.   if (CAN_MSGAVAIL == CAN.checkReceive()) {
  20.     CAN.readMsgBuf(&len, buf);
  21.     Serial.print("Message received: ");
  22.     for (int i = 0; i < len; i++) {
  23.       Serial.print((char)buf[i]);
  24.     }
  25.     Serial.println();
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement