Using Object Buffers

  • April 22, 2015
  • Magnus Carlsson

Some of our interfaces are equipped with hardware buffers for automatic sending or responding to messages. They can be used when the timing conditions are strict, and might not be possible to fulfill on the application level.

Normally, a CAN message received by the CAN driver in the Kvaser unit has to pass through to the computer, by for example a USB or PCI port. It has to pass the operating system and get picked up by the driver before the application can process it. Now, this isn’t as slow as it sounds, but sometimes it is not fast enough. That is when the Kvaser object buffers can be useful, to get around the latency caused by these steps.

There are two types of object buffers, auto response and auto transmit.

  • Auto response sends a defined message immediately upon receiving a message meeting your conditions.
  • Auto transmit sends a message periodically, with higher timing accuracy than can be achieved by an application working through driver and operating system.

Using the object buffers

Scenario 1: Auto Response

To use the object buffers, you start with allocating a buffer with a call to canObjBufAllocate().

For an auto response buffer, you define which messages to respond to by calling canObjBufSetFilter(), and the contents of the response is set with canObjBufWrite().

To enable a buffer, call canObjBufEnable(), and call canObjBufDisable() to disable it.

The filter in canObjBufSetFilter() is of mask and code type, which means you can set the buffer to respond to a range of CAN ids.

Example Code:

This code snippet sets up a buffer that responds to CAN ID 100, with a message with ID 200.

 /* Set up an auto response buffer */


byte[] msg = { 1, 2, 3, 4, 5, 6 }; //define message
int autoBufIndex = (int)(canObjBufAllocate(handle, canOBJBUF_TYPE_AUTO_RESPONSE)); //Allocate buffer
int status;
status = canObjBufWrite(handle, autoBufIndex, 200, msg, 0, 0); //Write to buffer
status=  canObjBufSetFilter(handle, autoBufIndex, 100, 0xFFFF); //Only match message with id 100, match on all bits.        
status = canlibCLSNET.Canlib.canObjBufEnable(handle, autoBufIndex); //activate the buffer
            

Scenario 2: Auto Transmit

Once again, to use the object buffers, you start with allocating a buffer with a call to canObjBufAllocate().

For an auto transmit buffer, call canObjBufSetPeriod() to set the frequency of the message, call canObjBufSetMsgCount() to set the number of messages to send from the buffer, and call canObjBufWrite() to define the contents of the message.

Example Code:

This code snippet set up a auto transmit buffer that sends a message with ID 300 with an interval of 1ms.

/* Set up an auto transmit buffer */

byte[] msg = { 1, 2, 3, 4, 5, 6 }; //define message
int autoBufIndex = (int)(canObjBufAllocate(handle, canOBJBUF_TYPE_AUTO_TRANSMIT)); //Allocate buffer
int status;
status = canObjBufWrite(handle, autoBufIndex, 300, msg, 0, 0); //Write to buffer
status = canObjBufSetPeriod(handle, autoBufIndex, 1000); //Set period to 1000 microseconds
status = canlibCLSNET.Canlib.canObjBufEnable(handle, autoBufIndex); //activate the buffer
            

Which CAN interfaces has Object Buffers?

Hardware object buffers are available on our Professional products, and in general 8 buffers are available.

Search Professional products >>

Comments?  Contact us directly at [email protected].

Author Image

Magnus Carlsson

Magnus Carlsson is a Software Developer for Kvaser AB and has developed firmware and software for Kvaser products since 2007. He has also written a number of articles for Kvaser’s Developer Blog dealing with the popular Python language.