MX Foundation 4
mil1553_rt.cs
/*****************************************************************************
//
## File:
## mil1553_rt.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 basic usage of MIL-STD-1553 remote terminal.
// Can be run with mil1553_bc and mil1553_bm at the same time.
//
// Hardware requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*****************************************************************************/
#define LOCAL
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_rt
{
const UInt32 CMD_TX = 0;
const UInt32 CMD_RX = 1;
const UInt32 CMD_MC = 2;
static void Main(string[] args)
{
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var rt = new UInt64();
var tx = new UInt64[3]; //buffer
var rx = new UInt64(); //buffer
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt32 txBufferSize = 1024;
IntPtr txBuffer = IntPtr.Zero;
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 64 * 4096;
IntPtr rxBuffer = IntPtr.Zero;
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
UInt64 bus = 0;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 wordCount;
UInt64 card, mod, port, indexBuffer;
UInt32 data;
var msgInfo = new MXF_MIL1553_MSGINFO[1];
var errorString = new StringBuilder(200);
UInt32 rc;
// Connect to MX Foundation library
#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)
{
rc = mxfSystemInit(server);
if (rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
{
// if another MX Foundation application is running, only load MX-Foundation library, otherwise reset cards first
rc = mxfSystemResourcesInit(server, 0);
}
}
// Get handle of first MIL-STD-1553 RT5 channel
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, out deviceCount, device);
if (rc == MAXT_SUCCESS && deviceCount > 0)
{
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 = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelGet(module[0], 7, out rt);
if (rc == MAXT_SUCCESS)
{
// Show channel location for RT
rc = mxfChannelLocationGet(rt, out card, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("RT location is Device:{0} Module:{1} Port:{2}\n", card, mod, port);
}
// Allocate 1KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 1024;
// RT 5: address=0, subaddress 3 TX
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, CMD_TX, txBufferSize, out tx[CMD_TX], IntPtr.Zero);
// RT 5: address=0, subaddress 3 RX
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, CMD_RX, txBufferSize, out tx[CMD_RX], IntPtr.Zero);
// RT 5: Mode Command 16
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(rt, CMD_MC, txBufferSize, out tx[CMD_MC], IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
txBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate buffer for Rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 64 * 4096;
// Device allocation
rc = mxfRxAcqBufferAlloc(rt, rxBufferSize, out rx, 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);
// Enable the BUS A and B of RT 5 subaddress 3
if (rc == MAXT_SUCCESS)
{
bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
// TX
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_TX, 3, bus, tx[CMD_TX]);
// RX
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_RX, 3, bus, tx[CMD_RX]);
}
// Enable the BUS A and B of RT 5 mode command 16
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_MODECODE, 16, bus, tx[CMD_MC]);
// Set default data for address 5, subaddress 3
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 34; //34 bytes (command + 16 words)
txRec1553.data[0] = 0x0000; //Not used
txRec1553.data[1] = 0xFFFF;
txRec1553.data[2] = 0xEEEE;
txRec1553.data[3] = 0xDDDD;
txRec1553.data[4] = 0xCCCC;
txRec1553.data[5] = 0xBBBB;
txRec1553.data[6] = 0xAAAA;
txRec1553.data[7] = 0x9999;
txRec1553.data[8] = 0x8888;
txRec1553.data[9] = 0x7777;
txRec1553.data[10] = 0x6666;
txRec1553.data[11] = 0x5555;
txRec1553.data[12] = 0x4444;
txRec1553.data[13] = 0x3333;
txRec1553.data[14] = 0x2222;
txRec1553.data[15] = 0x1111;
txRec1553.data[16] = 0x0000;
Marshal.StructureToPtr(txRec1553, txBuffer, false);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(tx[CMD_TX], 1, txBuffer);
}
// Start messages queues
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting RT\n\r");
rc = mxfMIL1553RtEnableSet(rt, VMXF_ENABLE);
}
// Start RT acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Read and display received messages
if (rc == MAXT_SUCCESS)
{
UInt64 addr, subAddr, dir;
IntPtr recPtr = IntPtr.Zero;
do
{
rc = mxfMIL1553RxAcqRead(rx, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
recPtr = rxBuffer;
for (rxRec = 0; rxRec < msgCount && rc == MAXT_SUCCESS; rxRec++)
{
rxRec1553[0] = (MXF_MIL1553_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_DATAREC));
rc = mxfMIL1553DataRecordDecompose(rt, 1, rxRec1553, msgInfo);
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandDecompose(rxRec1553[0].data[0], out addr, out subAddr, out dir, out wordCount);
if (rc == MAXT_SUCCESS)
{
Console.Write("\n\r{0}:\t", rxRec1553[0].timeTag);
switch (msgInfo[0].msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_BCRT:
Console.Write("BC to RT{0}SA{1}WC{2}(0x{3:x4})\n\r", addr, subAddr, wordCount, rxRec1553[0].data[0]);
Console.Write("\t\tBC data:");
for (data = 0; data < msgInfo[0].dataWordCount; data++)
{
if (data > 0 && (data % 4) == 0)
Console.Write("\n\r\t\t\t");
Console.Write(" 0x{0:x4}", rxRec1553[0].data[msgInfo[0].dataIndex + data]);
}
Console.Write("\n\r");
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
Console.Write("RT{0}SA{1}WC{2}to BC (0x{3:x4})\n\r", addr, subAddr, wordCount, rxRec1553[0].data[0]);
break;
case MXF_MIL1553_MSGINFO_TYPE_MODECODE_TXDATA:
Console.Write("BC Mode Command {0}to RT{1}SA{2}(0x{3:x4})\n\r", wordCount, addr, subAddr, rxRec1553[0].data[0]);
break;
}
}
}
// Get next msg
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
} while (loop < 200 && rc == MAXT_SUCCESS);
}
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rx);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqClear(rx);
// Stop RT
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtEnableSet(rt, VMXF_DISABLE);
Console.Write("\nStopping RT\n\r");
// Disable the BUS A and B of RT 5 subaddress 3
if (rc == MAXT_SUCCESS)
{
bus = 0;
// TX
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_TX, 3, bus, tx[CMD_TX]);
// RX
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_RX, 3, bus, tx[CMD_RX]);
}
// Disable the BUS A and B of RT 5 mode command 16
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RtSubsystemEnableSet(rt, MXF_MIL1553_MSGTYPE_MODECODE, 16, bus, tx[CMD_MC]);
// 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
for (indexBuffer = 0; indexBuffer < 3; indexBuffer++)
{
if (tx[indexBuffer] != 0)
{
rc = mxfTxPeriodicUpdateMsgBufferFree(tx[indexBuffer]);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
if (rc != MAXT_SUCCESS)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
// Unload MX Foundation library
rc = mxfSystemTerminate(server);
if (rc == MAXT_ERROR_ANOTHER_PROCESS_RUNNING)
{
// if other MX Foundation application running, only unload MX-Foundation library, otherwise reset cards first
}
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}
Updated 10/23/2023