#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_cond_branch_2
{
const UInt64 BUF_SA3_RX = 0;
const UInt64 BUF_SA3_TX = 1;
const UInt64 BUF_SA5_RX = 2;
const UInt64 BUF_SA5_TX = 3;
const UInt16 RT_ADRS = 5;
static void Main(string[] args)
{
UInt32 rc;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 server = 0;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
var bm = new UInt64[1];
var discIn = new UInt64[1];
var discOut = new UInt64[1];
var bcBufferTx = new UInt64[4];
var bcBufferNoOp = new UInt64();
var bmBufferRx = new UInt64();
{
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = RT_ADRS, subAddress = 3, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = RT_ADRS, subAddress = 3, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = RT_ADRS, subAddress = 5, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = RT_ADRS, subAddress = 5, reserved = 0 }
};
UInt32 txBufferSize = 1024;
IntPtr txBuffer = IntPtr.Zero;
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 10 * 1024;
IntPtr rxBuffer = IntPtr.Zero;
UInt64 rxAcqStatus = 0;
UInt64 msgCount = 0;
UInt64 byteCount = 0;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 address, subAddress, dir, wordCount, indexBuffer;
UInt32 data;
UInt32 msg;
var errorString = new StringBuilder(200);
#if (LOCAL)
#else
rc =
mxfServerConnect(
"192.168.0.1",
"admin",
"admin", Convert.ToUInt64(
false), out server);
#endif
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n\r");
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS && deviceCount > 0)
if (rc == MAXT_SUCCESS && moduleCount > 0)
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc =
mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, discIn);
if (rc == MAXT_SUCCESS && channelCount > 0)
rc =
mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, discOut);
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
#endif
if (rc == MAXT_SUCCESS)
{
txBufferSize = 1024;
for (msg = 0; msg < 4 && rc == MAXT_SUCCESS; msg++)
{
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
try
{
txBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 10 * 1024;
if (rc == MAXT_SUCCESS)
{
try
{
rxBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
minorFrame[0].buffer = bcBufferNoOp;
minorFrame[0].modulo = 1;
minorFrame[0].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_NOOP_AND_WAITONCOND | MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_NEXTFRAME_NOW;
minorFrame[0].condMask = 0x0001;
minorFrame[0].condData = 0x0001;
minorFrame[0].branchMinorIndex = 1;
minorFrame[0].branchMsgIndex = 0xFFFF;
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
rc =
mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_RX].address, bcMsgID[BUF_SA3_RX].subAddress, bcMsgID[BUF_SA3_RX].type, 2, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA3_RX];
if (rc == MAXT_SUCCESS)
{
rc =
mxfMIL1553CommandCompose(bcMsgID[BUF_SA3_TX].address, bcMsgID[BUF_SA3_TX].subAddress, bcMsgID[BUF_SA3_TX].type, 2, out minorFrame[1].command);
if (rc == MAXT_SUCCESS)
{
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_DELAY;
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[BUF_SA3_TX];
minorFrame[1].delay = 8000;
}
}
}
if (rc == MAXT_SUCCESS)
{
mfProp.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_GOTO;
mfProp.modulo = 1;
mfProp.repeatCount = 1;
mfProp.branchIndex = 1;
rc =
mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_RX].address, bcMsgID[BUF_SA5_RX].subAddress, bcMsgID[BUF_SA5_RX].type, 3, out minorFrame[0].command);
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[BUF_SA5_RX];
if (rc == MAXT_SUCCESS)
{
rc =
mxfMIL1553CommandCompose(bcMsgID[BUF_SA5_TX].address, bcMsgID[BUF_SA5_TX].subAddress, bcMsgID[BUF_SA5_TX].type, 3, out minorFrame[1].command);
if (rc == MAXT_SUCCESS)
{
minorFrame[1].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_GAP;
minorFrame[1].buffer = bcBufferTx[BUF_SA5_TX];
minorFrame[1].modulo = 1;
minorFrame[1].delay = 8000;
}
}
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 10;
txRec1553.dataSize = 6;
txRec1553.data[0] = 0x0000;
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 8;
txRec1553.data[0] = 0x0000;
txRec1553.data[1] = 0xFFFF;
txRec1553.data[2] = 0xEEEE;
txRec1553.data[3] = 0xDDDD;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
IntPtr recPtr = IntPtr.Zero;
do
{
rc =
mxfMIL1553RxAcqRead(bmBufferRx, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
recPtr = rxBuffer;
for (rxRec = 0; (rxRec < msgCount) && (rc == MAXT_SUCCESS); rxRec++)
{
if (rc == MAXT_SUCCESS)
{
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\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");
if (msgInfo[0].statusIndex[0] != 0xffff)
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]);
if (msgInfo[0].statusIndex[0] != 0xffff)
{
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 > 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_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)
{
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;
}
}
}
}
loop++;
if (rc == MAXT_SUCCESS && loop == 1)
{
}
} while (loop < 10);
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
Console.Write("Terminating\n\r");
if (txBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txBuffer);
if (rxBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxBuffer);
if (rc != MAXT_SUCCESS)
{
{
errorString.Clear();
errorString.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(errorString + "\n\r");
}
for (indexBuffer = 0; indexBuffer < 4 && bcBufferTx[indexBuffer] > 0; indexBuffer++)
{
if (bcBufferTx[indexBuffer] != 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
if (bcBufferNoOp > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bmBufferRx > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}