3

I'm writing a program that continuously sends "Hello" to a CAN-bus and reads data from the bus via SocketCAN. I want those two steps to be independent. That means even if there is no data on the bus, the program will still send "Hello". But with usual CAN reading it is impossible, because this function stops program run and waits for data.

Is there a way how to make it non-blocking or waiting for data only for a few milliseconds?

yegorich
  • 4,653
  • 3
  • 31
  • 37
PaulPonomarev
  • 355
  • 1
  • 4
  • 20

4 Answers4

3

Another way I found - threads. Just make CAN reading work in thread and it won't stop main cycle. For Linux systems it looks like this:

 #include <pthread.h>

    void *thread(int cansock) {
       struct can_frame rxmsg;      
       while (1) {   
            read(cansock, &rxmsg, sizeof(rxmsg));
            printf("message received\n");                     
        }
    }

    int main(){
// initialize CAN socket and message to send
    pthread_t pth;
    pthread_create(&pth, NULL, thread, cansock);
    while(1){
      write(cansock, &txmsg, sizeof(txmsg));
      printf("message sent\n");
      }
    return 0;
    }
PaulPonomarev
  • 355
  • 1
  • 4
  • 20
  • 3
    By the way, you can use `setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))` where `tv` is a `timeval` to set the read timeout. Then `read` will fail with `EAGAIN` if the timeout expires. You can also use [`CAN_RAW_FILTER`](https://www.kernel.org/doc/Documentation/networking/can.txt) to reduce processing overhead and filter only the frames you are interested in. – Jason C Apr 17 '14 at 13:10
2

You can use following constellation (this is not the complete solution, but just algorithm):

while(1) {
    FD_ZERO(&rdfs);
    FD_SET(s, &rdfs);

    tv.tv_sec = 0;
    tv.tv_usec = 10000; // microseconds

    rc = select(s + 1, &rdfs, NULL, NULL, &tv);

    // rc == 0 - timeout
    if (!rc) {
         // write your CAN frame
    }

    if (FD_ISSET(s, &rdfs)) {
         // read CAN frames
    }
}

See man select for more information and how to handle return value.

yegorich
  • 4,653
  • 3
  • 31
  • 37
  • 3
    You don't have to use `select` for SocketCAN (although it's not a problem at all if you do); you can also use `setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))` where `tv` is a `timeval` for receive timeouts. Then `read` will fail with `EAGAIN` if the timeout expires. – Jason C Apr 17 '14 at 13:09
  • @Jason C: I like your suggestion, but it didn't work for me: I tried as above and also with "setsockopt(skt, SOL_CAN_RAW, SO_RCVTIMEO,..." since this seemed more likely, but in both cases the "read" call blocked forever until a frame arrived (no timeout). – Jeremy Nov 23 '17 at 00:17
  • Oops, my mistake... from the linux manual for "socket": "If the timeout is set to zero (the default) then the operation will never timeout." I was using a timeout of 0 in the hope of getting an immediate return if there was no frame to read. – Jeremy Nov 23 '17 at 18:47
2

Be careful with this solution:

void *thread(int cansock) {
       struct can_frame rxmsg;      
       while (1) {   
            read(cansock, &rxmsg, sizeof(rxmsg));
            printf("message received\n");                     
        }
    }

because if read() starts returning an error without block, and this is out in the field and there's nobody to look at the results of printf(), you're going into a busy loop.

user6269400
  • 129
  • 1
  • 2
  • This probably would be better as a comment on the other answer; rather than an answer in itself. I know you can't comment yet - see [this post](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead/214174#214174) for some details on what you can do in the meantime. – Tim Malone Jul 12 '16 at 23:40
1

I solved it by add the following code on initialization

timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
setsockopt(skt_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));

and add the loop read in another thread.

Gavin Gao
  • 309
  • 3
  • 6