MX Foundation 4
ar708_aperiodic.cs
/*******************************************************************************
//
// File:
// ar708_aperiodic.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 shows how to use the different types of aperiodic transmissions
// with ARINC 708 channels.
//
// 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_aperiodic
{
private const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
private const int BUFFER_SIZE = 1 * 1024 * 1024;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
var module = new UInt64[1];
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
UInt32 txBufferSize;
uint rxBufferSize = 0;
IntPtr rxHostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 dev, mod, port;
#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);
//Enables 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;
}
}
}
//Allocate 4kb for Rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 4 * 1024;
//Allocates Rx acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel[0], rxBufferSize, out rxBuffer, IntPtr.Zero);
try
{
rxHostBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
//Sets timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
//Get the physical port
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(rxChannel[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Acquisition Channel (RX) location = {0}.{1}.{2}", dev, mod, port);
}
}
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(txChannel[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitter Channel (TX) location = {0}.{1}.{2}", dev, mod, port);
}
}
//Start the acquisition process
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
{
//Start ARINC 708 data transmission
rc = StartAperiodicTransmissionDefault(txBuffer, txHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = StartAperiodicTransmissionAbsolute(device, txBuffer, txHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = StartAperiodicTransmissionRecordAbsolute(device, txBuffer, txHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = StartAperiodicTransmissionRecordRelative(txBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
}
//Stops Acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
//Free device and host buffers
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer != 0)
//Get 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.WriteLine();
Console.WriteLine("Terminating...");
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
/***********************************************************************/
//ReadAcquisitionData
/***********************************************************************/
private static UInt32 ReadAcquisitionData(UInt64 rxBuffer, IntPtr rxHostBuffer)
{
UInt64 status, msgsCount, bytesCount;
UInt32 rc = 0;
//Read and display records
Console.WriteLine();
Console.WriteLine("Receiving!");
rc = mxfA708RxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, rxHostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("String received count: {0}", msgsCount);
}
//Display Strings
if (rc == MAXT_SUCCESS)
DisplayDataArray(msgsCount, rxHostBuffer);
if (rc != MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Acquisition read failed.");
}
return rc;
}
/***********************************************************************/
//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:x8} Size: {3}", iRec, rec.timeTag, rec.control, rec.dataSize);
Console.Write("Data: ");
for(iData = 0; iData < rec.dataSize/2; iData++)
{
Console.Write("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
/***********************************************************************/
//WaitQueueEmpty
/***********************************************************************/
private static UInt32 WaitQueueEmpty(UInt64 txBuffer)
{
UInt64 msgCount, usedBytes, freeBytes;
UInt32 rc;
//Wait until all records are sent on the interface
do
{
rc = mxfTxAperiodicBufferStatusGet(txBuffer, out msgCount, out usedBytes, out freeBytes);
if (rc != MAXT_SUCCESS)
return rc;
} while (msgCount != 0);
//Make sure they are received
mxfSleep(100);
return MAXT_SUCCESS;
}
/***********************************************************************/
//StartAperiodicTransmissionDefault
/***********************************************************************/
private static UInt32 StartAperiodicTransmissionDefault(UInt64 txBuffer, IntPtr txHostBuffer)
{
MXF_A708_DATAREC rec;// = txHostBuffer;
IntPtr p = txHostBuffer;
UInt64 data, word = 0;
UInt32 rc = 0;
UInt32 delay100ms = 100000000;
rec.data = new UInt16[128];
//In the example below the records are sent back-to-back 100ms in the future
Console.WriteLine();
Console.WriteLine("Aperiodic Transmission (Relative Start Time - Default)");
//Prepares records to send for basic transmission/reception test
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 200;
for (word = 0; word < rec.dataSize / 2; word++)
{
switch (word)
{
case 0:
rec.data[word] = 055; //Label
break;
case 1:
case 2:
case 3:
rec.data[word] = 0x0000;
break;
default:
rec.data[word] = (UInt16)(0x0101 * data);
break;
}
}
Marshal.StructureToPtr(rec, p, false);
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting!");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
/***********************************************************************/
//StartAperiodicTransmissionAbsolute
/***********************************************************************/
private static UInt32 StartAperiodicTransmissionAbsolute(UInt64 device, UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt32 rc;
UInt32 delay100ms = 100000000;
UInt64 currentTIme;
UInt64 data, word;
rec.data = new UInt16[128];
//Get the current time
rc = mxfDeviceTimerGet(device, out currentTIme);
if (rc != MAXT_SUCCESS)
return rc;
Console.WriteLine();
Console.WriteLine("Aperiodic Transmisison (Absolute)");
if (rc == MAXT_SUCCESS)
{
for(data = 0; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 200;
for(word = 0; word < rec.dataSize / 2; word++)
{
switch(word)
{
case 0:
rec.data[word] = 055;
break;
case 1:
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)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting!");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME, currentTIme + delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
//Wait TX completion
if (rc == MAXT_SUCCESS)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
/***********************************************************************/
//StartAperiodicTransmissionRecordAbsolute
/***********************************************************************/
private static UInt32 StartAperiodicTransmissionRecordAbsolute(UInt64 device, UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt64 currentTime;
UInt32 rc;
UInt64 data, word;
rec.data = new UInt16[128];
Console.WriteLine();
Console.WriteLine("Aperiodic transmission (Absolute with timetag)");
//Get the current time
rc = mxfDeviceTimerGet(device, out currentTime);
if (rc != MAXT_SUCCESS)
return rc;
currentTime += 100000000;
for(data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = currentTime + (data*5000000);
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 200;
for (word = 0; word < rec.dataSize / 2; word++)
{
switch (word)
{
case 0:
rec.data[word] = 055;
break;
case 1:
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)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
//Transmit the array of records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting!");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
//Wait TX Completion
if (rc == MAXT_SUCCESS)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
/***********************************************************************/
//StartAperiodicTransmissionRecordRelative
/***********************************************************************/
private static UInt32 StartAperiodicTransmissionRecordRelative(UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt64 currentTime;
UInt32 rc = 0;
UInt64 data, word;
rec.data = new UInt16[128];
Console.WriteLine();
Console.WriteLine("Aperiodic transmission (Record Relative)");
if (rc == MAXT_SUCCESS)
{
for (data = 0, currentTime = 100000000; data<MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = currentTime + (data * 5000000);
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 200;
for (word = 0; word < rec.dataSize / 2; word++)
{
switch (word)
{
case 0:
rec.data[word] = 055;
break;
case 1:
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)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
//Transmit array of records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting!");
rc = mxfA708TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
rc = WaitQueueEmpty(txBuffer);
return rc;
}
}
}
Updated 10/23/2023