MX Foundation 4
ar708_trigger.cs
/************************************************************************************************
//
// File:
// ar708_trigger.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2015, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example illustrates how to manage buffer thresholds with asynchronous events.
// The demo will transmit and record the messages using asynchronous
// events.
//
// Hardware Requirements:
// - MAXT FlexMulti-1553 or FlexMulti-629 with A708 option.
// - Loopback between TX and RX 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 ar708_example
{
class ar708_trigger
{
const UInt64 MAX_TX_RECORDS_TO_TRANSMIT = 8;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
UInt64 moduleCount = 0, channelCount = 0;
uint txBufferSize, rxBufferSize;
IntPtr rxHostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 rxAcqStatus = 0;
UInt64 msgCount = 0, byteCount = 0;
UInt64 data, word;
UInt64 rxBufferAllocatedSize, txBufferAllocatedSize;
UInt64 condList = 0;
var offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
var time = new DateTime(1970, 1, 1).AddTicks(offset.Ticks);
UInt64 usec;
UInt64 trigTime;
// Connect 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;
}
// Initialize the server
Console.WriteLine();
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
//Get Module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A708_EH, 1, out moduleCount, module);
// If module not found, return an error
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Activates the ARINC 708 module (because the MIL-STD-1553 is by default activated, except on FM1553-2)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A708_MODULE_ACTIVE, Convert.ToUInt64(true));
if (rc == MAXT_ERROR_NOT_SUPPORTED)
rc = MAXT_SUCCESS;
//Get handle of first ARINC 708 RX Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A708, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rxChannel);
//Get handle of first A708 Tx Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A708, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
// Enable loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A708_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
//Allocates 4KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 4 * 1024;
//Allocates Tx Aperiodic static buffer for HIGH priority queue
rc = mxfTxAperiodicBufferAlloc(txChannel[0], MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, out txBuffer, IntPtr.Zero);
//Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
rxBufferSize = 4 * 1024;
//Allocate 4kb for Rx data
if (rc == MAXT_SUCCESS)
{
//Allocates Rx acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel[0], rxBufferSize, out rxBuffer, out rxBufferAllocatedSize);
try
{
rxHostBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
//Sets timebase to 64bit usec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
//Configures Trigger and Creates the condition List
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigConditionListAlloc(server, out condList);
//Condition will be triggered on the 5th record
if (rc == MAXT_SUCCESS)
{
condParam.mask = 0x01FE0000;
condParam.data = 0x00000000;
condParam.offset = 0;
condParam.options = MXF_RXACQ_TRIG_COND_RDATA_OPTIONS_NOTEQUAL;
rc = mxfRxAcqTrigConditionAdd(condList, MXF_RXACQ_TRIG_COND_ID_RDATA_DW, ref condParam);
}
//Set 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);
// Set acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
// Start acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Acquisition started");
if (rc == MAXT_SUCCESS)
{
IntPtr recPtr = txHostBuffer;
rec.data = new UInt16[128];
//Prepares records to send for basic transmission/reception test
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = data * 5000;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 200; //200 bytes corresponds to 1600 bits (see ARINC 708 specification)
for (word = 0; word < rec.dataSize / 2; word++)
{
switch (word)
{
case 0:
rec.data[word] = 055; // Label
break;
case 1:
rec.data[word] = (ushort)((data != 5) ? 0x0000 : 0x0004); // Cooling Fault detected will fire trigger
break;
case 2:
case 3:
rec.data[word] = 0x0000;
break;
default:
rec.data[word] = (UInt16)(0x0101 * data);
break;
}
}
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfA708NextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting...");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(1000);
//Check that trigger occurred
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferStatusGet(rxBuffer, out rxAcqStatus, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (rc == MAXT_SUCCESS && (rxAcqStatus & MXF_RXACQ_STATUS_TRIG_OCCURRED) > 0)
{
rc = mxfRxAcqTrigTimeGet(rxBuffer, out trigTime);
if (rc == MAXT_SUCCESS)
{
time = time.AddMilliseconds(trigTime / 1000);
usec = trigTime % 1000;
Console.WriteLine();
Console.WriteLine("Event triggered at {0}:{1:000}", time.ToString("yyyy-MM-dd HH:mm:ss:ff"), usec);
}
}
else if (rc == MAXT_SUCCESS)
Console.WriteLine("Trigger not fired.");
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Receiving...");
rc = mxfA708RxAcqRead(rxBuffer, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
Console.WriteLine("Strings received: {0}", msgCount);
if (rc == MAXT_SUCCESS)
DisplayDataArray(msgCount, rxHostBuffer);
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
//Free trigger condition list
if (rc == MAXT_SUCCESS)
// Catch any previous failing function
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);
}
//Frees all buffers
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer != 0)
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
if (rxHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxHostBuffer);
// Terminate
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
/********************************************************************************************************************/
//DisplayDataArray
/********************************************************************************************************************/
private static void DisplayDataArray(UInt64 recNum, IntPtr rxHostBuffer)
{
UInt64 iRec, iData;
IntPtr p = rxHostBuffer;
Console.WriteLine();
for (iRec = 0; iRec < recNum; iRec++)
{
rec = (MXF_A708_DATAREC)Marshal.PtrToStructure(p, typeof(MXF_A708_DATAREC));
Console.WriteLine("{0}: Timetag: {1} Control: {2:x2} Size: {3}", iRec, rec.timeTag, rec.control, rec.dataSize);
Console.Write("Data: ");
for (iData = 0; iData < rec.dataSize / 2; iData++)
{
Console.WriteLine("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
}
}
Updated 10/23/2023