MX Foundation 4
usb.cs
/*******************************************************************************
//
// File:
// usb.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 429 data over USB connection.
//
// Hardware Requirements:
// - MAXT FlexMulti with loopback between first TX and RX ARINC 429 Enhanced channels.
//
*******************************************************************************/
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Text;
using System.Runtime.InteropServices;
namespace DeviceMgmt_example
{
class usb
{
public const int BUFFER_SIZE = 4096;// 4KB
public const int TX_MSG_LABEL = 5;
public const int TX_MSG_SDI = 0;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
var module = new UInt64[1];
UInt64 count = 0;
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
IntPtr rec429 = IntPtr.Zero;
UInt64 dev, mod, port;
UInt64 usbDevCount;
// Detect MAXT USB device connected
rc = mxfSystemUSBDetect(out usbDevCount);
if (rc == MAXT_SUCCESS && usbDevCount == 0)
{
Console.Write("No MAXT USB device detected\n");
rc = MAXT_ERROR_DEVICE_NOT_DETECTED;
}
// Connect to the first USB device
if (rc == MAXT_SUCCESS)
rc = mxfSystemUSBServerConnect(0, out server);
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");
if (rc == MAXT_SUCCESS)
rc = mxfSystemInit(server);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_A429_EH, 1, out count, module);
// Obtain the first ARINC 429 Protocol RX channel (RX logical #0)
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A429, MXF_SCLASS_RX_CHANNEL, 1, out count, rxChannel);
// Obtain the first ARINC 429 Protocol TX channel (TX logical #0)
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_A429, MXF_SCLASS_TX_CHANNEL, 1, 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 microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Get the physical port location
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(rxChannel[0], 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[0], 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[0], KMXF_A429_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Allocate TX Periodic Update buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 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[0], txBuffer, rec429);
if (rc == MAXT_SUCCESS)
{
// Read 429 data in acquisition buffer
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);
rc = mxfSystemErrorStringGet(server, rc, (UInt32)buffer.Length, buffer);
Console.WriteLine(buffer + "\n");
}
Console.Write("\nTerminating\n");
// Free all buffers and terminate
if (device != 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 rec429Ptr)
{
IntPtr recPtr = rec429Ptr;
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, rec429Ptr);
for (j = 0; j < msgsCount && rc == MAXT_SUCCESS; j++)
{
rec429 = (MXF_A429_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_A429_DATAREC));
rc = mxfA429ArwDecompose(rec429.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, rec429.timeTag, label, sdi, data, ssm, (parity == VMXF_A429_PARITY_ODD) ? "ODD" : "EVEN");
if (rc == MAXT_SUCCESS)
rc = mxfA429NextDataRecordPtrGet(recPtr, out recPtr);
}
}
return rc;
}
/***************************************************************************************************************/
// TX429PeriodicScheduling
/***************************************************************************************************************/
private static UInt32 TX429PeriodicScheduling(UInt64 txChannel, UInt64 txBuffer, IntPtr rec429Ptr)
{
UInt64 schedule;
UInt64 msg = 0;
UInt64 label, sdi, data, ssm, parity;
UInt32 rc;
UInt64[] bufferArray = { 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, bufferArray);
// 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, rec429Ptr, true);
if (rc == MAXT_SUCCESS)
rc = mxfA429TxPeriodicUpdateMsgWrite(txBuffer, 1, rec429Ptr);
}
// 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