Advertisement
microrobotics

Maduino Zero SAMD21G18A CAN-BUS Dev Board

Jul 3rd, 2023
2,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. You should type your messages as IDH,IDH,LEN,D0,D1,D2,D3,D4,D5,D6,D7\n, where:
  3. IDH,IDL are the high and low parts of the CAN ID, in hexadecimal.
  4. LEN is the data length (0 to 8).
  5. D0 to D7 are the data bytes, in hexadecimal.
  6. If you type 123,456,3,12,34,56, this will send a CAN message with ID 0x123456, length 3, and data bytes 0x12, 0x34, 0x56.
  7.  
  8. This code listens for user input. Once a newline is received, it parses the input and sends it as a CAN message. Note that this code does not handle errors in the input format, you should ensure you enter the data correctly.
  9. */
  10.  
  11. #include <mcp_can.h>
  12. #include <SPI.h>
  13.  
  14. MCP_CAN CAN(10); // Set CS to pin 10
  15. String inputString = "";
  16.  
  17. void setup() {
  18.     Serial.begin(115200);
  19.  
  20.     while (CAN_OK != CAN.begin(CAN_500KBPS)) {            // init can bus : baudrate = 500k
  21.         Serial.println("CAN BUS Shield init fail");
  22.         Serial.println(" Init CAN BUS Shield again");
  23.         delay(100);
  24.     }
  25.     Serial.println("CAN BUS Shield init ok!");
  26. }
  27.  
  28. void loop() {
  29.     unsigned char len = 0;
  30.     unsigned char buf[8];
  31.  
  32.     if (CAN_MSGAVAIL == CAN.checkReceive()) {            // check if data coming
  33.         CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf
  34.  
  35.         unsigned long canId = CAN.getCanId();
  36.  
  37.         Serial.println("-----------------------------");
  38.         Serial.println("Get data from CAN BUS");
  39.         Serial.print("CAN ID: ");
  40.         Serial.println(canId, HEX); // Prints ID in HEX format
  41.  
  42.         Serial.print("CAN Data: ");
  43.         for(int i = 0; i<len; i++) {    // print the data
  44.             Serial.print(buf[i], HEX); // Prints data bytes in HEX format
  45.             Serial.print("\t");
  46.         }
  47.         Serial.println();
  48.     }
  49.  
  50.     // check if there's any serial available
  51.     while (Serial.available()) {
  52.         char inChar = (char)Serial.read();
  53.         inputString += inChar;
  54.  
  55.         // if received character is a newline, parse the command
  56.         if (inChar == '\n') {
  57.             parseCommand(inputString);
  58.             inputString = "";
  59.         }
  60.     }
  61. }
  62.  
  63. void parseCommand(String command) {
  64.     // parse the command in the format: IDH,IDH,LEN,D0,D1,D2,D3,D4,D5,D6,D7
  65.     int parts[11];
  66.     int i = 0;
  67.  
  68.     int idx1 = 0;
  69.     int idx2 = command.indexOf(',');
  70.     while (idx2 != -1 && i < 11) {
  71.         parts[i++] = strtol(command.substring(idx1, idx2).c_str(), NULL, 16);
  72.         idx1 = idx2 + 1;
  73.         idx2 = command.indexOf(',', idx1);
  74.     }
  75.     if (i < 11) {
  76.         parts[i++] = strtol(command.substring(idx1).c_str(), NULL, 16);
  77.     }
  78.  
  79.     // sanity check
  80.     if (parts[2] > 8 || parts[2] < 0) {
  81.         Serial.println("Invalid length");
  82.         return;
  83.     }
  84.  
  85.     // prepare id and data
  86.     unsigned long canId = (parts[0] << 8) + parts[1];
  87.     unsigned char len = parts[2];
  88.     unsigned char data[8];
  89.     for (int i = 0; i < len; i++) {
  90.         data[i] = parts[3 + i];
  91.     }
  92.  
  93.     // send the message
  94.     CAN.sendMsgBuf(canId, 0, len, data);
  95.     Serial.println("Sent CAN message");
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement