socketCAN connection: read() not fast enough
Hello,
I use the socket() connection for my CAN communication.
fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);
I'm using 2 threads: one periodic 1ms RT thread to send data and one thread to read the incoming messages. The read function looks like:
void readCan0Socket(void){
int receivedBytes = 0;
do
{
// set GPIO pin low
receivedBytes = read(fd ,
&receiveCanFrame[recvBufferWritePosition],
sizeof(struct can_frame));
// reset GPIO pin high
if (receivedBytes != 0)
{
if (receivedBytes == sizeof(struct can_frame))
{
recvBufferWritePosition++;
if (recvBufferWritePosition == CAN_MAX_RECEIVE_BUFFER_LENGTH)
{
recvBufferWritePosition = 0;
}
}
receivedBytes = 0;
}
} while (1);
}
The socket is configured in blocking mode, so the read function stays open until a message arrived. The current implementation is working, but when I measure the time between reading a message and the next waiting state of the read function (see set/reset GPIO comment) the time varies between 30 us (the mean value) and > 200 us. A value greather than 200us means (CAN has a baud rate of 1000 kBit/s) that packages are not recognized while the read() handles the previous message. The read() function must be ready within 134 us.
How can I accelerate my implementation? I tried to use two threads which are separated with Mutexes (lock before the read() function and unlock after a message reception), but this didn't solve my problem.