MX Foundation 4
mil1553_aperiodic_event.cs
/*****************************************************************************
//
## File:
## mil1553_aperiodic_event.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 aperiodic transmission asynchronous
// event to transmit a major frame with MIL-STD-1553 BC channel and record using
// BM string mode.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti.
//
*****************************************************************************/
#define LOCAL
#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_aperiodic_event
{
const UInt64 FRAME_RATE = 20000000; //20 msec
const UInt32 ALMOST_FULL = 20;
const UInt32 ALMOST_EMPTY = 10;
private static UInt64 startTime = 0;
static void Main(string[] args)
{
UInt32 rc;
UInt64 deviceCount;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
UInt64 bm = 0;
var bcBufferTx = new UInt64[1];
var bmBufferRx = new UInt64[1];
UInt64 asyncEvent = 0;
var condition = new MXF_ASYNCEVENT_CONDITION[1];
MXF_ASYNCEVENT_HANDLER txHandler = TxHandler;
IntPtr recPtr = IntPtr.Zero;
UInt32 txBufferSize;
IntPtr txBuffer = IntPtr.Zero;
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
var rxRec1553 = new MXF_MIL1553_DATAREC();
UInt64 card, mod, port;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt32 loop = 0;
UInt32 data;
var errorString = new StringBuilder(200);
// Connect 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
// Initialize MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n\r");
rc = mxfSystemInit(server);
}
// Get handle of first Flex MIL-STD-1553 Bus controller channel
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, out deviceCount, device);
if (rc == MAXT_SUCCESS)
{
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_MIL1553MRT_EH, 1, out moduleCount, module);
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_MIL1553MRT, 1, out moduleCount, module);
}
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, out channelCount, bc);
// If module or channel not found, return an error
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Get handle of first Flex MIL - STD - 1553 bus monitor channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 0, out bm);
// Show channel locations for BM/BC
rc = mxfChannelLocationGet(bm, out card, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("BM location is Device:{0} Module:{1} Port:{2}\n", card, mod, port);
rc = mxfChannelLocationGet(bc[0], out card, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("BC location is Device:{0} Module:{1} Port:{2}\n", card, mod, port);
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bc[0], KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate enough buffer for at least ALMOST_FULL tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = ALMOST_FULL * (UInt32)Marshal.SizeOf(typeof(MXF_MIL1553_DATAREC));
// Device allocation
rc = mxfTxAperiodicBufferAlloc(bc[0], MXF_TXAPERIODIC_PRIORITY_LOW, txBufferSize, out bcBufferTx[0], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
txBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate 10KB buffer for rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 10 * 1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bm, rxBufferSize, out bmBufferRx[0], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Configure to BM to string mode
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_BM_MODE, VMXF_MIL1553_BM_MODE_STRING);
// Set timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Get current time and add 100 msec to it
if (rc == MAXT_SUCCESS)
rc = mxfDeviceTimerGet(device[0], out startTime);
startTime += 100000000;
// Configure aperiodic asynchronous event condition
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, txHandler, txBuffer, out asyncEvent);
if (rc == MAXT_SUCCESS)
{
condition[0].reserved = 0;
condition[0].condID = MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD;
condition[0].condition.txAperiodicBufferThreshold.almostEmpty = ALMOST_EMPTY;
condition[0].condition.txAperiodicBufferThreshold.almostFull = ALMOST_FULL;
condition[0].condition.txAperiodicBufferThreshold.buffer = bcBufferTx[0];
rc = mxfAsyncEventConditionsSet(asyncEvent, VMXF_ENABLE, 1, condition);
}
mxfSleep(10000);
// Start BM acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(bmBufferRx[0], MXF_RXACQ_FLAG_DEFAULT,0, 0);
// Read and display received messages for 5 seconds
if (rc == MAXT_SUCCESS)
{
do
{
rc = mxfMIL1553RxAcqRead(bmBufferRx[0], 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
recPtr = rxBuffer;
for (rxRec = 0; rxRec < msgCount && rc == MAXT_SUCCESS; rxRec++)
{
rxRec1553 = (MXF_MIL1553_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_DATAREC));
Console.Write("\n\r{0}:\t", rxRec1553.timeTag);
Console.Write("Control: 0x{0:x8}", rxRec1553.control);
for (data = 0; data < rxRec1553.dataSize / 2; data++)
{
if (data > 0 && data % 5 == 0)
Console.Write("\n\r\t\t\t");
Console.Write(" 0x{0:x4}", rxRec1553.data[data]);
}
Console.Write("\n\r");
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
} while (loop < 10);
}
// disable asynchronous event condition
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventConditionsSet(asyncEvent, VMXF_DISABLE, 1, condition);
if (rc == MAXT_SUCCESS)
// Clear aperiodic buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicClear(bcBufferTx[0], 0);
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(bmBufferRx[0]);
// Clear acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(bmBufferRx[0]);
// free buffers
if (txBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txBuffer);
if (rxBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxBuffer);
if (rc != MAXT_SUCCESS)
{
if (mxfSystemErrorStringGet(server, rc, (UInt32)errorString.Capacity, errorString) != MAXT_SUCCESS)
{
errorString.Clear();
errorString.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(errorString + "\n\r");
}
// Free all buffers and terminate
if (bcBufferTx[0] > 0)
{
rc = mxfTxAperiodicBufferFree(bcBufferTx[0]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bmBufferRx[0] > 0)
{
rc = mxfRxAcqBufferFree(bmBufferRx[0]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
// Unload MX Foundation library
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
private static UInt32 TxHandler(UInt64 asyncEvent, IntPtr pParam)
{
UInt32 rc = 0;
UInt64 count = 0;
var pendingInfo = new MXF_ASYNCEVENT_PENDING_INFO[1];
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
IntPtr recPtr = pParam;
// get asynchronous event information
rc = mxfAsyncEventPendingGet(asyncEvent, 1, out count, pendingInfo);
if (rc == MAXT_SUCCESS && count > 0)
{
if (pendingInfo[0].condID == MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD)
{
UInt64 cmd = 0;
// Send commands until it reaches almost full threshold
for (cmd = 0; cmd < pendingInfo[0].condition.txAperiodicBufferThreshold.almostFull - 1; cmd++)
{
// send two commands in simulated major frame
if (cmd % 2 > 0)
{
txRec1553.timeTag = startTime + 1000000; // 1 msec after other command
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
mxfMIL1553CommandCompose(0, 3, MXF_MIL1553_MSGTYPE_RX, 4, out txRec1553.data[0]);
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
txRec1553.data[4] = 0x3333;
}
else
{
startTime += FRAME_RATE; // one FRAME_RATE after the first command
txRec1553.timeTag = startTime;
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
mxfMIL1553CommandCompose(0, 5, MXF_MIL1553_MSGTYPE_RX, 4, out txRec1553.data[0]);
txRec1553.data[1] = 0x3333;
txRec1553.data[2] = 0x2222;
txRec1553.data[3] = 0x1111;
txRec1553.data[4] = 0x0000;
}
Marshal.StructureToPtr(txRec1553, recPtr, false);
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
// Send commands
rc = mxfMIL1553TxAperiodicWrite(pendingInfo[0].condition.txAperiodicBufferThreshold.buffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, cmd, pParam);
}
}
return rc;
}
}
}
Updated 10/23/2023