MX Foundation 4
ar708_tx_error_injection.cs
/*****************************************************************************
//
// File:
// ar708_tx_error_injection.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example demonstrates the usage of ARINC 708 channel class
// for transmission and reception with an error injection in transmission.
//
// 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_sampling
{
private const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[1];
var rec = new MXF_A708_DATAREC();
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
uint txBufferSize = 0;
uint rxBufferSize = 0;
IntPtr txHostBuffer = IntPtr.Zero;
IntPtr hostBuffer = IntPtr.Zero;
UInt64 schedule = 0;
UInt64 msg = 0;
UInt64 i, msgsCount, bytesCount;
UInt64 data, word;
UInt64 allocated_size_rx, allocated_size_tx;
#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); //Hey! lol
//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;
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 0, txBufferSize, out txBuffer[0], out allocated_size_tx);
//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;
rc = mxfRxSamplingBufferAlloc(rxChannel[0], rxBufferSize, out rxBuffer, out allocated_size_rx);
try
{
hostBuffer = 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);
//Starts Acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxSamplingStart(rxBuffer);
//Create the periodic scheduler
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleNew(txChannel[0], out schedule);
//Set Scheduling Values: Rate 5ms, Phase = 0us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 5000000, 0, out msg);
//Define the number of buffers for the list and link to it
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 1, 0, txBuffer);
//Sets records for the buffers
if (rc == MAXT_SUCCESS)
{
IntPtr recPtr = txHostBuffer;
rec.data = new UInt16[128];
//Prepares records to send for basic transmission/reception test with an error injection in transmission for the 3rd and 7th record
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
if (data == 2)
{
rec.control = (uint)MXF_A708_TX_REC_CTRL_MANCHESTERBIT_ERROR;
rec.repeatCount = 1;
rec.manchesterBitErr = 1000;
}
else if (data == 6)
{
rec.control = 0x1;
rec.repeatCount = 1;
rec.manchesterBitErr = 0;
}
else
{
rec.control = 0;
rec.repeatCount = 100;
rec.manchesterBitErr = 0;
}
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, recPtr, false);
rc = mxfA708NextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc == MAXT_SUCCESS)
rc = mxfA708TxPeriodicUpdateMsgWrite(txBuffer[0], MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
//Run the scheduler, update records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Running periodic transmission...");
rc = mxfTxPeriodicScheduleRun(schedule);
}
//Read and display records for 5 seconds
for (i = 0; i < 10 && rc == MAXT_SUCCESS; i++)
{
rc = mxfA708RxSamplingRead(rxBuffer, MXF_RXSAMPLING_FLAG_DEFAULT, 0, rxBufferSize, out msgsCount, out bytesCount, hostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Read {0} messages.", msgsCount);
DisplaySampDataArray(msgsCount, hostBuffer);
}
if (rc != 0)
{
Console.WriteLine();
Console.WriteLine("Sampling read failed.");
}
else
mxfSleep(500);
}
//Stops Transmission
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleFree(schedule);
//Stop Sampling
if (rc == MAXT_SUCCESS)
{
rc = mxfRxSamplingStop(rxBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Sampling stopped.");
}
}
//Free device and host buffers
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer[0] != 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...");
//Unloads MX Foundation Library
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
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.Write("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
Console.WriteLine();
}
}
private static void DisplaySampDataArray(UInt64 recNum, IntPtr rxHostBuffer)
{
UInt64 iRec, iData;
IntPtr p = rxHostBuffer;
Console.WriteLine();
for (iRec = 0; iRec < recNum; iRec++)
{
rec = (MXF_A708_SAMPREC)Marshal.PtrToStructure(p, typeof(MXF_A708_SAMPREC));
Console.WriteLine("{0}: Timetag: {1} Control: {2:x2} Size: {3}", iRec, rec.timeTag, rec.control, rec.dataSize);
for (iData = 0; iData < rec.dataSize / 2; iData++)
{
Console.Write("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
}
}
Updated 03/30/2022