MX Foundation 4
ar629_freshnessCounter_manyCID.cs
/*****************************************************************************
//
// File:
// ar629_freshnessCounter_manyCID.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 in block
// mode and set the ARINC 629 data. 2 labels 0xA and 0xB with different CIDs
// are transmitted. Label 0xA has FC enabled.
// We also check if the FC incrementation is correct.
//
// Hardware Requirements:
// - MAXT FlexMulti-629 or MAXT 500 series carrier with IPM-629.
//
*****************************************************************************/
#define TTL
#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
namespace ar629_example
{
class ar629_freshnessCounter_manyCID
{
const int MAX_RX_RECORDS_TO_RECEIVE = 1000;
const int MAX_TX_RECORDS_TO_TRANSMIT = 1;
static void Main(string[] args)
{
int RX_BUFFER_SIZE = MAX_RX_RECORDS_TO_RECEIVE * Marshal.SizeOf(typeof(MXF_A629_DATAREC));
int TX_BUFFER_SIZE = MAX_TX_RECORDS_TO_TRANSMIT * Marshal.SizeOf(typeof(MXF_A629_DATAREC));
UInt32 rc;
UInt64 server;
var module = new UInt64[1];
UInt64 count = 0;
UInt64 rxChannel = 0;
var txChannel = new UInt64[2];
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[2];
IntPtr hostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
IntPtr rec629 = IntPtr.Zero;
UInt64 dev, mod, port;
var minorFrame = new MXF_A629_TXPERIODIC_MJRFRAME_MSG[3];
UInt64 index, word;
UInt64 status, msgsCount, bytesCount, indexBuffer, fcCounter = 0;
{
data = new UInt16[258]
};
{
data = new UInt16[258]
};
// Connects to services and initialize environment
#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.WriteLine("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
// Initialize the server
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
}
rc = mxfSystemInit(server);
// Obtain the first ARINC 629 Protocol RX channel (RX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfChannelGet(server, MXF_CLASS_A629, MXF_SCLASS_BM_CHANNEL, MXF_MODULE_ALL, 0, out rxChannel);
// Obtain the first ARINC 629 Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfChannelAllGet(server, MXF_CLASS_A629, MXF_SCLASS_RT_CHANNEL, MXF_MODULE_ALL, 2, 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 nanoseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Get the physical port location
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(rxChannel, out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Acquisition Channel (RX) location={0}.{1}.{2}", dev, mod, port);
}
for (index = 0; (index < count) && (rc == MAXT_SUCCESS); index++)
{
rc = mxfChannelLocationGet(txChannel[index], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Transmitter Channel (TX) location={0}.{1}.{2}", dev, mod, port);
}
if (rc == MAXT_SUCCESS)
rc = mxfChannelInfoGet(rxChannel, IntPtr.Zero, out module);
// Set interface to SIM or TTL
#if TTL
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A629_MODULE_INTERFACE, VMXF_A629_MODULE_INTERFACE_TTL);
#else
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A629_MODULE_INTERFACE, VMXF_A629_MODULE_INTERFACE_SIM);
#endif
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel, (UInt64)RX_BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Allocate two TX Periodic Update Message buffer
for (indexBuffer = 0; (indexBuffer < count) && (rc == MAXT_SUCCESS); indexBuffer++)
{
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[indexBuffer], indexBuffer, (UInt64)TX_BUFFER_SIZE, out txBuffer[indexBuffer], IntPtr.Zero);
}
// Allocate host buffer
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(Math.Max(RX_BUFFER_SIZE, TX_BUFFER_SIZE));
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
try
{
txHostBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(txRec629)*10);
}
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.WriteLine();
Console.WriteLine("Acquisition started");
}
}
//Set the TI to 25 msec
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A629_MODULE_TI, 25000000);
//Set the SG to 16
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_A629_MODULE_SG, VMXF_A629_MODULE_SG_16);
//Sets the TG for the different RTs
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A629_RT_TG, 2);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[1], KMXF_A629_RT_TG, 4);
//Sets the CID for the different labels
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_A629_RT_CID, 0xC);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[1], KMXF_A629_RT_CID, 0xD);
//Stops and clears the major frames
for (index = 0; (index < count) && (rc == MAXT_SUCCESS); index++)
{
rc = mxfTxPeriodicMajorFrameStop(txChannel[index], 0, 0);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicMajorFrameClear(txChannel[index], 0);
}
//--- Sets the minor frame #0 with 1 WordString --//
if (rc == MAXT_SUCCESS)
{
//WordString #0 : Label 0xA, CID 0xC
minorFrame[0].buffer = txBuffer[0];
minorFrame[0].label = 0xA;
minorFrame[0].cid = 0;
minorFrame[0].length = 1;
minorFrame[0].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_DEFAULT;
rc = mxfA629TxPeriodicMajorFrameSet(txChannel[0], 0, 0, 1, minorFrame);
// Adds a FC to label 0xA
if (rc == MAXT_SUCCESS)
{
fc.enable = Convert.ToUInt64(true);
fc.position = 1;
fc.mask = 0x00f0;
fc.modulo = 1;
rc = mxfA629MsgFreshnessCounterSet(txBuffer[0], ref fc);
}
}
//------------------------------------------------//
//--- Sets the minor frame #0 with 1 WordString --//
if (rc == MAXT_SUCCESS)
{
//WordString #0 : Label 0xB, CID 0xD
minorFrame[0].buffer = txBuffer[1];
minorFrame[0].label = 0xB;
minorFrame[0].cid = 0;
minorFrame[0].length = 1;
minorFrame[0].options = MXF_A629_TXPERIODIC_MJRFRAME_MSG_OPT_DEFAULT;
rc = mxfA629TxPeriodicMajorFrameSet(txChannel[1], 0, 0, 1, minorFrame);
}
//------------------------------------------------//
//--- Sets default data for label 0xA, CID 0xC ---//
if (rc == MAXT_SUCCESS)
{
txRec629.control = 0;
txRec629.repeatCount = 1;
txRec629.dataSize = 4; //4 bytes (label (2 bytes) + 1 data words)
txRec629.data[0] = 0;
txRec629.data[1] = 0x1111;
Marshal.StructureToPtr(txRec629, txHostBuffer, false);
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[0], 1, txHostBuffer);
}
//------------------------------------------------//
//--- Sets default data for label 0xB, CID 0xD ---//
if (rc == MAXT_SUCCESS)
{
txRec629.control = 0;
txRec629.repeatCount = 1;
txRec629.dataSize = 4;
txRec629.data[0] = 0;
txRec629.data[1] = 0x2222;
Marshal.StructureToPtr(txRec629, txHostBuffer, false);
rc = mxfA629TxPeriodicUpdateMsgWrite(txBuffer[1], 1, txHostBuffer);
}
//------------------------------------------------//
//Starts the major frames in the Block mode
for (index = 0; (index < count) && (rc == MAXT_SUCCESS); index++)
{
majorProperties.mode = MXF_A629_TXPERIODIC_MJRFRAME_PROPERTIES_MODE_BLOCK;
majorProperties.reserved = 0;
rc = mxfA629TxPeriodicMajorFrameStart(txChannel[index], 0, ref majorProperties);
}
//Waiting 2 seconds. Allows major frame to run a little bit.
if (rc == MAXT_SUCCESS)
mxfSleep(2000);
//Stops the major frames
for (index = 0; (index < count) && (rc == MAXT_SUCCESS); index++)
{
rc = mxfTxPeriodicMajorFrameStop(txChannel[index], 0, 0);
}
// Stop acquisition and read data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
if (rc == MAXT_SUCCESS)
{
rc = mxfA629RxAcqRead(rxBuffer, 0, (UInt64)RX_BUFFER_SIZE, out status, out msgsCount, out bytesCount, hostBuffer);
if (rc == MAXT_SUCCESS)
{
//Writes the program results in a file (results.txt)
string directoryPath = Directory.GetCurrentDirectory();
directoryPath += "/results.txt";
// Creates a file to write to.
using (StreamWriter w = File.CreateText(directoryPath))
{
}
using (StreamWriter w = File.AppendText(directoryPath))
{
// Display received messages
rec629 = hostBuffer;
for (index = 0; (index < msgsCount) && (rc == MAXT_SUCCESS); index++)
{
rxRec629 = (MXF_A629_DATAREC)Marshal.PtrToStructure(rec629, typeof(MXF_A629_DATAREC));
w.Write(" {0:D2}: Timetag={1:D12}, Size={2}", index, rxRec629.timeTag, rxRec629.dataSize);
if ((rxRec629.control & MXF_A629_RX_REC_CTRL_STRING_CRC_ERROR) == MXF_A629_RX_REC_CTRL_STRING_CRC_ERROR)
w.Write(" CRC error");
if ((rxRec629.control & MXF_A629_RX_REC_CTRL_STRING_DATA_SYNC_ERROR) == MXF_A629_RX_REC_CTRL_STRING_DATA_SYNC_ERROR)
w.Write(" Data sync error");
if (((rxRec629.control & (MXF_A629_RX_REC_CTRL_STRING_LABEL_MANCHESTER_ERROR)) == (MXF_A629_RX_REC_CTRL_STRING_LABEL_MANCHESTER_ERROR)) ||
((rxRec629.control & (MXF_A629_RX_REC_CTRL_STRING_DATA_MANCHESTER_ERROR)) == (MXF_A629_RX_REC_CTRL_STRING_DATA_MANCHESTER_ERROR)))
w.Write(" Manchester error");
if (((rxRec629.control & (MXF_A629_RX_REC_CTRL_STRING_LABEL_PARITY_ERROR)) == (MXF_A629_RX_REC_CTRL_STRING_LABEL_PARITY_ERROR)) ||
((rxRec629.control & (MXF_A629_RX_REC_CTRL_STRING_DATA_PARITY_ERROR)) == (MXF_A629_RX_REC_CTRL_STRING_DATA_PARITY_ERROR)))
w.Write(" Parity error");
if ((rxRec629.control & MXF_A629_RX_REC_CTRL_STRING_EOS_ERROR) == MXF_A629_RX_REC_CTRL_STRING_EOS_ERROR)
w.Write(" End of String error");
w.Write("\n Data=");
for (word = 0; word < rxRec629.dataSize / 2; word++)
{
w.Write("{0:X4} ", rxRec629.data[word]);
if (rxRec629.data[0] == 0xC00A)
{
if (word == fc.position)
{
if (fcCounter == ((rxRec629.data[word] & fc.mask) >> 4))
{
w.Write("Freshness Counter ok. \n");
fcCounter++;
if (fcCounter == 16)
fcCounter = 0;
}
else
{
w.Write("ERROR Freshness Counter. \n");
}
}
}
}
w.Write("\n");
rc = mxfA629NextDataRecordPtrGet(rec629, out rec629);
}
}
}
}
// Catch any previous 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");
// Free all buffers and terminate
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
for (indexBuffer = 0; indexBuffer < 2; indexBuffer++)
{
if (txBuffer[indexBuffer] != 0)
mxfTxPeriodicUpdateMsgBufferFree(txBuffer[indexBuffer]);
}
if (hostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(hostBuffer);
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
}
}
Updated 10/23/2023