0

I'm trying to implement a CAN interface on embedded linux platform (yocto based) using SocketCAN.

After reading the documentation I wrote a quick testing application code in order to receive/read a few packages from a CAN bus on non blocking mode:

int main()
{
    int s;
    struct sockaddr_can addr;
    struct ifreq ifr;
    struct can_frame rxframe;
    int nbytes=0;
    char byte0,byte1,byte2,byte3;

    //CAN socket setup
    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    strcpy(ifr.ifr_name, "can0" );
    ioctl(s, SIOCGIFINDEX, &ifr);

    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;

    bind(s, (struct sockaddr *)&addr, sizeof(addr));
    fcntl(s, F_SETFL, O_NONBLOCK);  

    //Read packages test
    while(1)
    {
        nbytes = read(s, &rxframe, sizeof(struct can_frame));
        //Package received. Save data.
        byte0                           = rxframe.data[0];
        byte1                           = rxframe.data[1];
        byte2                           = rxframe.data[2];
        byte3                           = rxframe.data[3];

        printf("byte0: %d \n",byte0);
    }

    return 0;
}

The CAN link works and it is receiving the data but the problem is that it takes too long to update the data (a few seconds), e.g if the byte0 changes from 0x00 to 0xff it will take ~5 seconds to see that change reflected in the application.

I checked in the console using the command: candump can0 and I can see that the data changes fine without this delay.

Does anyone have an idea why there's this delay in the application? what am I missing here?

Thanks in advance.

joe
  • 309
  • 4
  • 16
  • The code looks okay to me. Maybe you could run `strace` on your program and on `candump can0` and see if there are any differences? – user253751 Feb 24 '20 at 17:52
  • @user253751 thanks for your comment, yes, but the difference is obvious, in the C code application the value is update after a few seconds and with `candump can0` I can see the data change/update immediately. Do you know if could it be down to a file descriptor or FIFO buffer setup? – joe Feb 24 '20 at 18:02
  • I don't know why, that's why I suggested `strace` which shows you which system calls the program makes. (It can be a lot of data, so you would only run it for long enough to see the problem, and save it to a file to check the differences) – user253751 Feb 24 '20 at 18:20
  • ok, yes it would be good check but the problem is that I have not available `strace` in the embedded system that I'm working on, do you know if there's any other alternative? – joe Feb 24 '20 at 18:33
  • yocto has strace. But you could also get the source code for candump, and trace it in your mind. or delete all the bits you aren't using and then see what is different between candump and your code. – user253751 Feb 24 '20 at 18:37
  • @user253751 thanks for your suggestions, I've managed to trace it down and I realised that there was a CAN functions conflict with other application which was running in background, I killed that application and now it works fine. – joe Feb 25 '20 at 09:25

0 Answers0