0

I am trying to read from a socketCAN and the message is always filtered for the 29bit identifier. I meet a terrible problem that i can't deal with it.

when I receive extend frame message,The received extend frame ID and DLC are normal,but all of the received data are 0; when I send the standard frame message, all of send data are normal.

I don't know why the received data is 0,I can't find the answer and all of demo are standard frame.

"the hardware platform is Zynq7000,OS is Ubuntu 12.04, CAN use the real hardware,baudrate is set to 500kbps"

    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);                  //create socket
    printf("can_s is %d\n",s);
    strcpy(ifr.ifr_name, "can0" );
    ioctl(s, SIOCGIFINDEX, &ifr);                           //select dev
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;
    printf("ifr_ifindex is %d\n",addr.can_ifindex );
    bind(s, (struct sockaddr *)&addr, sizeof(addr));        //bind
    frame.can_id |= CAN_EFF_FLAG | CAN_RTR_FLAG | CAN_EFF_MASK;
    
    while(1)
    {
        SendCAN();//send the command

        nbytes = read(s, &frame, sizeof(frame));   //recv protocol
        printf("can_r_nbytes is %d\n",nbytes);
        printf("frame.can_id is 0x%X\n",frame.can_id & CAN_EFF_MASK);
        if (nbytes < 0) {
                perror("can raw socket read");
                return -2;
        }

        /* paranoid check ... */
        if (nbytes < sizeof(struct can_frame)) {
                fprintf(stderr, "read: incomplete CAN frame\n");
                return -3;
        }

        //for test
        RecvCAN();//recv the data and save the data to a buffer
    } 

Am I missing something in my code or doing something wrong?Has anyone else encountered this issue and found a solution?

received message info:

can_r_nbytes is 16
frame.can_id is 0xDFFFFFFF
ID=0x80000800 DLC=8 data[0][0][2]=0x0;

supplymentary content:

which the simulate CAN upper-computer send message is:
Frame format: extend
Frame type: data frame 
ID(hex):800
data(hex): 11 22 33 44 55 66 77 88


int RecvBuffer[16][4][10]={0,0,0};//16:means 16 nodes;4:different PFs;10:recv data,include can_id、can_dlc
struct can_frame frame;
int count=0;
int nbytes;
int s;
void RecvCAN(void)
{
        RecvBuffer[0][0][0]=frame.can_id ;
        RecvBuffer[0][0][1]=frame.can_dlc;
        RecvBuffer[0][0][2]=frame.data[0];
        RecvBuffer[0][0][3]=frame.data[1];
        RecvBuffer[0][0][4]=frame.data[2];
        RecvBuffer[0][0][5]=frame.data[0];
        RecvBuffer[0][0][6]=frame.data[4];
        RecvBuffer[0][0][7]=frame.data[5];
        RecvBuffer[0][0][8]=frame.data[6];
        RecvBuffer[0][0][9]=frame.data[7];
        //show message
        if (nbytes > 0) {
             printf("ID=0x%X DLC=%d data[0][0][2]=0x%X\n", RecvBuffer[0][0][0],
                     RecvBuffer[0][0][1],RecvBuffer[0][0][2]);
        }
}

void SendCAN(void)
{
    
    frame.can_id = CAN_EFF_FLAG | 0x1C2;
    frame.can_dlc = 8;
    frame.data[0] = 0xA8;
    frame.data[1] = 0xA7;
    frame.data[2] = 0xA6;
    frame.data[3] = 0xA5;
    frame.data[4] = 0xA4;
    frame.data[5] = 0xA3;
    frame.data[6] = 0xA2;
    frame.data[7] = 0xA1;

    nbytes = write(s, &frame, sizeof(frame));  //send message
    if (nbytes != sizeof(frame)) {
        printf("Send Error frame\n!");
    }
    printf("Send Back id[0]=0x%X\n",frame.can_id);
}

  • Please do not add links to pictures of plain text. Instead copy&paste your text output as formatted text directly into the question. That will make it much more readable and avoid the need for clicking external links. You can use the `edit` button below your question to update accordingly. – Gerhardh Sep 23 '21 at 09:49
  • Where does `RecvCAN` come from? Is it your code? Where is the source? Is it from a library? Which library? Is that what produces the output you showed? What does `data[0][0][2]` mean and why aren't you printing all 8 bytes (that should be there according to DLC)? – Useless Sep 23 '21 at 11:00
  • oh,I am sorry to my unclear description, RecvCAN and SendCAN has to added. all of the 8 bytes recevied are 0, The earliest,I thought more printing message will influence the runnig rate,so delete others data print. Thank you for your reply! Thank you for focusing my problem,Thank you again. – Jonne gong Sep 23 '21 at 15:13

0 Answers0