MX Foundation 4
async_tx_error_injection.c
/*****************************************************************************
//
// File:
// async_tx_error_injection.c
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates how to inject errors and trig a discrete line
// using the ASYNC record control field. Note that this examples uses
// RS-422 instead of the default RS-485.
//
// Hardware requirements:
// - MAXT Flex device with ASYNC_EH module
//
*****************************************************************************/
#include "example.h"
#define LOCAL
#define LOOPBACK
#define MAX_TX_RECORDS_TO_TRANSMIT 8
int main(void)
{
uint32 rc;
HMXF_SERVER server=0;
HMXF_DEVICE device=0;
HMXF_MODULE module=0;
HMXF_CHANNEL rxChannel=0;
HMXF_CHANNEL txChannel=0;
HMXF_BUFFER rxBuffer=0;
HMXF_BUFFER txBuffer=0;
uint64 moduleCount=0;
uint64 channelCount=0;
size_t txBufferSize=0;
size_t rxBufferSize=0;
MXF_ASYNCEH_DATAREC* rxHostBuffer=0;
MXF_ASYNCEH_DATAREC* txHostBuffer=0;
uint64 rxAcqStatus=0;
uint64 msgCount=0;
uint64 byteCount=0;
uint64 data;
uint64 byte;
// Connect to services local or remote
#ifdef LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", FALSE, &server);
#endif
// Initialize MX Foundation library
if(!rc)
{
printf("Starting ...\n");
rc=mxfSystemInit(server);
}
// Get the device handle
if (!rc)
rc=mxfSystemDeviceGet(server, 0, &device);
// Get handle of first ASYNC-EH module
if (!rc)
rc=mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, &moduleCount, &module);
// Get handle of first ASYNC-EH RX channel
if(!rc)
rc=mxfModuleChannelAllGet(module, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_RX_CHANNEL, 1, &channelCount, &rxChannel);
// Get handle of first ASYNC-EH TX channel
if(!rc)
rc=mxfModuleChannelAllGet(module, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_TX_CHANNEL, 1, &channelCount, &txChannel);
// Allocate 10 KB buffer for tx data
if(!rc)
{
txBufferSize = 10*1024;
// Allocate TX Aperiodic static buffer for HIGH priority queue
rc=mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, &txBuffer, NULL);
// Host buffer allocation
if(!rc)
{
txHostBuffer = calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
//Activate loopback before transmission and reception
#ifdef LOOPBACK
if (!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate 10 KB buffer for rx data
if(!rc)
{
rxBufferSize = 10*1024;
// Allocate RX acquisition static buffer
rc=mxfRxAcqBufferAlloc(rxChannel, rxBufferSize, &rxBuffer, NULL);
// Host buffer allocation
if(!rc)
{
rxHostBuffer = calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
//Set electrical interface to RS-422
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_ELECTRICAL_INTERFACE, VMXF_ASYNCEH_ELECTRICAL_INTERFACE_RS422);
// Set RX & TX channels speed to 1 mbps
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_SPEED, 1000000);
if(!rc)
rc=mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_SPEED, 1000000);
// Set RX string gap to 1 bit
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_STRING_GAP, 1);
//Enable TX#0 discrete trigger on transmission (discrete #1)
if(!rc)
rc=mxfChannelDiscreteOutputTriggerEnableSet(txChannel, MXF_ASYNCEH_DISCRETE_OUTPUT_TRIG_ON_TX, VMXF_ENABLE, 1);
// Start acquisition
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
mxfSleep(100);
if(!rc)
{
uint64 startTime = 100*1000*1000; //100 msec
uint64 delay = 1*1000*1000; //1 msec
// Prepare string array (8 bytes per string with delay of 1 msec between each)
rec = txHostBuffer;
for(data=0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec->timeTag = startTime+((data+1)*delay);
if(data == 2) // send this record with a long word size error and discrete output trigger
rec->control = MXF_ASYNCEH_TX_REC_CTRL_LONG_WORD_SIZE|MXF_ASYNCEH_TX_REC_CTRL_DISCRETE_OUTPUT_TRIG;
else
rec->control = 0;
rec->repeatCount = 1;
rec->dataSize = 8;
for(byte=0; byte < rec->dataSize; byte++)
rec->data[byte] = (uint8)(0x11*byte);
}
}
if(!rc)
{
printf("Transmitting ...\n");
// Transmit strings on relative record time
rc = mxfASYNCEHTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if(!rc)
mxfSleep(5000);
if(!rc)
{
printf("Receiving ...\n");
// Read rx buffer
rc = mxfASYNCEHRxAcqRead(rxBuffer, 0xFFFFFFFF, rxBufferSize, &rxAcqStatus, &msgCount, &byteCount, rxHostBuffer);
}
if(!rc)
printf("String received count = %"PRIu64" \n", msgCount);
if(!rc)
{
// Display received strings
rec = rxHostBuffer;
for(data=0; data<msgCount && !rc; data++)
{
printf(" %02"PRIu64": Timetag=%012"PRIu64", Size=%u, Control: 0x%08x ", data, rec->timeTag, rec->dataSize, rec->control);
for(byte=0; byte<rec->dataSize; byte++)
printf("%02X", rec->data[byte]);
printf("\n");
}
}
// Stop acquisition
if(!rc)
rc = mxfRxAcqStop(rxBuffer);
// Free device and host buffers
if(rxBuffer)
mxfRxAcqBufferFree(rxBuffer);
if(txBuffer)
if(txHostBuffer)
free(txHostBuffer);
if(rxHostBuffer)
free(rxHostBuffer);
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");
// Unload MX Foundation library
// Disconnect from MX Foundation library
printf("\nPress a key to terminate\n");
getchar();
return rc;
}
Updated 10/23/2023