MX Foundation 4
ar429_aperiodic.cs
/*******************************************************************************
//
// File:
// ar429_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 perform a basic aperiodic transmission
// with ARINC 429 data.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX ARINC 429 Enhanced channels.
//
*******************************************************************************/
//#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
class ar429_aperiodic
{
const int BUFFER_SIZE = 4096;
const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
UInt64 count = 0;
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
IntPtr rec429 = IntPtr.Zero;
UInt64 dev, mod, port;
// Connect to services and initialize environment
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", 0, out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", 0, out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
// Initialize the server
Console.Write("\nStarting\n");
rc = mxfSystemInit(server);
if (rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
rc = mxfSystemResourcesInit(server, 0);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A429_EH, 1, out count, module);
// Obtain the first ARINC 429 Protocol RX channel (RX logical #0)
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, out count, rxChannel);
// Obtain the first ARINC 429 Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, out count, txChannel);
// If module or channel not found, return an error
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Get the physical port location
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(rxChannel[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("Acquisition Channel (RX) location={0}.{1}.{2}\n", dev, mod, port);
}
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(txChannel[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("Transmitter Channel (TX) location={0}.{1}.{2}\n", dev, mod, port);
}
//Activate loopback before transmission and reception
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Allocate Aperiodic TX static buffer for HIGH priority queue
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(txChannel[0], MXF_TXAPERIODIC_PRIORITY_HIGH, BUFFER_SIZE, out txBuffer, IntPtr.Zero);
// Alloc host buffers
if (rc == MAXT_SUCCESS)
{
try
{
rec429 = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// 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)
Console.Write("\nAcquisition started\n\r");
}
if (rc == MAXT_SUCCESS)
{
// Start 429 data transmission
rc = TX429StartAperiodicTransmissionDefault(txBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = TX429StartAperiodicTransmissionAbsolute(device, txBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = TX429StartAperiodicTransmissionRecordAbsolute(device, txBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = TX429StartAperiodicTransmissionRecordRelative(txBuffer, rec429);
if (rc == MAXT_SUCCESS)
rc = RX429ReadAcquisitionData(rxBuffer, rec429);
}
// Stop and flush 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");
}
// Catch any previous error
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
if (mxfSystemErrorStringGet(server, rc, (UInt32)buffer.Capacity, buffer) != MAXT_SUCCESS)
{
buffer.Clear();
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(buffer + "\n\r");
}
Console.Write("\nTerminating\n");
// Free all buffers and terminate
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer != 0)
if (rec429 != IntPtr.Zero)
Marshal.FreeHGlobal(rec429);
Console.Write("\nPress enter to terminate\n");
Console.ReadKey();
return;
}
/***************************************************************************************************************/
// RX429ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 RX429ReadAcquisitionData(UInt64 rxBuffer, IntPtr rec429)
{
IntPtr recPtr = rec429;
var rxRec429 = new MXF_A429_DATAREC();
UInt64 status, msgsCount, bytesCount;
UInt64 label, sdi, data, ssm, parity;
UInt64 j;
UInt32 rc;
// Read and display records
rc = mxfA429RxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, rec429);
for (j = 0; j < msgsCount && rc == MAXT_SUCCESS; j++)
{
rxRec429 = (MXF_A429_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A429_DATAREC));
rc = mxfA429ArwDecompose(rxRec429.data, out label, out sdi, out data, out ssm, out parity);
if (rc == MAXT_SUCCESS)
{
Console.Write("{0:00}: Timetag {1} - ARINC word=[{2:000},{3},{4:x5},{5},{6} ]\n",
j, rxRec429.timeTag, label, sdi, data, ssm, parity == VMXF_A429_PARITY_ODD ? "ODD" : "EVEN");
rc = mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
}
return rc;
}
/***************************************************************************************************************/
// TX429WaitQueueEmpty
/***************************************************************************************************************/
private static UInt32 TX429WaitQueueEmpty(UInt64 txBuffer)
{
UInt64 msgCount, usedBytes, freeBytes;
UInt32 rc;
// Wait till 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;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionDefault
/***************************************************************************************************************/
private static UInt32 TX429StartAperiodicTransmissionDefault(UInt64 txBuffer, IntPtr rec429)
{
var txRec429 = new MXF_A429_DATAREC();
IntPtr recPtr;
UInt64 label, sdi, data, ssm, parity;
UInt32 record;
UInt64 delay100ms = 100000;
UInt32 rc;
// In the example below the record are sent back-to-back 100 ms in the future.
Console.Write("\nAperiodic transmission (Relative Start Time-Default)\n");
// Set the record
recPtr = rec429;
for (record = 0; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each record: ARINC 429 LABEL=005, SDI=1
txRec429.timeTag = 0;
txRec429.control = 0;
txRec429.repeatCount = 1;
txRec429.reserved = 0;
label = 005;
sdi = 1;
data = record * 8;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
mxfA429ArwCompose(label, sdi, data, ssm, parity, out txRec429.data);
Marshal.StructureToPtr(txRec429, recPtr, false);
mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (rc == MAXT_SUCCESS)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionAbsolute
/***************************************************************************************************************/
private static UInt32 TX429StartAperiodicTransmissionAbsolute(UInt64 device, UInt64 txBuffer, IntPtr rec429)
{
var rec = new MXF_A429_DATAREC();
IntPtr recPtr = IntPtr.Zero;
UInt64 label, sdi, data, ssm, parity;
UInt32 record, rc;
UInt32 delay100ms = 100000;
UInt64 currentTime;
// Get the current time
rc = mxfDeviceTimerGet(device, out currentTime);
if (rc != MAXT_SUCCESS)
return rc;
// In the example below the option MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME
// is used. In this case the records are sent back-to-back
// with a start transmission time based on the device clock + 100ms in the future.
Console.Write("\nAperiodic transmission (Absolute)\n");
recPtr = rec429;
for (record = 0; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each record: ARINC 429 LABEL=006, SDI=2
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.reserved = 0;
label = 6;
sdi = 2;
data = record * 16;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
mxfA429ArwCompose(label, sdi, data, ssm, parity, out rec.data);
Marshal.StructureToPtr(rec, recPtr, false);
mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME, currentTime + delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (rc == MAXT_SUCCESS)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionRecordAbsolute
/***************************************************************************************************************/
private static UInt32 TX429StartAperiodicTransmissionRecordAbsolute(UInt64 device, UInt64 txBuffer, IntPtr rec429)
{
var rec = new MXF_A429_DATAREC();
IntPtr recPtr = IntPtr.Zero;
UInt64 label, sdi, data, ssm, parity;
UInt32 record;
UInt64 currentTime;
UInt32 rc;
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME
// In this case the records are sent based on the absolute clock
// timing specified in the timetag field for each records.
Console.Write("\nAperiodic transmission (Absolute with timetag)\n");
// Get the current time
rc = mxfDeviceTimerGet(device, out currentTime);
if (rc != MAXT_SUCCESS)
return rc;
currentTime += 100000;
// Set the record
recPtr = rec429;
for (record = 0; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each records: ARINC 429 LABEL=007, SDI=1
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record is then sent with a 10 ms interval.
rec.timeTag = currentTime + ((record + 1) * 10000);
rec.control = 0;
rec.repeatCount = 1;
rec.reserved = 0;
label = 7;
sdi = 1;
data = record * 32;
ssm = 0;
parity = VMXF_A429_PARITY_ODD;
mxfA429ArwCompose(label, sdi, data, ssm, parity, out rec.data);
Marshal.StructureToPtr(rec, recPtr, false);
mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (rc == MAXT_SUCCESS)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
/***************************************************************************************************************/
// TX429StartAperiodicTransmissionRecordRelative
/***************************************************************************************************************/
private static UInt32 TX429StartAperiodicTransmissionRecordRelative(UInt64 txBuffer, IntPtr rec429)
{
var rec = new MXF_A429_DATAREC();
IntPtr recPtr = IntPtr.Zero;
UInt64 label, sdi, data, ssm, parity;
UInt32 record;
UInt64 currentTime;
UInt32 rc;
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME.
// The relative time between each records is set in the
// timetag field of the record array.
Console.Write("\nAperiodic transmission (Record Relative)\n");
// Set the record
recPtr = rec429;
for (record = 0, currentTime = 100000; record < MAX_TX_RECORDS_TO_TRANSMIT; record++)
{
// Set each records: ARINC 429 LABEL=010, SDI=1
// The scheduling timetag is as follow:
// 100 ms offset before first record.
// Each record is then sent with a 10 ms interval.
rec.timeTag = currentTime + ((record + 1) * 10000);
rec.control = 0;
rec.repeatCount = 1;
rec.reserved = 0;
label = 010;
sdi = 1;
data = record * 64;
parity = 0;
ssm = 0;
mxfA429ArwCompose(label, sdi, data, ssm, parity, out rec.data);
Marshal.StructureToPtr(rec, recPtr, false);
mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
// Transmit the array of records
rc = mxfA429TxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, rec429);
// Wait TX completion
if (rc == MAXT_SUCCESS)
rc = TX429WaitQueueEmpty(txBuffer);
return rc;
}
}
}
Updated 10/23/2023