Shadasviar
Shadasviar

Reputation: 537

CAN socket read with frames late

I am using CAN linux socket created as below:

    ...
    sockaddr_can addr;
    struct ifreq ifr;

    _sock_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);

    if (_sock_fd < 0) {
        throw(std::bad_exception());
    }

    strcpy(ifr.ifr_name, "can0");
    if (0 != ioctl(_sock_fd, SIOCGIFINDEX, &ifr)) {
        throw(std::bad_exception());
    }
    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;

    fcntl(_sock_fd, F_SETFL, O_NONBLOCK);

    if (0 != bind(_sock_fd, (struct sockaddr*)&addr, sizeof(addr))) {
        throw(std::bad_exception());
    }
    ...

And next I use usual read function for read frames from CAN network:

int CANSocket::CANRead(canid_t &id, vector<uint8_t> &data) {

    size_t size = 0;

    while (size < sizeof(struct can_frame)) {
        size += read(_sock_fd, &_msg, sizeof(struct can_frame));
    }

    id = _msg.can_id;

    data.clear();
    for (int i = 0; i < _msg.can_dlc; ++i) {
        data.push_back(_msg.data[i]);
    }

    return data.size();
}

My problem is that when I call my CANRead function, it returns frames which is about 100 frames before actual frame I get by candump utility.

I use 5ms sleep between reading frames, and server sends frames near 25 frames per second.

For example: when I list read frames by candump utility, i get e.g. frames

101
102
103
104
...
200

but my program running same time returns frames like

1
1
1
2
2
2
...
99

What do I wrong in frames reading and socket configuration so it reads late frames with duplicates?

Upvotes: 2

Views: 1443

Answers (1)

Shadasviar
Shadasviar

Reputation: 537

The problem was caused by socket read buffering. Setting up the buffer size to small resolved problem:

    ...
    int bufsize = 128;
    if (0 != setsockopt(_sock_fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize))) {
        throw(std::bad_exception());
    }
    ...

Upvotes: 1

Related Questions