0

I have developed a C program that receive can traffics from vcan0 interface.

I would like to add timeout on reception, I mean when a timeout expired (10 seconds for example) with no data received during this time, I print "no data received during 10 seconds" and I make a reboot of my pc(the reboot is made if a specific condition is satisfied).

I have tested with select function I get the timeout but when the specific condition is not satisfied I can't receive can traffics anymore.

should I add something for reactivate reception when I have timeout and specific condition is not satisfied? if yes how?

My program is like this:

...
while(1)
{
    FD_ZERO(&set); /* clear the set */
    FD_SET(filedesc, &set); /* add our file descriptor to the set */

    timeout.tv_sec = 0;
    timeout.tv_usec = 10000000; // 10 second

    rv = select(fd_max + 1, &set, NULL, NULL, &timeout);
    if(rv == -1)
        perror("select"); /* an error accured */
    else if(rv == 0)
    {
        printf("no data received within 10 second"); /* a timeout occured */
        if (specific condition is true) 
        {
            reboot(RB_AUTOBOOT);
        }
    }
    else
        int s;
        /* loop on all sockets */
        for(i=s;i<=fd_max;i++)  
        {
            read( i, buff, len );
            /* some other instruction*/
        }
}
...
developer
  • 4,744
  • 7
  • 40
  • 55
  • 2
    Is your receiver function blocking or non-blocking? Can it provide a status? Where is your code? – Weather Vane Oct 21 '15 at 08:47
  • Please narrow down the question. Do you need help with the CAN reception, or with the timeout, or how to programmatic reboot? – Lundin Oct 21 '15 at 09:01
  • Code your receiver with select, where you can specify a timeout. – LPs Oct 21 '15 at 09:02
  • I have updated my question – developer Oct 21 '15 at 09:23
  • I need to detect no reception within x seconds and reboot if my condition is true otherwise I can receive can traffics when I receive a new packet after expiration of timeout and my condition is false – developer Oct 21 '15 at 09:27
  • Your code looks ok for me. BTW what is strage is that you use fd_max but you have only a single file descriptor added to your set. If you have more than 1 file descriptor you must FD_SET all descriptors. – LPs Oct 23 '15 at 07:50

0 Answers0