MX Foundation 4
ar429_rx_sampling.cs
/****************************************************************************************************************
//
// File:
// ar429_rx_sampling.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 is a basic program for using the MX API sampling services
// for receiving data. In this example, message selection is used to
// discriminate data received.
//
// 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 DevelopmentAPICSharp
{
class ar429_rx_sampling
{
const int BUFFER_SIZE = 4096;
const int LABEL = 3;
const int SDI = 0;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
UInt64 module = 0;
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[2];
UInt64 count = 0;
IntPtr hostBuffer = IntPtr.Zero;
IntPtr hostBufferRead = IntPtr.Zero;
UInt64 schedule = 0;
UInt64 msg = 0;
UInt64 i, j;
UInt64 msgsCount, bytesCount, indexBuffer;
UInt64 sdi, data, ssm, parity;
UInt64 allocated = 0;
UInt64[] label = { LABEL };
// Connect to services and initialize environment
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt32(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(false), out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.WriteLine("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
// Initialize the server
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get the module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleGet(device, 2, out module);
// Get the first ARINC 429 Protocol RX channel (RX logical #0)
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, out count, rxChannel);
// Get the first ARINC 429 Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module, MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, out count, txChannel);
// If channel not found, return an error
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Set the channels to low speed
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A429_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A429_SPEED, 12500);
// Enable loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Set timebase to 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Alloc host buffers
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
if (rc == MAXT_SUCCESS)
{
try
{
hostBufferRead = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Allocate RX sampling buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, out allocated);
// Filter out a specific label
if (rc == MAXT_SUCCESS)
rc = mxfA429RxSamplingMsgSelectSet(rxBuffer, MXF_MSG_DESELECT, 1, label);
// Start sampling
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStart(rxBuffer);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Sampling started");
// Allocate TX Periodic Update buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], LABEL, BUFFER_SIZE, out txBuffer[0], out allocated);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], LABEL + 1, BUFFER_SIZE, out txBuffer[1], out allocated);
// Create the periodic scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel[0], 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, 2, 0, txBuffer);
// Set record for the buffer, will be ignored by sampling
if (rc == MAXT_SUCCESS)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
rc = mxfA429ArwCompose(LABEL, SDI, 0x7fe, 1, VMXF_A429_PARITY_ODD, out rec429.data);
if (rc == MAXT_SUCCESS)
{
Marshal.StructureToPtr(rec429, hostBuffer, false);
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer[0], 1, hostBuffer);
}
}
// Set a record that will be received by sampling
if (rc == MAXT_SUCCESS)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
rc = mxfA429ArwCompose(LABEL + 1, SDI, 0x12, 0, VMXF_A429_PARITY_ODD, out rec429.data);
if (rc == MAXT_SUCCESS)
{
Marshal.StructureToPtr(rec429, hostBuffer, false);
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer[1], 1, hostBuffer);
}
}
// Run the scheduler, update records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Running periodic transmission, please wait...");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Read and display records for 5 seconds
for (i = 0; (i < 10) && (rc == MAXT_SUCCESS); i++)
{
rc = mxfA429RxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, BUFFER_SIZE, out msgsCount, out bytesCount, hostBufferRead);
IntPtr recPtr = hostBufferRead;
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Read {0} messages", msgsCount);
for (j = 0; (rc == MAXT_SUCCESS) && (j < msgsCount); j++)
{
recSamp = (MXF_A429_SAMPREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A429_SAMPREC));
rc = mxfA429ArwDecompose(recSamp.data, out label[0], out sdi, out data, out ssm, out parity);
if (rc == MAXT_SUCCESS)
{
Console.Write("{0:D2}: Timetag {1}", j, recSamp.timeTag);
Console.WriteLine("- ARINC word=[{0},{1},{2:x5},{3},{4}]", Convert.ToString((int)label[0], 8).PadLeft(3, '0'), sdi, data, ssm, (parity == VMXF_A429_PARITY_ODD) ? "ODD" : "EVEN");
if (rc == MAXT_SUCCESS)
rc = mxfA429NextSamplingRecordPtrGet(recPtr, out recPtr);
}
}
}
if (rc != MAXT_SUCCESS)
Console.WriteLine("Sampling read failed; rc={0:x8}", rc);
else
mxfSleep(500);
}
// Stop transmission
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleFree(schedule);
// Stop sampling
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStop(rxBuffer);
// Catch any previous failing function
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");
}
//Frees all buffers
if (rxBuffer != 0)
for (indexBuffer = 0; indexBuffer < 2; indexBuffer++)
{
if (txBuffer[indexBuffer] != 0)
mxfTxPeriodicUpdateMsgBufferFree(txBuffer[indexBuffer]);
}
// Terminate
if (hostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(hostBuffer);
if (hostBufferRead != IntPtr.Zero)
Marshal.FreeHGlobal(hostBufferRead);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
}
}
Updated 10/23/2023