MX Foundation 4
ar717_rx_acquisition_trigger.cs
/*******************************************************************************
//
// File:
// ar717_rx_acquisition_trigger.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo illustrates how to use the trigger functionality
// to start acquisition on a receive channel.
//
//
// Hardware Requirements:
// - MAXT FlexMulti
// - Loopback between first TX and RX ARINC 717 channels if internal loopback is not used
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar717_example
{
class ar717_rx_acquisition_trigger
{
const int MAX_TX_SUBFRAMES_TO_TRANSMIT = 8; //minimum of 4 to have at least one full frame.
static int BUFFER_SIZE = (int)(MAX_TX_SUBFRAMES_TO_TRANSMIT * Marshal.SizeOf(typeof(MXF_A717_DATAREC)));
public static void Main(string[] args)
{
UInt64 condList = 0;
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
IntPtr recPtr717 = IntPtr.Zero;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
IntPtr rxHostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 data;
UInt64 word;
UInt64 subframeSize = 128;
UInt16 value = 0;
int nbFrames = MAX_TX_SUBFRAMES_TO_TRANSMIT;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
{
data = new UInt16[8192]
};
// Connects to services and initialize environment
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt64(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(false), out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine();
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
// Initializes init callback handler to set first TX and RX channel to A717
// Initializes MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Gets handle of first multi_eh module
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MULTI_EH, 1, out moduleCount, module);
// If module not found, returns an error
if ((rc == MAXT_SUCCESS) && (moduleCount == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Gets handle of first A717 RX channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A717, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rxChannel);
// Gets handle of first A717 TX channel
if ((rc == MAXT_SUCCESS) && (channelCount != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A717, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
// If channel not found, returns an error
if ((rc == MAXT_SUCCESS) && (channelCount == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Enables loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A717_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
if (nbFrames < 4)
{
if (rc == MAXT_SUCCESS)
Console.Write("Number of subframes to transmit less than 4: synchronization will not occur.\n");
}
// Sets subframe size
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A717_SUBFRAME_SIZE, subframeSize);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A717_SUBFRAME_SIZE, subframeSize);
// Sets timebase to computer usec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Allocates TX Aperiodic Update buffer
if (rc == MAXT_SUCCESS)
{
rc = mxfTxAperiodicBufferAlloc(txChannel[0], MXF_TXAPERIODIC_PRIORITY_HIGH, (UInt64)BUFFER_SIZE, out txBuffer, IntPtr.Zero);
// Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocates RX acquisition buffer
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqBufferAlloc(rxChannel[0], (UInt64)BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxHostBuffer = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocates host buffer
if (rc == MAXT_SUCCESS)
{
try
{
recPtr717 = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Configures trigger
// Creates the condition list
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigConditionListAlloc(server, out condList);
// The condition will be triggered when data is 0x315 at word 21 of any subframe
if (rc == MAXT_SUCCESS)
{
condParam.mask = 0x0FFF0000;
condParam.data = 0x03150000;
condParam.offset = 10;
condParam.options = MXF_RXACQ_TRIG_COND_RDATA_OPTIONS_EQUAL;
rc = mxfRxAcqTrigConditionAdd(condList, MXF_RXACQ_TRIG_COND_ID_RDATA_DW, ref condParam);
}
// Sets the trigger with a pretrig count of 3 which allows some records to be read before the condition was triggered.
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigSet(rxBuffer, condList, 3);
// Starts the acquisition process
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStart(rxBuffer, 0, 0, MXF_RXACQ_FLAG_DEFAULT);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition started\n\r");
}
// Starts 717 data transmission
if (rc == MAXT_SUCCESS)
{
// Prepares string array
recPtr717 = txHostBuffer;
for (data = 0; data < MAX_TX_SUBFRAMES_TO_TRANSMIT; data++)
{
rec717.timeTag = 0;
rec717.control = 0;
rec717.dataSize = 2 * (UInt32)subframeSize; // 16 bits per word in subframe
rec717.repeatCount = 1;
rec717.reserved = 0;
for (word = 0; word < subframeSize; word++, value++)
{
if (word == 0)
{
//1st word of each subframe has to be a sync word
// sync words are:
// - 0x247 for subframe #0
// - 0x5B8 for subframe #1
// - 0xA47 for subframe #2
// - 0xDB8 for subframe #3
switch (data % 4)
{
case 0:
rec717.data[word] = 0x247;
break;
case 1:
rec717.data[word] = 0x5B8;
break;
case 2:
rec717.data[word] = 0xA47;
break;
case 3:
rec717.data[word] = 0xDB8;
break;
default:
break;
}
}
else
rec717.data[word] = value;
}
Marshal.StructureToPtr(rec717, recPtr717, false);
rc = mxfA717NextDataRecordPtrGet(recPtr717, out recPtr717);
}
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Transmitting ...\n");
// Transmits strings
rc = mxfA717TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_SUBFRAMES_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
{
mxfSleep((MAX_TX_SUBFRAMES_TO_TRANSMIT + 1) * 1000); // 1 second per subframe + 1 second to be sure to exceed the minimum duration to wait.
// Reads 717 data in acquisition buffer
rc = RX717ReadAcquisitionData(rxBuffer, subframeSize, rxHostBuffer);
}
// Stops and flushes unread data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqClear(rxBuffer);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition stopped\n\r");
}
// Frees trigger condition list
if ((rc == MAXT_SUCCESS) && (condList != 0))
// Catches any previous error
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
if (mxfSystemErrorStringGet(server, rc, 256, buffer) != MAXT_SUCCESS)
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
Console.WriteLine(buffer);
}
Console.Write("\nTerminating\n");
// Frees all buffers and terminate
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer != 0)
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
if (rxHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxHostBuffer);
Console.Write("\nPress enter to terminate\n");
Console.Read();
return;
}
/***************************************************************************************************************/
// RX717ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 RX717ReadAcquisitionData(UInt64 rxBuffer, UInt64 subframesize, IntPtr rxHostBuffer)
{
IntPtr recPtr = IntPtr.Zero;
UInt64 status, msgsCount = 0, bytesCount;
UInt64 data;
UInt64 trigTime = 0;
UInt32 rc;
UInt64 usec;
UInt64 word;
{
data = new UInt16[8192]
};
// Checks if trigger happened
rc = mxfRxAcqBufferStatusGet(rxBuffer, out status, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
{
if ((status & MXF_RXACQ_STATUS_TRIG_OCCURRED) == MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Displays the acquisition trigger time
rc = mxfRxAcqTrigTimeGet(rxBuffer, out trigTime);
if (rc == MAXT_SUCCESS)
{
var offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
var time = new DateTime(1970, 1, 1).AddTicks(offset.Ticks); //create a time object set to jan 01 1970 in current timezone
time = time.AddMilliseconds(trigTime / 1000);
usec = trigTime % 1000;
Console.WriteLine("Event triggered at {0}:{1:000}", time.ToString("yyyy-MM-dd HH:mm:ss:fff"), usec);
}
}
else
Console.Write("Trigger not fired\n");
}
// Reads and displays records
if (rc == MAXT_SUCCESS)
rc = mxfA717RxAcqRead(rxBuffer, 0, (UInt64)BUFFER_SIZE, out status, out msgsCount, out bytesCount, rxHostBuffer);
// Displays received strings
recPtr = rxHostBuffer;
for (data = 0; (data < msgsCount) && (rc == MAXT_SUCCESS); data++)
{
rec = (MXF_A717_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A717_DATAREC));
Console.Write("\n{0:D2}: Timetag={1:D12}, Size={2} words\n", data, rec.timeTag, (rec.dataSize) / 2);
for (word = 0; word < subframesize; word++)
Console.Write("{0:X3}", rec.data[word]);
Console.Write("\n");
rc = mxfA717NextDataRecordPtrGet(recPtr, out recPtr);
}
return rc;
}
private static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
UInt64 device = 0;
MXF_DEVICE_INFO deviceInfo = new MXF_DEVICE_INFO();
UInt32 rc;
server = server;
deviceIndex = deviceIndex;
if (attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceInfoGet(device, out deviceInfo);
if ((rc == MAXT_SUCCESS) && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI_EH))
{
// Sets MXF_MODULE_MULTI_EH first TX and RX channels to A717
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_A717;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
}
}
Updated 03/30/2022