MX Foundation 4
mil1553_bc.cs
/*****************************************************************************
//
## File:
## mil1553_bc.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 a MIL-STD-1553 bus controller.
// It can be run with mil1553_rt 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_exemple
{
public class mil1553_bc
{
static void Main(string[] args)
{
const UInt32 M_ADDRESS = 5;
const UInt32 M_SUBADDRESS = 3;
UInt32 rc;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
UInt64 tx = 0;
UInt64 tx2 = 0;
UInt64 rx = 0;
UInt32 txBufferSize;
IntPtr txBuffer = IntPtr.Zero;
var txRec1553 = new MXF_MIL1553_DATAREC();
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 20 * 1024;
IntPtr rxBuffer = IntPtr.Zero;
var rxRec1553 = new MXF_MIL1553_DATAREC[1];
var minorFrame = new MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG[3];
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt64 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt64 card, mod, port;
UInt32 data;
var msgInfo = new MXF_MIL1553_MSGINFO[1];
var errorString = new StringBuilder(200);
#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 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 not found, return an error
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Display BC channel location
if (rc == MAXT_SUCCESS)
rc = mxfChannelLocationGet(bc[0], out card, out mod, out port);
if (rc == MAXT_SUCCESS)
System.Console.Write("BC location is Device:{0} Module:{1} Port:{2}\n", card, mod, port);
// Allocate 1KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 1024;
// Device allocation
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc[0], 0, txBufferSize, out tx, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(bc[0], 1, 0, out tx2, IntPtr.Zero);
// Host allocation
if (rc == MAXT_SUCCESS)
{
try
{
txBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocate 20KB buffer for rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 20 * 1024;
// Device allocation
rc = mxfRxAcqBufferAlloc(bc[0], 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);
// Clear the major frame
if (rc == MAXT_SUCCESS)
// Set the minor frame #0 using 3 Commands
if (rc == MAXT_SUCCESS)
{
// Command #0 : Address 5, Subaddress 3, RX, 16 words
rc = mxfMIL1553CommandCompose(M_ADDRESS, M_SUBADDRESS, MXF_MIL1553_COMMAND_DIR_RX, 16, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = tx;
// Command #1 : Address 5, Subaddress 3, TX, 16 words
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(M_ADDRESS, M_SUBADDRESS, MXF_MIL1553_COMMAND_DIR_TX, 16, out minorFrame[1].command);
minorFrame[1].modulo = 1;
minorFrame[1].buffer = tx2;
}
// Command #2 : Address 5, Mode Command 16, TX
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandCompose(M_ADDRESS, 0, MXF_MIL1553_COMMAND_DIR_TX, 16, out minorFrame[2].command);
minorFrame[2].modulo = 1;
minorFrame[2].buffer = tx2;
}
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameSet(bc[0], 0, 0, 3, minorFrame, IntPtr.Zero);
}
// 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] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
txRec1553.data[4] = 0x3333;
txRec1553.data[5] = 0x4444;
txRec1553.data[6] = 0x5555;
txRec1553.data[7] = 0x6666;
txRec1553.data[8] = 0x7777;
txRec1553.data[9] = 0x8888;
txRec1553.data[10] = 0x9999;
txRec1553.data[11] = 0xAAAA;
txRec1553.data[12] = 0xBBBB;
txRec1553.data[13] = 0xCCCC;
txRec1553.data[14] = 0xDDDD;
txRec1553.data[15] = 0xEEEE;
txRec1553.data[16] = 0xFFFF;
Marshal.StructureToPtr(txRec1553, txBuffer, false);
rc = mxfMIL1553TxPeriodicUpdateMsgWrite(tx, 1, txBuffer);
}
// Start BC acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rx, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
// Start the major frame with 250 msec rate
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553TxPeriodicMajorFrameStart(bc[0], 0, 250000000, IntPtr.Zero);
// Read and display received messages
if (rc == MAXT_SUCCESS)
{
IntPtr recPtr = IntPtr.Zero;
do
{
rc = mxfMIL1553RxAcqRead(rx, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
if (rc != MAXT_SUCCESS)
{
Console.Write("mxfMIL1553RxAcqRead() failed; rc=0x{0:x8}\n\r", rc);
break;
}
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(bc[0], 1, rxRec1553, msgInfo);
if (rc == MAXT_SUCCESS)
{
rc = mxfMIL1553CommandDecompose(rxRec1553[0].data[0], out address, out subAddress, 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", address, subAddress, wordCount, rxRec1553[0].data[0]);
Console.Write("\t\tRT status: 0x{0:x4} \n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
Console.Write("RT{0} SA{1} WC{2} to BC (0x{3:x4})\n\r", address, subAddress, wordCount, rxRec1553[0].data[0]);
Console.Write("\t\tRT status: 0x{0:x4} \n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
Console.Write("\t\tRT data:");
for (data = 0; data < msgInfo[0].dataWordCount; data++)
{
if (data > 1 && (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_MODECODE_TXDATA:
Console.Write("BC Mode Command {0} to RT{1} SA{2} (0x{3:x4})\n\r", wordCount, address, subAddress, rxRec1553[0].data[0]);
if ((msgInfo[0].statusIndex[0] != 0xffff) && (msgInfo[0].dataIndex != 0xffff))
{
Console.Write("\t\tRT status: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].statusIndex[0]]);
Console.Write("\t\tRT data: 0x{0:x4}\n\r", rxRec1553[0].data[msgInfo[0].dataIndex]);
}
break;
}
}
}
// Get next msg
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
mxfSleep(500);
loop++;
} while (loop < 300);
}
// Stop the major frame
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicMajorFrameStop(bc[0], 0, 0);
if (rc == MAXT_SUCCESS)
// Stop acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rx);
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 buffers
if (txBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txBuffer);
if (rxBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxBuffer);
// Free all buffers and terminate
if (tx > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (tx2 > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (rx > 0)
{
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)
{
}
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}
Updated 03/30/2022