MX Foundation 4
ar429_periodic.cs
/*******************************************************************************
//
// File:
// arinc429_periodic.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 set a basic periodic transmission with the
// scheduler and update the ARINC data.
//
// Hardware Requirements:
// - MAXT FM429, FM664 or series 500 carrier with IPM-429/IPM-MULTI with A429 option installed on IP1
// - Loopback between first TX and RX ARINC 429 channels.
//
*******************************************************************************/
//#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
class arinc429_periodic
{
// User commands
const int BUFFER_SIZE = 4096; // 4KB
const int TX_MSG_LABEL = 5;
const int TX_MSG_SDI = 0;
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
UInt64 module = 0;
UInt64 rxChannel = 0;
UInt64 txChannel = 0;
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
IntPtr rec429 = IntPtr.Zero;
UInt64 dev, mod, port;
// Connect to MX Foundation library
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt32(false), 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);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleGet(device, 1, out module);
// Obtain channel #0 ( 429 TX channel)
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module, 0, out rxChannel);
// Obtain channel #8 ( 429 RX channel)
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module, 8, out txChannel);
// 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, 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, 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, KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel, BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Allocate TX Periodic Update buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel, TX_MSG_LABEL, BUFFER_SIZE, out txBuffer, IntPtr.Zero);
// Allocate host buffer
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");
// Start 429 data transmission
if (rc == MAXT_SUCCESS)
rc = TX429PeriodicScheduling(txChannel, txBuffer, rec429);
// Read 429 data in acquisition buffer
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;
}
/***************************************************************************************************************/
// TX429PeriodicScheduling
/***************************************************************************************************************/
private static UInt32 TX429PeriodicScheduling(UInt64 txChannel, UInt64 txBuffer, IntPtr recPtr)
{
UInt64 schedule;
UInt64 msg = 0;
UInt64 label, sdi, data, ssm, parity;
UInt32 rc;
var txBufferArray = new UInt64[] { txBuffer };
// Set the A429 records and create the schedule
// Create the periodic scheduler
rc = mxfTxPeriodicScheduleNew(txChannel, out schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000, 0, out msg);
// Define the number of buffer for the list and link to it
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 1, 0, txBufferArray);
// Set record for the buffer: ARINC 429 LABEL=005, SDI=0
if (rc == MAXT_SUCCESS)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
label = TX_MSG_LABEL;
sdi = TX_MSG_SDI;
data = 0x7fe;
parity = VMXF_A429_PARITY_ODD;
ssm = 1;
rc = mxfA429ArwCompose(label, sdi, data, ssm, parity, out rec429.data);
Marshal.StructureToPtr(rec429, recPtr, false);
if (rc == MAXT_SUCCESS)
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer, 1, recPtr);
}
// Run the scheduler, update records
if (rc == MAXT_SUCCESS)
{
Console.Write("Running periodic transmission, please wait...\n\r");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Wait 10 seconds
if (rc == MAXT_SUCCESS)
{
mxfSleep(10000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if (rc == MAXT_SUCCESS)
Console.Write("\n\rTransmission stopped\n\r");
return rc;
}
}
}
Updated 10/23/2023