CAN Communication

Which function to call to communicate over CAN on the portenta H7 and the breakout board?

read

#include <Arduino_CAN.h>

void setup(){
  Serial.begin(115200);
  while (!Serial) { }

  if (!CAN.begin(CanBitRate::BR_250k))  {
    Serial.println("CAN.begin(...) failed.");
    for (;;) {}
  }
}

void loop(){
  if (CAN.available())  {
    CanMsg const msg = CAN.read();
    Serial.println(msg);
  }
}

write

#include <Arduino_CAN.h>
static uint32_t const CAN_ID = 0x20;
static uint32_t msg_cnt = 0;

void setup(){
  Serial.begin(115200);
  while (!Serial) { }

  if (!CAN.begin(CanBitRate::BR_250k))  {
    Serial.println("CAN.begin(...) failed.");
    for (;;) {}
  }
}

void loop(){
  /* Assemble a CAN message with the format of
   * 0xCA 0xFE 0x00 0x00 [4 byte message counter]
   */
  uint8_t const msg_data[] = {0xCA,0xFE,0,0,0,0,0,0};
  memcpy((void *)(msg_data + 4), &msg_cnt, sizeof(msg_cnt));
  CanMsg msg(CanStandardId(CAN_ID), sizeof(msg_data), msg_data);

  /* Transmit the CAN message, capture and display an
   * error core in case of failure.
   */
  if (int const rc = CAN.write(msg); rc <= 0)
  {
    Serial.print  ("CAN.write(...) failed with error code ");
    Serial.println(rc);
    for (;;) { }
  }

  /* Increase the message counter. */
  msg_cnt++;

  /* Only send one message per second. */
  delay(1000);
}