PaulPonomarev PaulPonomarev - 1 year ago 120
C Question

SocketCAN continuous reading and writing

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?

Answer Source

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

while(1) {
    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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download