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.