9 years ago.

Compare Integer #CAN #Format

I'm writing a program, which reads CAN-data (ID,DLC,DATA) from a text document and send it as a CAN message to the Transceiver. It works fine. I just need a bit help to compare my ID's.

I Have to know if the ID is a Stadard-Frame (11-bit) or an Extended-Frame Format (29-bit).

There are Integer like this (Extended): 1B000012x, 1BFC0C11x, 1BFC0C21x.

And Integer like that (Standard): 86, B4, 121.

Only if I know which Format the ID is I can decide which "send-function" I use. So I need and IF-condition.

Now the question: What is the best way to check if it is a Stadard or Extended Format? You have a hint for me?

Many Thanks Marv

1 Answer

9 years ago.

So extended IDs all end in an x?

If so and assuming you have the ID in a standard c string format then

int idLength = strlen(IDString);
if ( *(IDString+idLength-1) == 'x' ) {
  message.format = CANExtended;
  message.id = strtol(IDString,IDString+idLength-1,16);
} else {
  message.format = CANStandard;
  message.id = strtol(IDString,null,16);
}

should set the ID and format for an mbed standard CANMessage data structure.