Kvaser Linux CANLIB

Message Mailboxes

Some other CAN driver libraries in the market feature what usually is called "mailboxes", "message objects", "user buffers" etc. CANLIB, on the other hand, focuses on message queues because we feel this imposes fewer restrictions on the CAN system.

For example, a higher layer protocol that uses a sequence of CAN messages with the same identifier is hard to implement using mailboxes: when the next message arrives it overwrites the previous one. You can, in a way, simulate the behavior of mailboxes by using canReadSpecific(). You will have to call canRead() periodically in order to clear out messages not read by canReadSpecific().

Example. How to simulate a mailbox-style CAN interface.

Assume you take special interest in CAN messages with identifiers 530, 540 and 550. Your message-handling loop could then look something like this:

    while (..) {
      if (canReadSpecific(h, 530, buf, ...) == canOK) {
        // Process msg 530
      }
      
      if (canReadSpecific(h, 540, buf, ...) == canOK) {
        // Process msg 540
      }

      if (canReadSpecific(h, 550, buf, ...) == canOK) {
        // Process msg 550
      }

      while (canRead(h, &id, buf, ...) == canOK) {
        // Handle other messages
      }
    }

You can call this message-processing routine from your WM__CANLIB handler or make it the main routine in your program.