FlexCAN message ID issue... Maybe data type?

Hi all! I'm trying to set up a simple CAN bus between two Teensy 3.5 boards each with a little board I made that includes the necessary circuitry to transmit and receive on a bus.

I'm just running some tests before trying to communicate with a Bosch motor and I seem to have a small issue.

I've set up the following simple programme to transmit and receive a simple message across the bus:

#include <FlexCAN.h> //Can Bus

//------------DIAGNOSTIC SERIAL DUMP------------
static uint8_t hex[17] = "0123456789abcdef";
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
  uint8_t working;
  while ( dumpLen-- ) {
    working = *bytePtr++;
    Serial.write( hex[ working >> 4 ] );
    Serial.write( hex[ working & 15 ] );
  }
  Serial.write('\r');
  Serial.write('\n');
}

//------------CAN Variables------------
CAN_message_t can_MsgRx, can_MsgTx;
bool CANWaiting = false;

void setup() {
  Serial.begin(9600);
  Can0.begin(500000);
  delay(500);


}

void loop() {

  CAN_SEND();
  delay(500);
  CAN_Read();
  delay(500);

}


void printFullBin(long long data) {
  for (int c = (sizeof(data) * 8) - 1; c >= 0; c--)
  {
    Serial.print((byte)(data >> c) & 1);
  }
  Serial.println();
}

void CAN_SEND() {
  can_MsgTx.ext = 0;
  can_MsgTx.id = 0x6001;
  can_MsgTx.len = 8;

  can_MsgTx.buf[0] = 0x01;
  can_MsgTx.buf[1] = 0x02;
  can_MsgTx.buf[2] = 0x03;
  can_MsgTx.buf[3] = 0x04;
  can_MsgTx.buf[4] = 0x05;
  can_MsgTx.buf[5] = 0x06;
  can_MsgTx.buf[6] = 0x07;
  can_MsgTx.buf[7] = 0x08;

  can_MsgTx.flags.extended = 0;
  can_MsgTx.flags.remote = 0;
  Serial.print("CAN SENT: "); Serial.print(can_MsgTx.id); Serial.print(" "); hexDump(8, can_MsgTx.buf); //Diagnostic - Dump message to Serial//@@@@@@@@@@@@@@@@@@@@@@@@@@@CAN DIAGNOSTICS@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  //Serial.println(currentMillis / 10);
  Can0.write(can_MsgTx); //Send message
}

void CAN_Read() {
  if (Can0.available() > 0) {//Is a response waiting?
    Can0.read(can_MsgRx);
    Serial.print("CAN Received: "); Serial.print(can_MsgRx.id); Serial.print(" "); hexDump(8, can_MsgRx.buf);
    //printFullBin(can_MsgRx.buf);
  }
}

After setting this up and reading the serial monitors, I can see that the data in the main buffer (can_MsgRx.buf) is received no problem at all, so the message is successfully sent and received across the two boards. I then tried to check the CAN message ID, but this does not seem to be received correctly.

On the transmit board, it reports that the message ID for the outgoing message is 0x6001 (24577).

On the receive board, the data is received correctly, but the message ID reads in the serial monitor as just 1.

I'm surprised that the data could arrive correctly but not the message ID. Is there anything obviously wrong with what I'm doing that could cause this. Is it that I'm reading the id wrong, or could it be a problem with FlexCAN? I'm using the Collin80 fork.

It sounds like a termination/grounding problem. Be sure the two boards share the same ground (CAN needs a reference ground to kill common mode noise). If this is OK then check if you have the correct termination resistor values with them placed at the physical ends of the bus.

Hmm I have termination resistors at both ends but I'm powering the two boards via USB cables plugged into the same laptop. Would that potentially be a problem? I suppose I could run a ground jumper between the two boards to ensure they are connected?

Ok after some testing, it seems I'm hitting the limit of something. If I change the message ID to 0x01, or 0xFF, or 0x601, these all come through no problem. If I use 0x6001 however (the address I need to use to communicate with the motor), only "1" is received.

I guess this must be because at some point in the transmission or reception of the message, the size of the integer is too large for the FlexCAN message ID container?

Looking through the attached library, it looks like in the header file, the ID data container is initialised with a data type of uint32_t, which should be plenty big enough..?

FlexCAN.cpp (37.7 KB)
FlexCAN.h (7.9 KB)

After some reading up, it seems that I have a 29 bit message ID, and to use that I need to set .flags.extended = 1

It now works! I'll leave the answer here in case anyone has the same issue :slight_smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.