MX Foundation 4
Sending Periodic Messages

The basic logic to send periodic messages is as follows:

  • First, allocate the necessary buffers that will hold the CAN data using mxfTxPeriodicUpdateMsgBufferAlloc(). Usually, one buffer is allocated for each message id to be sent. The buffer index can be any number between 0 and 255.
  • Second, create a schedule instance for a given channel using mxfTxPeriodicScheduleNew().
  • The simplest CAN message consists of a single list containing a single buffer. When working with such a simple message the function mxfTxPeriodicScheduleSingleMsgAdd() can be used. This function creates a message that contains a single list with a single buffer.
  • Before adding the CAN records to the message, one buffer list must be added using mxfTxPeriodicScheduleBufferListAdd(). The function includes an argument to specify how many buffers (CAN data) the list contains. If the list includes more than one buffer, one CAN data for each buffer included in the list is sent back-to-back at the message update rate.
  • Set the CAN data value(s) in the MXF_CANBUS_DATAREC for a given record using mxfCanBusTxPeriodicUpdateMsgWrite().
  • Start the schedule using mxfTxPeriodicScheduleRun().

canbus_periodic.c
The example below illustrates how to put it together:

#define BUFFER_SIZE 4096 // 4KB
HMXF_CHANNEL channel;
HMXF_BUFFER txBuffer;
HMXF_SCHED schedule;
HMXF_SCHED_MSG msg=0;
uint32 rc;
...
// Allocates TX Periodic Update buffer
if(!rc)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(channel, 0, BUFFER_SIZE, &txBuffer, NULL);
// Allocates host buffer
if (!rc)
{
rec = (MXF_CANBUS_DATAREC*)malloc(BUFFER_SIZE);
if (!rec)
rc = MAXT_ERROR_MEM;
}
// Creates the periodic scheduler
rc = mxfTxPeriodicScheduleNew(channel, &schedule);
// Sets scheduling values: Rate=500 ms (.1sec), Phase=0 us
if(!rc)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 500000LL, 0, &msg);
// Defines the number of buffer for the list and link to it
if(!rc)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 1, 0, &txBuffer);
// Set record for the buffer: ID Extended 0x080A0000, DLC=8
if (!rc)
{
rec->timeTag = 0;
rec->control = 0;
rec->repeatCount = 1;
rec->reserved = 0;
rec->dataSize = 8;
rec->id = 0x080A0000;
rec->info = MXF_CANBUS_REC_INFO_EXTENDED | 8;
for (i = 0; i<rec->dataSize; i++)
rec->data[i] = (uint8)i;
rc = mxfCanBusTxPeriodicUpdateMsgWrite(txBuffer, 1, rec);
}
// Runs the scheduler, updates records
if(!rc)
{
printf("Running periodic transmission, please wait...\n\r");
// Runs the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Waits 10 seconds
if(!rc)
{
mxfSleep(10000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if(rec)
free(rec)
...
Updated 10/23/2023