autowp / arduino-mcp2515

Arduino MCP2515 CAN interface library
MIT License
795 stars 279 forks source link

How to know if received frame is standard frame or extended frame? #56

Closed Akshay1595 closed 3 years ago

Akshay1595 commented 3 years ago

I want to know if my received frame is the standard or extended frame.

I have the following code to receive a can frame. ` while ((mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)) { debug_log("I got some data!");

    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");

    for (int i = 0; i < 8; i++)  {  // print the data
        Serial.print(canMsg.data[i],HEX);
        Serial.print(" ");
    }
    Serial.println(" ");
    //debug_log(msg_string);
}

`

I also want to display the frame format of the above CAN message. How to do that?

Akshay1595 commented 3 years ago

Hey No worries!

Sorted! I found this comment line!

/*
 * Controller Area Network Identifier structure
 *
 * bit 0-28 : CAN identifier (11/29 bit)
 * bit 29   : error message frame flag (0 = data frame, 1 = error message)
 * bit 30   : remote transmission request flag (1 = rtr frame)
 * bit 31   : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
 */
typedef __u32 canid_t;

We can check bit 31 and decide if it is a Standard or extended frame! Also one can check if it is an error message or data frame as well as one can check if it is an RTR frame.

Thanks for the comment inside the code! I would suggest using bitfields for such cases and provide in code so that coding will be easier!

jxltom commented 3 years ago

Hey, the CAN_EFF_MASK or CAN_EFF_FLAG https://github.com/autowp/arduino-mcp2515/blob/cdc0141ee734f47f54628b1ac8acaafa9120511b/can.h#L19 might help.