MX Foundation 4
async.c
/*****************************************************************************
//
// File:
// async.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 the basic usage of ASYNC enhanced channel class
// for transmission and reception. Note that this examples uses RS-422 instead
// of the default RS-485
//
// Hardware requirements:
// - MAXT Flex Device with ASYNC capabilities
//
*****************************************************************************/
#include "example.h"
#define MAX_TX_RECORDS_TO_TRANSMIT 8
#define LOCAL
#define LOOPBACK
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value);
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 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
if(!rc)
rc = mxfSystemInitAttributeUint64CallbackHandler(server, &initHandler);
// 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);
// Enable loopback
#ifdef LOOPBACK
if(!rc)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// 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 = (MXF_ASYNCEH_DATAREC*) calloc(1, txBufferSize);
if(!txHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// 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 = (MXF_ASYNCEH_DATAREC*) calloc(1, rxBufferSize);
if(!rxHostBuffer)
rc = MAXT_ERROR_MEM;
}
}
// Set timebase to RTC nsec
if(!rc)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// 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 the Electrical Interface to RS422
if (!rc)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_ELECTRICAL_INTERFACE, VMXF_ASYNCEH_ELECTRICAL_INTERFACE_RS422);
// Set RX string gap to 1 bit
if(!rc)
rc=mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_STRING_GAP, 4);
// Start acquisition
if(!rc)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if(!rc)
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);
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)
rc = mxfRxAcqBufferStatusGet(rxBuffer, &rxAcqStatus, &msgCount, &byteCount, NULL);
if(!rc)
{
printf("Receiving ...\n");
// Read rx buffer
rc = mxfASYNCEHRxAcqRead(rxBuffer, 0, 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\n Data=", data, rec->timeTag, rec->dataSize);
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;
}
uint32 initHandler(HMXF_SERVER server, uint64 deviceIndex, uint64 moduleIndex, uint64 channelIndex, uint64 attrib, uint64* value)
{
server=server;
deviceIndex=deviceIndex;
moduleIndex=moduleIndex;
channelIndex=channelIndex;
if(attrib == KMXF_CHANNEL_CLASS)
{
*value = MXF_CLASS_ASYNC_ENHANCED;
return TRUE;
}
return FALSE;
}
Updated 10/23/2023