MX Foundation 4
mil1553_bc_rt_data_assignment.cs
/*****************************************************************************
//
// File:
// mil1553_bc_rt_data_assignment.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 buffer assignments for BC and RT,
// periodic update message with asynchronous events, IRIG-B timebase, and
// fixed delay between messages of the frame.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#define LOOPBACK
#define LOCAL
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_exemple
{
public class mil1553_bc_rt_data_assignment
{
public struct EVENT_INFO
{
public UInt64 bcBuffer0;
public UInt64 bcBuffer1;
public UInt64 rtBuffer;
[StructLayout(LayoutKind.Explicit)]
public struct Recptr
{
[FieldOffset(0)] public IntPtr rtData;
[FieldOffset(0)] public IntPtr bcData0;
[FieldOffset(0)] public IntPtr bcData1;
[FieldOffset(0)] public UInt64 reserved1;
[FieldOffset(0)] public UInt64 reserved2;
}
public Recptr recptr;
}
static void Main(string[] args)
{
UInt64 server = 0;
UInt64 device = 0;
var module = new UInt64[1];
UInt64 bc = 0;
UInt64 bm = 0;
UInt64 rt4 = 0;
UInt64 rt5 = 0;
var acq1553Buffer = new UInt64[1];
var bcBuffer0 = new UInt64[1];
var bcBuffer1 = new UInt64[1];
var bcBuffer2 = new UInt64[1];
var rt4Buffer0 = new UInt64[1];
var rt4Buffer1 = new UInt64[1];
var rt5Buffer0 = new UInt64[1];
var rt5Buffer1 = new UInt64[1];
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[5];
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
var rxRec1553 = new MXF_MIL1553_DATAREC();
UInt32 rc = MAXT_SUCCESS;
UInt32 txDataSize = 4096;
IntPtr bcData0 = IntPtr.Zero;
IntPtr bcData1 = IntPtr.Zero;
IntPtr rtData = IntPtr.Zero;
UInt32 rxDataSize = 0;
IntPtr rxData = IntPtr.Zero;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt64 loop = 0;
UInt32 data;
var errorString = new StringBuilder(200);
UInt64 rate;
UInt64 last = 0, moduleCount = 0;
UInt64 irigbStatus = 0;
UInt64 asyncEvent = 0;
var condition = new MXF_ASYNCEVENT_CONDITION[1];
var bcCondition = new MXF_ASYNCEVENT_CONDITION[1];
var eventInfo = new EVENT_INFO();
IntPtr eventInfoPtr = IntPtr.Zero;
MXF_ASYNCEVENT_HANDLER EventHandler = eventHandler;
#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 device
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get handle of first module
if (rc == MAXT_SUCCESS)
{
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT_EH, 1, out moduleCount, module);
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_MIL1553MRT, 1, out moduleCount, module);
}
// Get handle of first MIL1553 Bus controller channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 1, out bc);
// Get handle of first MIL1553 Bus monitor channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 0, out bm);
// Get handle of MIL1553 remote terminal 4 channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + 4, out rt4);
// Get handle of MIL1553 remote terminal 5 channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 2 + 5, out rt5);
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm, KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate 4KB buffer for bc data buffer 0
if (rc == MAXT_SUCCESS)
{
txDataSize = 4096;
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 0, txDataSize, out bcBuffer0[0], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
bcData0 = Marshal.AllocHGlobal((int)txDataSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate 4KB buffer for bc data buffer 1
if (rc == MAXT_SUCCESS)
{
txDataSize = 4096;
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 1, txDataSize, out bcBuffer1[0], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
bcData1 = Marshal.AllocHGlobal((int)txDataSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate 0KB buffer for bc data buffer 2
if (rc == MAXT_SUCCESS)
{
txDataSize = 0;
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc, 2, txDataSize, out bcBuffer2[0], IntPtr.Zero);
}
// Allocate 4KB buffer for rt4 data buffer
if (rc == MAXT_SUCCESS)
{
txDataSize = 4096;
// RX 0-31 -. 0-31
// TX 0-31 -. 32-63
// MODE-CODE 0-31 -. 64-95
// RT 4 SA 1 RX
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt4, 1, txDataSize, out rt4Buffer0[0], IntPtr.Zero);
// RT 4 SA 30 RX
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt4, 30, txDataSize, out rt4Buffer1[0], IntPtr.Zero);
}
// Allocate 4KB buffer for rt5 data buffer
if (rc == MAXT_SUCCESS)
{
txDataSize = 4096;
// RT 5 SA 1 RX
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt5, 1, txDataSize, out rt5Buffer0[0], IntPtr.Zero);
// RT 5 SA 1 TX
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt5, 33, txDataSize, out rt5Buffer1[0], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rtData = Marshal.AllocHGlobal((int)txDataSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate 10KB buffer for 1553 acquisition data
if (rc == MAXT_SUCCESS)
{
rxDataSize = 10 * 1024;
rc = mxfRxAcqBufferAlloc(bm, rxDataSize, out acq1553Buffer[0], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxData = Marshal.AllocHGlobal((int)rxDataSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Configure IRIG-B
// Enable IRIG-B generator
if (rc == MAXT_SUCCESS)
{
Console.Write("Configuring IRIG-B\n");
rc = mxfAttributeUint64Set(device, KMXF_DEVICE_IRIGB_GEN_ENABLE, VMXF_ENABLE);
}
// Enable IRIG-B internal loopback
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(device, KMXF_DEVICE_IRIGB_GEN_LOOPBACK_ENABLE, VMXF_ENABLE);
// Set IRIG-B input to device input
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(device, KMXF_DEVICE_IRIGB_INPUT, VMXF_DEVICE_IRIGB_INPUT_DEVICE);
// Set IRIG-B signal to digital
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(device, KMXF_DEVICE_IRIGB_INPUT_SIGNAL, VMXF_DEVICE_IRIGB_INPUT_SIGNAL_DIGITAL);
// Wait IRIG-B signal locked
if (rc == MAXT_SUCCESS)
{
do
{
rc = mxfDeviceIrigbStatusGet(device, out irigbStatus);
} while (rc == MAXT_SUCCESS && (irigbStatus != MXF_IRIGB_STATUS_NO_SIGNAL) && (irigbStatus != MXF_IRIGB_STATUS_LOCKED));
}
// Set timebase to IRIG-B nsec
if (rc == MAXT_SUCCESS)
{
if (irigbStatus == MXF_IRIGB_STATUS_LOCKED)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_IRIGB_NSEC);
else
{
Console.Write("No IRIG-B signal found\n\r");
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_NSEC);
}
}
// Set the minor frame #0 using 5 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 4, Subaddress 1, RX, 4 words
rc = mxfMIL1553CommandCompose(4, 1, MXF_MIL1553_COMMAND_DIR_RX, 4, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBuffer0[0]; // link command to bcBuffer0
// Command #1 : Address 5, Subaddress 1, RX, 4 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(5, 1, MXF_MIL1553_COMMAND_DIR_RX, 4, out minorFrame[1].command);
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBuffer0[0]; // link command to bcBuffer0
minorFrame[1].delay = 8000; // send command 1 msec after preceding command
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
}
// Command #2 : Address 4, Subaddress 30, RX, 4 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(4, 30, MXF_MIL1553_COMMAND_DIR_RX, 4, out minorFrame[2].command);
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBuffer1[0]; // link command to bcBuffer1
minorFrame[2].delay = 8000; // send command 1 msec after preceding command
minorFrame[2].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
}
// Command #3 : Address 4, Subaddress 30, TX, 4 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(4, 30, MXF_MIL1553_COMMAND_DIR_TX, 4, out minorFrame[3].command);
minorFrame[3].modulo = 1;
minorFrame[3].buffer = bcBuffer2[0]; // link command to bcBuffer2
minorFrame[3].delay = 8000; // send command 1 msec after preceding command
minorFrame[3].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
}
// Command #4 : Address 5, Subaddress 1, TX, 4 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(5, 1, MXF_MIL1553_COMMAND_DIR_TX, 4, out minorFrame[4].command);
minorFrame[4].modulo = 1;
minorFrame[4].buffer = bcBuffer2[0]; // link command to bcBuffer2
minorFrame[4].delay = 8000; // send command 1 msec after preceding command
minorFrame[4].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc, 0, 0, 5, minorFrame, IntPtr.Zero);
}
// Set default data for bc buffer 0
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
txRec1553.data[4] = 0x3333;
Marshal.StructureToPtr(txRec1553, bcData0, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBuffer0[0], 1, bcData0);
}
// Set default data for bc buffer 1
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0x1234;
txRec1553.data[2] = 0x5678;
txRec1553.data[3] = 0x9ABC;
txRec1553.data[4] = 0xDEF0;
Marshal.StructureToPtr(txRec1553, bcData1, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(bcBuffer1[0], 1, bcData1);
}
// Set default data for rt5 buffer 1
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0xAAAA;
txRec1553.data[2] = 0xBBBB;
txRec1553.data[3] = 0xCCCC;
txRec1553.data[4] = 0xDDDD;
Marshal.StructureToPtr(txRec1553, rtData, true);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(rt5Buffer1[0], 1, rtData);
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting RT 4\n\r");
rc = mxfMIL1553RtSubsystemEnableSet(rt4, MXF_MIL1553_MSGTYPE_RX, 1, MXF_MIL1553_BUS_A, rt4Buffer0[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt4, MXF_MIL1553_MSGTYPE_RX, 30, MXF_MIL1553_BUS_A, rt4Buffer1[0]);
// link RT4 SA30 TX buffer to RT4 SA30 RX buffer (data wrap-around)
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt4, MXF_MIL1553_MSGTYPE_TX, 30, MXF_MIL1553_BUS_A, rt4Buffer1[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt4, VMXF_ENABLE);
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting RT 5\n\r");
rc = mxfMIL1553RtSubsystemEnableSet(rt5, MXF_MIL1553_MSGTYPE_RX, 1, MXF_MIL1553_BUS_A, rt5Buffer0[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt5, MXF_MIL1553_MSGTYPE_TX, 1, MXF_MIL1553_BUS_A, rt5Buffer1[0]);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt5, VMXF_ENABLE);
}
// attach async event handler
if (rc == MAXT_SUCCESS)
{
eventInfo.recptr.rtData = rtData;
eventInfo.recptr.bcData0 = bcData0;
eventInfo.recptr.bcData1 = bcData1;
eventInfo.bcBuffer0 = bcBuffer0[0];
eventInfo.bcBuffer1 = bcBuffer1[0];
eventInfo.rtBuffer = rt5Buffer1[0];
//allocate memory
try
{
eventInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(EVENT_INFO)));
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
Marshal.StructureToPtr(eventInfo, eventInfoPtr, true);
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventHandlerInit(server, EventHandler, eventInfoPtr, out asyncEvent); //attach async event handler
}
// set event condition on TX periodic update message threshold for RT5
if (rc == MAXT_SUCCESS)
{
condition[0].condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
condition[0].condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = 5;
condition[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull = 10;
condition[0].condition.txPeriodicUpdateMsgBufferThreshold.channel = rt5;
rc = mxfAsyncEventConditionsSet(asyncEvent, VMXF_ENABLE, 1, condition);
}
// set event condition on TX periodic update message threshold for BC
if (rc == MAXT_SUCCESS)
{
bcCondition[0].condID = MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD;
bcCondition[0].condition.txPeriodicUpdateMsgBufferThreshold.almostEmpty = 5;
bcCondition[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull = 10;
bcCondition[0].condition.txPeriodicUpdateMsgBufferThreshold.channel = bc;
rc = mxfAsyncEventConditionsSet(asyncEvent, VMXF_ENABLE, 1, bcCondition);
}
// Select BC buffers and RT buffer for async events
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, rt5, MXF_MSG_SELECT_ONLY, 1, rt5Buffer1);
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, bc, MXF_MSG_SELECT_ONLY, 1, bcBuffer0);
if (rc == MAXT_SUCCESS)
rc = mxfAsyncEventTxPeriodicUpdateMsgSelectSet(asyncEvent, bc, MXF_MSG_SELECT_ADD, 1, bcBuffer1);
// Start BM acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(acq1553Buffer[0], MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc, 0, 50 * 1000 * 1000, IntPtr.Zero); //50 msec
if (rc == MAXT_SUCCESS)
{
mxfSleep(2000);
}
// Read and display received messages
if (rc == MAXT_SUCCESS)
{
var recPtr = new IntPtr();
do
{
rc = mxfMIL1553RxAcqRead(acq1553Buffer[0], 0, rxDataSize, out rxAcqStatus, out msgCount, out byteCount, rxData);
recPtr = rxData;
for (rxRec = 0; rc == MAXT_SUCCESS && rxRec < msgCount; rxRec++)
{
rxRec1553 = (MXF_MIL1553_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_DATAREC));
rate = last > 0 ? ((rxRec1553.timeTag - last) / 1000000) : 0;//msec
last = rxRec1553.timeTag;
Console.Write("{0:d12} ", rxRec1553.timeTag);
Console.Write("{0:d3} ", rate);
Console.Write("{0:X8} ", rxRec1553.control);
Console.Write("{0:d4} ", (rxRec1553.dataSize / 2) - 1);
for (data = 0; data < Math.Min(8, rxRec1553.dataSize / 2); data++)
{
Console.Write("{0:X4} ", rxRec1553.data[data]);
}
Console.Write("\n\r");
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(100);
loop++;
}
while (loop < 20);
}
// Clear the major frame
// Stop acquisition
mxfRxAcqStop(acq1553Buffer[0]);
// Stop and Clear message queues
Console.Write("Stopping BC and RTs\n\r");
mxfMIL1553RtEnableSet(rt4, VMXF_DISABLE);
mxfMIL1553RtEnableSet(rt5, VMXF_DISABLE);
// Disable conditions and detach event handler
mxfAsyncEventConditionsSet(asyncEvent, VMXF_DISABLE, 1, condition);
mxfAsyncEventConditionsSet(asyncEvent, VMXF_DISABLE, 1, bcCondition);
mxfTxPeriodicUpdateMsgClear(bcBuffer0[0], 0);
mxfTxPeriodicUpdateMsgClear(bcBuffer1[0], 0);
mxfTxPeriodicUpdateMsgClear(rt5Buffer1[0], 0);
mxfRxAcqClear(acq1553Buffer[0]);
// Free buffers
if (bcData0 != IntPtr.Zero)
Marshal.FreeHGlobal(bcData0);
if (bcData1 != IntPtr.Zero)
Marshal.FreeHGlobal(bcData1);
if (rtData != IntPtr.Zero)
Marshal.FreeHGlobal(rtData);
if (rxData != IntPtr.Zero)
Marshal.FreeHGlobal(rxData);
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 (bcBuffer0[0] > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bcBuffer1[0] > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bcBuffer2[0] > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (rt4Buffer0[0] > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rt4Buffer0[0]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (rt4Buffer1[0] > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rt4Buffer1[0]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (rt5Buffer0[0] > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rt5Buffer0[0]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (rt5Buffer1[0] > 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(rt5Buffer1[0]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (acq1553Buffer[0] > 0)
{
rc = mxfRxAcqBufferFree(acq1553Buffer[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 eventHandler(UInt64 asyncEvent, IntPtr pParam)
{
EVENT_INFO eventInfo;
var pendingList = new MXF_ASYNCEVENT_PENDING_INFO[1];
UInt32 rc = 0;
UInt64 eventCount;
UInt64 rec;
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
IntPtr recPtr = IntPtr.Zero;
eventInfo = (EVENT_INFO)Marshal.PtrToStructure(pParam, typeof(EVENT_INFO));
// Get pending event
rc = mxfAsyncEventPendingGet(asyncEvent, 1, out eventCount, pendingList);
if (rc == MAXT_SUCCESS)
{
switch (pendingList[0].condID)
{
case MXF_ASYNCEVENT_COND_TXPERIODIC_UPDATEMSG_BUFFER_THRESHOLD:
// Set data for rt5 buffer 1
if (pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.buffer == eventInfo.rtBuffer)
{
recPtr = eventInfo.recptr.rtData;
for (rec = 0; rec < pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull; rec++)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = (UInt16)rec;
txRec1553.data[2] = (UInt16)rec;
txRec1553.data[3] = (UInt16)rec;
txRec1553.data[4] = (UInt16)rec;
Marshal.StructureToPtr(txRec1553, recPtr, false);
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.buffer, rec, eventInfo.recptr.rtData);
}
// Set data for BC buffer 0
else if (pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.buffer == eventInfo.bcBuffer0)
{
recPtr = eventInfo.recptr.bcData0;
for (rec = 0; rec < pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull; rec++)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = (UInt16)(0x1111 * rec);
txRec1553.data[2] = (UInt16)(0x1111 * rec);
txRec1553.data[3] = (UInt16)(0x1111 * rec);
txRec1553.data[4] = (UInt16)(0x1111 * rec);
Marshal.StructureToPtr(txRec1553, recPtr, false);
mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.buffer, rec, eventInfo.recptr.bcData0);
}
else if (pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.buffer == eventInfo.bcBuffer1) // Set data for BC buffer 1
{
recPtr = eventInfo.recptr.bcData1;
for (rec = 0; rec < pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.almostFull; rec++)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10; //10 bytes (command + 4 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = (UInt16)((rec % 2 > 0) ? 0xACDC : 0xCAFE);
txRec1553.data[2] = (UInt16)((rec % 2 > 0) ? 0xACDC : 0xCAFE);
txRec1553.data[3] = (UInt16)((rec % 2 > 0) ? 0xACDC : 0xCAFE);
txRec1553.data[4] = (UInt16)((rec % 2 > 0) ? 0xACDC : 0xCAFE);
Marshal.StructureToPtr(txRec1553, recPtr, false);
mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(pendingList[0].condition.txPeriodicUpdateMsgBufferThreshold.buffer, rec, eventInfo.recptr.bcData1);
}
break;
}
}
return rc;
}
}
}
Updated 03/30/2022