Page 1 of 1

How to do a pcan2.0 send and receive with a timeout?

Posted: Tue 29. Aug 2017, 18:16
by cheeyw
I am using the linux headers of peak-linux-driver-8.4.0.

When I use OFD_NONBLOCKING flag in pcanfd_open, the receive function will be a pure non-blocking function.
How do I send & receive pcan messages with a fixed timeout?

Code: Select all

int fd = pcanfd_open("/dev/pcan32", OFD_BITRATE | OFD_BTR0BTR1 | OFD_NONBLOCKING, PCAN_BAUD_250K);
Are there any functions that are similar to:

Code: Select all

DWORD LINUX_CAN_Read_Timeout(HANDLE hHandle, TPCANRdMsg* pMsgBuff, int
nMicroSeconds);

DWORD LINUX_CAN_Write_Timeout(HANDLE hHandle, TPCANMsg* pMsgBuff, int
nMicroSeconds);

Re: How to do a pcan2.0 send and receive with a timeout?

Posted: Wed 30. Aug 2017, 10:32
by S.Grosjean
Hello,

Simply setup the select() call by yourself: open lib/src/libpcan.c and have a look to both LINUX_CAN_xxx_Timeout() functions. Copy both functions in your own source, and replace "desc->fd" with your "fd".

Timed wait during "nMicroSeconds" for read one "msg" from "fd" should look like something like:

Code: Select all

        struct timeval t;
        fd_set fdRead;
        int err;

        /* calculate timeout values */
        t.tv_sec  = nMicroSeconds / 1000000L;
        t.tv_usec = nMicroSeconds % 1000000L;

        FD_ZERO(&fdRead);
        FD_SET(fd, &fdRead);

        /* wait until timeout or a message is ready to read */
        err = select(fd + 1, &fdRead, NULL, NULL, &t);

        /* the only one file descriptor is ready for read */
        if (err > 0)
                return pcanfd_recv_msg(fd, &msg);

        /* nothing is ready, timeout occured */
        if (!err)
                return CAN_ERR_QRCVEMPTY;
Regards,

Stéphane