MX Foundation 4
Basic Sampling

The way to set a basic sampling receive application is as follows:

The MXF_CANBUS_SAMPREC structure must be used for reading an CAN message with sampling service.

Example

canbus_sampling.c
The example below shows how to implement an CAN sampling receive application.

#define BUFFER_SIZE 4096 // 4KB
HMXF_SERVER server;
HMXF_CHANNEL rxChannel;
HMXF_BUFFER rxBuffer;
MXF_CANBUS_SAMPREC *hostBuffer=NULL;
uint64 ID=0x08120000;
uint64 msgsCount, bytesCount;
uint64 j;
uint32 rc;
...
// Sets timebase to 64-bit microseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Allocates host buffer
if(!rc)
{
hostBuffer = (MXF_CANBUS_SAMPREC *)malloc(BUFFER_SIZE);
if(!hostBuffer)
rc = MAXT_ERROR_MEM;
}
// Allocates RX sampling buffer
if(!rc)
rc = mxfRxSamplingBufferAlloc(rxChannel, BUFFER_SIZE, &rxBuffer, NULL);
// Filters out a specific ID
if (!rc)
rc = mxfA429RxSamplingMsgSelectSet(rxBuffer, MXF_MSG_DESELECT, 1, &ID);
// Sets sampling kill time to 1 second.
if (!rc)
rc = mxfRxSamplingKilltimeSet(rxBuffer, 1000000);
// Starts sampling
if(!rc)
rc = mxfRxSamplingStart(rxBuffer);
...
// Reads and displays records
if(!rc)
{
recSamp = hostBuffer;
rc = mxfCanBusRxSamplingRead(rxBuffer, 0, 0, BUFFER_SIZE, &msgsCount, &bytesCount, hostBuffer);
for (j = 0; j < msgsCount && !rc; j++)
{
printf(" %03llu: Timetag=%012llu, Size=%u", j, recSamp->timeTag, recSamp->dataSize);
if (!recSamp->control)
printf(" OK");
else
printf(" Error");
printf("\n Data=");
for (byte = 0; byte<recSamp->dataSize; byte++)
{
printf("%02X ", recSamp->data[byte]);
}
printf("\n");
rc = mxfCanBusNextSamplingRecordPtrGet(recSamp, &recSamp);
}
}
...
Updated 10/23/2023