CAN messages in Arduino_PortentaMachineControl.h

I'm having trouble setting message ID for outgoing CAN messages using the Arduino_PortentaMachineControl.h library.

My output seems to have the correct message body, but, I'm having trouble getting the correct message ID. I'm sending to 2FF when I'm trying to send to 18EF8AFF.

Also, if anyone knows the syntax to send in CAN extended format, that would be of help too.

Is there a resource showing more in depth information on how to use this library? I'm not seeing all the information I need in the library guide.

image

#include <Arduino_PortentaMachineControl.h>

void setup(){
  Serial.begin(9600);                                                     //Start serial communication
  while (!Serial) {                                                       //Wait for serial port to connect. Program waits for serial monitor to open before proceeding.                                                                  
  }
  if (!MachineControl_CANComm.begin(CanBitRate::BR_250k)) {
    Serial.println("Bitrate Set Failed");
    while(1);
  }
  else{
    Serial.println("Bitrate Set");
  }
}

int MessageID = 0x18EF8AFF;                                             
unsigned char payload[] = {0xFD,0xFF,0xFF,0xBF,0xFF,0xFF,0xFF,0xFF};       
int payload_size = 8;                                                     

void loop(){
  CanMsg msg(MessageID, 8, payload);
  int const rc = MachineControl_CANComm.write(msg);
  if (rc <= 0) {
    Serial.println("Failure");
    Serial.print(msg);
    while(1);
  }
  else{
    Serial.println("Success");
    Serial.println(msg);
    while(1);
  }
}
1 Like

I found a solution by looking at the library examples for the Portenta C33 instead of the H7. There was a function used in the code "CanStandardID(CAN_ID)" that isn't in the examples for the H7.

The original code was truncating my MessageID. I was passing an extended format ID when the default for CanMsg() appears to be standard format (makes sense).

When you send the MessageID, you should specify whether it is standard or extended format when using CanMsg msg(). This is done by writing CanStandardId(MessageID) or CanExtendedId(MessageID).

Incorrect code:
CanMsg msg(MessageID, 8, payload);
Correct code:
CanMsg const msg(CanExtendedId(MessageID), 8, payload);

1 Like