MX Foundation 4
mil1553_aperiodic.cs
/*****************************************************************************
//
## File:
## mil1553_aperiodic.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 simple aperiodic transmission
// to transmit commands with MIL-STD-1553 BC channel and record using BC.
// To record the messages with the BC, you need to connect RT0 to your hardware.
// Use hybrid mode if available.
//
// 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_example
{
public class mil1553_aperiodic
{
const UInt32 NUM_REC_TX = 10;
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 bcBufferTx = 0;
UInt64 bcBufferRx = 0;
IntPtr recPtr = IntPtr.Zero;
UInt32 txBufferSize;
IntPtr txBuffer = IntPtr.Zero;
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
UInt64 card, mod, port;
UInt64 cmd;
UInt64 rxAcqStatus;
UInt64 msgCount = 0;
UInt64 byteCount;
UInt64 rxRec;
UInt32 data;
UInt64 options;
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");
rc = mxfSystemInit(server);
}
// Get handle of first 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 && moduleCount > 0)
rc = MAXT_ERROR_NOT_FOUND;
// Show channel locations for BC
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);
// Use Hybrid Mode with RT0 set to 1553A if available
if (rc == MAXT_SUCCESS)
{
rc = mxfAttributeUint64Get(module[0], KMXF_MIL1553_MODULE_OPTIONS, out options);
if ((rc == MAXT_SUCCESS) && ((options & VMXF_MIL1553_MODULE_OPTIONS_HYBRID_MODE) == VMXF_MIL1553_MODULE_OPTIONS_HYBRID_MODE))
{
rc = mxfAttributeUint64Set(bc[0], KMXF_MIL1553_MILSTD1553_REV, VMXF_MIL1553_MILSTD1553_REVHYBRID);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtsHybridRevSet(bc[0], 0x00000001);
}
}
#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 NUM_REC_TX tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = NUM_REC_TX * (UInt32)Marshal.SizeOf(typeof(MXF_MIL1553_DATAREC));
// Device allocation
rc = mxfTxAperiodicBufferAlloc(bc[0], MXF_TXAPERIODIC_PRIORITY_LOW, txBufferSize, out bcBufferTx, 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(bc[0], rxBufferSize, out bcBufferRx, IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Set timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Start BC acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(bcBufferRx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Send NUM_REC_TX commands
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
for (cmd = 0; cmd < NUM_REC_TX && rc == MAXT_SUCCESS; cmd++)
{
// send two commands in alternance
if (cmd % 2 > 0)
{
txRec1553.timeTag = 0;
txRec1553.repeatCount = 1;
txRec1553.control = 0;
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
{
txRec1553.timeTag = 0;
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
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxAperiodicWrite(bcBufferTx, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, cmd, txBuffer);
}
// Read and display received messages for 5 seconds
if (rc == MAXT_SUCCESS)
{
// Wait a little for commands to be transmitted
mxfSleep(1000);
rc = mxfMIL1553RxAcqRead(bcBufferRx, 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);
}
}
// Clear aperiodic buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicClear(bcBufferTx, 0);
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(bcBufferRx);
// Clear acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(bcBufferRx);
// 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 (device[0] != 0)
// Unload MX Foundation library
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}
Updated 10/23/2023