MX Foundation 4
ar629_updating_data_sampling.c
/*****************************************************************************
//
// File:
// ar629_updating_data_sampling.c
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the basic steps to update the data during transmission.
// In this example, we use the sampling mode for acquisition.
// It is also shown how to set a record to be a "variable length" record (use of MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_VARIABLE_LENGTH constant).
//
// It demonstrates how to:
//
// - Get handle to first BM and RT with mxfModuleChannelAllGet().
// - Set the CID with mxfAttributeUint64Set().
// - Configure major frame with mxfA629TxPeriodicMajorFrameSet().
// - Write messages of a record array into a buffer with mxfA629TxPeriodicUpdateMsgWrite().
// - Start major frame with mxfA629TxPeriodicMajorFrameStart().
// - Stop major frame mxfTxPeriodicMajorFrameStop().
//
// Hardware requirements:
// - MAXT FlexMulti-629.
//
*****************************************************************************/
#include "example.h"
//#define LOCAL
//#define TTL
#define MAX_RX_RECORDS_TO_RECEIVE 1000
#define RX_BUFFER_SIZE MAX_RX_RECORDS_TO_RECEIVE*sizeof(MXF_A629_SAMPREC)
#define MAX_TX_RECORDS_TO_TRANSMIT 1
#define TX_BUFFER_SIZE MAX_TX_RECORDS_TO_TRANSMIT*sizeof(MXF_A629_DATAREC)
void DisplayDataSampArray(uint64 recNum, MXF_A629_SAMPREC* rec);
//***************************************************************************
//
// Main()
//
//***************************************************************************
int main(void)
{
MXF_A629_DATAREC aRec629[10];
HMXF_CHANNEL hChannel=0, ahBM=0;
HMXF_SERVER server = 0;
HMXF_DEVICE device = 0;
HMXF_MODULE module = 0;
MXF_A629_SAMPREC* hostBuffer = 0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer[3]={0,0,0};
uint32 rc;
uint64 TI, bytesCount, msgsCount, indexBuffer;
uint32 length;
uint64 count=0, index, iMsg;
// Connects to services and initialize environment
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
// Initializes MX Foundation library
if (!rc)
{
printf("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets the first device handle
if (!rc)
rc = mxfSystemDeviceGet(server, 0, &device);
if (!rc)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A629MRT_EH, 1, &count, &module);
// Detects first BM
if (!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A629, MXF_SCLASS_BM_CHANNEL, 1, &count, &ahBM);
// Detects first RT channel
if (!rc)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A629, MXF_SCLASS_RT_CHANNEL, 1, &count, &hChannel);
// If module or channel not found, returns an error
if(!rc && !count)
rc = MAXT_ERROR_NOT_FOUND;
// Sets timebase to 64-bit nanoseconds
if (!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Sets interface to SIM or TTL
#ifdef TTL
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_A629_MODULE_INTERFACE, VMXF_A629_MODULE_INTERFACE_TTL);
#else
if (!rc)
rc = mxfAttributeUint64Set(module, KMXF_A629_MODULE_INTERFACE, VMXF_A629_MODULE_INTERFACE_SIM);
#endif
// Allocates RX sampling buffer
if(!rc)
rc = mxfRxSamplingBufferAlloc(ahBM, RX_BUFFER_SIZE, &rxBuffer, NULL);
// Sets sampling kill time to 2 seconds.
if (!rc)
rc = mxfRxSamplingKilltimeSet(rxBuffer, 2000000000);
// Allocates six TX Periodic Update Message buffer
for(index=0; index<3 && !rc; index++)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(hChannel, index, TX_BUFFER_SIZE, &txBuffer[index], NULL);
// Allocates host buffer
if (!rc)
{
hostBuffer = (MXF_A629_SAMPREC*)malloc(max(RX_BUFFER_SIZE, TX_BUFFER_SIZE));
if (!hostBuffer)
rc = MAXT_ERROR_MEM;
}
//Sets the CID to 0xF
if(!rc)
rc = mxfAttributeUint64Set(hChannel, KMXF_A629_RT_CID, 0xF);
// Starts sampling
if(!rc)
rc = mxfRxSamplingStart(rxBuffer);
if(!rc)
printf("Sampling started\n\r");
// Stops and clears major frame on RT #0
if(!rc)
rc = mxfTxPeriodicMajorFrameStop(hChannel, 0, 0);
if(!rc)
rc = mxfTxPeriodicMajorFrameClear(hChannel, 0);
if(!rc)
{
memset(aMinorFrame, 0, sizeof(aMinorFrame));
//--- Sets the minor frame #0 with 3 WordStrings --//
//WordString #0 : Label 0x1, CID 0xF
iMsg = 0;
aMinorFrame[iMsg].buffer = txBuffer[0];
aMinorFrame[iMsg].label = 0x1;
aMinorFrame[iMsg].cid = 0xF;
aMinorFrame[iMsg].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_VARIABLE_LENGTH;
aMinorFrame[iMsg].length = 0;
iMsg = 1;
aMinorFrame[iMsg].buffer = txBuffer[1];
aMinorFrame[iMsg].label = 0x2;
aMinorFrame[iMsg].cid = 0xF;
aMinorFrame[iMsg].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_DEFAULT;
aMinorFrame[iMsg].length = 1;
iMsg = 2;
aMinorFrame[iMsg].buffer = txBuffer[2];
aMinorFrame[iMsg].label = 0x3;
aMinorFrame[iMsg].cid = 0xF;
aMinorFrame[iMsg].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_DEFAULT;
aMinorFrame[iMsg].length = 1;
if (!rc)
rc = mxfA629TxPeriodicMajorFrameSet(hChannel, 0, 0, 3, &aMinorFrame[0]);
}
//--- Sets default data for label 0x1, CID 0xF ---//
if(!rc)
{
memset(&aRec629, 0, sizeof(aRec629));
//Sets the lentgh of this label/CID
length = 4; //4 bytes (label + 1 data word)
aRec629[0].control = 0;
aRec629[0].repeatCount = 1;
aRec629[0].dataSize = length;
aRec629[0].data[0] = 0;
aRec629[0].data[1] = 0x1101;
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[0], 1, &aRec629[0]);
}
if(!rc)
{
//Sets the lentgh of this label/CID
length = 4; //4 bytes (label + 1 data word)
aRec629[0].control = 0;
aRec629[0].repeatCount = 1;
aRec629[0].dataSize = length;
aRec629[0].data[0] = 0;
aRec629[0].data[1] = 0x2201;
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[1], 1, &aRec629[0]);
}
if(!rc)
{
//Sets the lentgh of this label/CID
length = 4; //4 bytes (label + 1 data word)
aRec629[0].control = 0;
aRec629[0].repeatCount = 1;
aRec629[0].dataSize = length;
aRec629[0].data[0] = 0;
aRec629[0].data[1] = 0x3301;
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[2], 1, &aRec629[0]);
}
//Starts the major frame in the Block mode
TI = 25000000;
if (!rc)
rc = mxfAttributeUint64Set(module,KMXF_A629_MODULE_TI,TI);
if(!rc)
{
majorProperties.mode = MXF_A629_TXPERIODIC_MJRFRAME_PROPERTIES_MODE_BLOCK;
majorProperties.reserved = 0;
rc = mxfA629TxPeriodicMajorFrameStart(hChannel, 0, &majorProperties);
}
//Waiting 5 seconds. Allows major frame to run a little bit.
mxfSleep(5000);
//Reads first datas
if (!rc)
{
rc = mxfA629RxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, RX_BUFFER_SIZE, &msgsCount, &bytesCount, hostBuffer);
if (!rc)
printf("Read %llu messages\n\r", msgsCount);
DisplayDataSampArray(msgsCount,hostBuffer);
}
//--- Change data for label 0x1, CID 0xF ---//
if (!rc)
printf("Changing data for last 5 seconds\n\r");
if(!rc)
{
//Sets the lentgh of this label/CID
length = 8; //8 bytes (label + 3 data word)
aRec629[0].control = 0;
aRec629[0].repeatCount = 1;
aRec629[0].dataSize = length;
aRec629[0].data[0] = 0;
aRec629[0].data[1] = 0xFF03;
aRec629[0].data[2] = 0xFFFF;
aRec629[0].data[3] = 0xFFFF;
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[0], 1, &aRec629[0]);
}
//Waiting 5 seconds. Allows major frame to run a little bit.
mxfSleep(5000);
if (!rc)
{
rc = mxfA629RxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, RX_BUFFER_SIZE, &msgsCount, &bytesCount, hostBuffer);
if (!rc)
printf("Read %llu messages\n\r", msgsCount);
DisplayDataSampArray(msgsCount,hostBuffer);
}
//Stops the major frame
if(!rc)
rc = mxfTxPeriodicMajorFrameStop(hChannel, 0, 0);
// Stops sampling and reads last datas
if(!rc)
rc = mxfRxSamplingStop(rxBuffer);
if (!rc)
{
printf("Sampling stopped\n\r");
}
if(rc)
{
char errorString[200];
if(mxfSystemErrorStringGet(server, rc, sizeof(errorString), errorString))
sprintf (errorString,"ERROR # 0x%X", rc);
printf("%s\n\r", errorString);
}
printf("Terminating ...\n");
// Free all buffers and terminate
if (rxBuffer)
for (indexBuffer = 0; indexBuffer < 3; indexBuffer++)
{
if (txBuffer[indexBuffer])
mxfTxPeriodicUpdateMsgBufferFree(txBuffer[indexBuffer]);
}
if (hostBuffer)
free(hostBuffer);
// Unloads MX Foundation library
// Disconnects from MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
void DisplayDataSampArray(uint64 recNum, MXF_A629_SAMPREC* rec)
{
uint64 iRec,
iData;
printf("\n");
for(iRec=0; iRec < recNum; iRec++)
{
printf("%03llu %010llu %02u %u ", iRec, p->timeTag, p->dataSize, p->rate);
if(p->control & MXF_A629_RX_REC_CTRL_STRING_CRC_ERROR)
printf(" CRC error");
if(p->control & MXF_A629_RX_REC_CTRL_STRING_DATA_SYNC_ERROR)
printf(" Data sync error");
if(p->control & (MXF_A629_RX_REC_CTRL_STRING_LABEL_MANCHESTER_ERROR|MXF_A629_RX_REC_CTRL_STRING_DATA_MANCHESTER_ERROR))
printf(" Manchester error");
if(p->control & (MXF_A629_RX_REC_CTRL_STRING_LABEL_PARITY_ERROR|MXF_A629_RX_REC_CTRL_STRING_DATA_PARITY_ERROR))
printf(" Parity error");
if(p->control & MXF_A629_RX_REC_CTRL_STRING_EOS_ERROR)
printf(" End of String error");
for(iData=0; iData < p->dataSize/2; iData++)
{
printf("%04x ", p->data[iData]);
if(!((iData+1)%8) && (iData+1 < p->dataSize/2))
printf("\n ");
}
printf("\n");
}
}
Updated 10/23/2023