MX Foundation 4
ar664_recorder.cs
/*******************************************************************************
//
// File:
// ar664_recorder.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 capture and record A664 data to a binary file for
// replaying it with the file with the same timing.
//
// This player program used ar664_playback.c.
//
// Hardware Requirements:
// - FlexMulti with A664
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace ar664_example
{
public class ar664_recorder
{
private const UInt32 TEST_DATASIZE = 64;
private const UInt64 DATARECS_BUFFER_SIZE_MAX = 64 * 1024;
private const string SRC_IP_ADRS = "10.0.0.111";
private const UInt32 SRC_UDP_PORT = 1234;
private const string DST_IP_ADRS = "10.0.0.222";
private const UInt32 DST_UDP_PORT = 5678;
static void Main(string[] args)
{
UInt64 server = 0;
UInt64 vlTx = 0;
UInt64 portTx = 0;
UInt64 rxBuffer = 0;
var data = new MXF_A664_DATAREC();
var module = new UInt64[1];
UInt64 device = 0;
var phyChn = new UInt64[1];
IntPtr recPtr = IntPtr.Zero;
IntPtr rxRec = IntPtr.Zero;
IntPtr txRec = IntPtr.Zero;
var vlParamTx = new MXF_A664_VL_PARAM_TX();
var portParam = new MXF_A664_PORT_PARAM();
int srcIpAdrs = 0, dstIpAdrs = 0;
UInt64 status;
UInt64 usedBytes, freeBytes;
UInt64 msgCount = 0;
UInt64 acqStopTime;
UInt32 iRecord, idata;
UInt32 maxTxRecords = 16;
UInt32 vlid = 100;
UInt32 rc;
UInt64 count = 0;
// Connect to server
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", FALSE, &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}\n", rc);
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
// Initialize server, allocate base resources
rc = mxfSystemInit(server);
// Set Timebase
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get the first A66 module
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A664, 1, out count, module);
// Obtain the first ARINC 664 Phy channel (Phy logical #0)
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A664, MXF_SCLASS_RX_CHANNEL, 1, out count, phyChn);
// If module or channel not found, return an error
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(phyChn[0], KMXF_A664_PHY_LOOPBACK, VMXF_A664_PHY_LOOPBACK_EXTERNAL);
if (rc == MAXT_SUCCESS)
mxfSleep(500);
#endif
// Create the transmission (TX) virtual link
if (rc == MAXT_SUCCESS)
{
vlParamTx.dir.Tx.subVl = new MXF_A664_VL_PARAM_TX.Dir.TX.SubVl[4];
vlParamTx.direction = MXF_A664_VL_DIR_TX;
vlParamTx.VLId = vlid;
vlParamTx.frameType = MXF_A664_FRAME_TYPE_ARINC664;
vlParamTx.frameSizeMax = 512;
vlParamTx.dir.Tx.subVlNumber = 1;
vlParamTx.dir.Tx.bag = 1 * 1000 * 1000; // 1ms
vlParamTx.dir.Tx.netSelection = MXF_A664_NETSELECT_ALL;
vlParamTx.dir.Tx.Ede.enable = VMXF_DISABLE;
vlParamTx.dir.Tx.subVl[0].bufferSize = vlParamTx.frameSizeMax;
vlParamTx.dir.Tx.subVl[0].maxBuffers = maxTxRecords;
rc = mxfA664VlCreate(phyChn[0], ref vlParamTx, out vlTx);
}
// Create the transmission SAP port
if (rc == MAXT_SUCCESS)
{
srcIpAdrs = BitConverter.ToInt32(IPAddress.Parse(SRC_IP_ADRS).GetAddressBytes(), 0);
srcIpAdrs = IPAddress.HostToNetworkOrder(srcIpAdrs);
dstIpAdrs = BitConverter.ToInt32(IPAddress.Parse(DST_IP_ADRS).GetAddressBytes(), 0);
dstIpAdrs = IPAddress.HostToNetworkOrder(dstIpAdrs);
portParam.portType = MXF_A664_PORT_TYPE_COM;
portParam.family = MXF_A664_PORT_FAMILY_IPV4;
portParam.mode = MXF_A664_PORT_MODE_QUEUING;
portParam.type.COM.destAddress.port = DST_UDP_PORT;
portParam.type.COM.destAddress.version.IPv4.address = (UInt32)dstIpAdrs;
portParam.type.COM.srcAddress.port = SRC_UDP_PORT;
portParam.type.COM.srcAddress.version.IPv4.address = (UInt32)srcIpAdrs;
rc = mxfA664PortCreate(vlTx, ref portParam, out portTx);
}
if (rc == MAXT_SUCCESS)
{
try
{
txRec = Marshal.AllocHGlobal((int)DATARECS_BUFFER_SIZE_MAX);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// In RAW mode the acquisition buffer get the data
// directly from the physical port. A buffer must
// be however allocated for the port.
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(phyChn[0], 20000, out rxBuffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
{
try
{
rxRec = Marshal.AllocHGlobal((int)DATARECS_BUFFER_SIZE_MAX);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Set acquisition mode
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
// Start acquisition, check status
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqBufferStatusGet(rxBuffer, out status, out msgCount, out usedBytes, out freeBytes);
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_RUNNING) > 0)
Console.Write("Acquisition Running\n\n");
}
recPtr = txRec;
// Build the Transmit Records
for (iRecord = 0; iRecord < maxTxRecords && rc == MAXT_SUCCESS; iRecord++)
{
data = new MXF_A664_DATAREC();
data.data = new byte[8192];
data.control = 0;
data.dataSize = TEST_DATASIZE;
data.repeatCount = 1;
for (idata = 0; idata < TEST_DATASIZE; idata++)
data.data[idata] = (byte)((idata + ((iRecord * TEST_DATASIZE) & 0xff)) & 0xff);
Marshal.StructureToPtr(data, recPtr, false);
rc = mxfA664NextDataRecordPtrGet(recPtr, out recPtr);
}
// Transmit the data
if (rc == MAXT_SUCCESS)
{
rc = mxfA664TxAperiodicWrite(portTx, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, maxTxRecords, txRec, out count);
Console.Write("{0} frames transmitted\n\n", count);
// Wait for complete transmission
mxfSleep(2000);
}
// Receive the data
if (rc == MAXT_SUCCESS)
rc = readAcquisitionData(rxBuffer, rxRec);
// Terminate Acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(rxBuffer);
// Make sure the acquisition is stopped
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqBufferStatusGet(rxBuffer, out status, out msgCount, out usedBytes, out freeBytes);
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_STOPPED) > 0)
{
rc = mxfRxAcqStopTimeGet(rxBuffer, out acqStopTime);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition stopped at {0}\n", acqStopTime);
}
}
// Free Ports Resources
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferFree(rxBuffer);
// Free COM ports
if (rc == MAXT_SUCCESS)
rc = mxfA664PortRelease(portTx);
if (rc == MAXT_SUCCESS)
rc = mxfA664VlRelease(vlTx);
// Free datarec buffers
if (txRec != IntPtr.Zero)
Marshal.FreeHGlobal(txRec);
if (rxRec != IntPtr.Zero)
Marshal.FreeHGlobal(rxRec);
Console.Write("\n\nPress a key to terminate rc=0x{0:x8}\n", rc);
Console.ReadKey();
return;
}
private static UInt32 readAcquisitionData(UInt64 rxBuffer, IntPtr rec664)
{
IntPtr recPtr = rec664;
var rxRec664 = new MXF_A664_DATAREC();
UInt64 recordIdx, dataIdx;
UInt64 status, msgsCount, bytesCount;
UInt32 rc = 0;
BinaryWriter fp;
int datarecSize; // max is 8240, so 16 bits, in c we use a uint32
string fileName = "recorder.raw";
try
{
fp = new BinaryWriter(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write));
}
catch
{
return MAXT_ERROR_FILE_OPEN;
}
// Read and display records
rc = mxfA664RxAcqRead(rxBuffer, 0, DATARECS_BUFFER_SIZE_MAX, out status, out msgsCount, out bytesCount, rec664);
if (rc == MAXT_SUCCESS)
Console.Write("{0} records received\n\n", msgsCount);
for (recordIdx = 0; recordIdx < msgsCount && rc == MAXT_SUCCESS; recordIdx++)
{
rxRec664 = new MXF_A664_DATAREC();
rxRec664.data = new byte[8192];
rxRec664 = (MXF_A664_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A664_DATAREC));
Console.Write(" {0:000}: timeTag={1:D12}, Size={2}", recordIdx, rxRec664.timeTag, rxRec664.dataSize);
Console.Write("\n data=");
for (dataIdx = 0; dataIdx < rxRec664.dataSize; dataIdx++)
{
Console.Write("{0:X2} ", rxRec664.data[dataIdx]);
if (((dataIdx + 1) % 8) == 0 && (dataIdx + 1 < rxRec664.dataSize))
Console.Write("\n ");
}
Console.Write("\n\n");
datarecSize = Marshal.SizeOf(typeof(MXF_A664_DATAREC)) - rxRec664.data.Length + (int)rxRec664.dataSize;
try
{
// Write the total length of the record
fp.Write(ObjectToByteArray(datarecSize, Marshal.SizeOf(datarecSize)));
// Write the record
fp.Write(ObjectToByteArray(rxRec664, datarecSize));
}
catch
{
rc = MAXT_ERROR_FILE_WRITING;
}
mxfA664NextDataRecordPtrGet(recPtr, out recPtr);
}
fp.Flush();
fp.Close();
return rc;
}
private static byte[] ObjectToByteArray(object obj, int size)
{
if (obj == null)
return null;
byte[] arr = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(obj));
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, size);
Marshal.FreeHGlobal(ptr);
return arr;
}
}
}
Updated 03/30/2022