#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_cond_branch_1
{
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 rt = new UInt64[1];
var bcBufferTx = new UInt64[4];
var bcBufferRx = new UInt64();
var rtBuffer = new UInt64[4];
{
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 }
};
{
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = 0, subAddress = 3, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = 0, subAddress = 3, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_RX, address = 0, subAddress = 5, reserved = 0 },
new MXF_MSGID_MIL1553{ type = MXF_MIL1553_MSGTYPE_TX, address = 0, subAddress = 5, reserved = 0 }
};
IntPtr recPtr = IntPtr.Zero;
UInt32 txBufferSize = 1024;
IntPtr txBuffer = IntPtr.Zero;
for (int i = 0; i < 3; i++)
txRec1553[i].data = new UInt16[36];
UInt32 rxBufferSize = 10 * 1024;
IntPtr rxBuffer = IntPtr.Zero;
UInt64 bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
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 (LOOPBACK)
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)
}
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)
{
err.errors = new UInt16[34];
err.errors[33] = (RT_ADRS << 11) + 1;
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
mfProp.modulo = 1;
mfProp.repeatCount = 1;
mfProp.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_GOTO;
mfProp.branchIndex = 0;
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 = 2;
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;
minorFrame[1].branchMinorIndex = 1;
minorFrame[1].branchMsgIndex = 0xFFFF;
minorFrame[1].condMask = 0xFFFF;
minorFrame[1].condData = (UInt16)(bcMsgID[BUF_SA3_TX].address << 11);
}
if (rc == MAXT_SUCCESS)
}
}
if (rc == MAXT_SUCCESS)
{
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].modulo = 1;
minorFrame[1].buffer = bcBufferTx[BUF_SA5_TX];
minorFrame[1].delay = 8000;
minorFrame[1].branchMinorIndex = 0;
minorFrame[1].branchMsgIndex = 1;
minorFrame[1].condMask = 0xFFFF;
minorFrame[1].condData = (UInt16)(bcMsgID[BUF_SA3_TX].address << 11);
}
}
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553[0].data = new UInt16[36];
txRec1553[0].repeatCount = 2;
txRec1553[0].dataSize = 6;
txRec1553[0].data[0] = 0x0000;
txRec1553[0].data[1] = 0x0000;
txRec1553[0].data[2] = 0x1111;
Marshal.StructureToPtr(txRec1553[0], txBuffer, true);
}
txRec1553[0].data = new UInt16[36];
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553[0].repeatCount = 1;
txRec1553[0].dataSize = 8;
txRec1553[0].data[0] = 0x0000;
txRec1553[0].data[1] = 0xFFFF;
txRec1553[0].data[2] = 0xEEEE;
txRec1553[0].data[3] = 0xDDDD;
Marshal.StructureToPtr(txRec1553[0], recPtr, true);
}
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553[0].repeatCount = 5;
txRec1553[0].dataSize = 6;
txRec1553[0].data[0] = 0x0000;
txRec1553[0].data[1] = 0x1234;
txRec1553[0].data[2] = 0x5678;
Marshal.StructureToPtr(txRec1553[0], recPtr, true);
if (rc == MAXT_SUCCESS)
{
txRec1553[1].dataSize = 6;
txRec1553[1].repeatCount = 5;
txRec1553[1].control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553[1].service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553[1].data[0] = 0x0000;
txRec1553[1].data[1] = 0x9ABC;
txRec1553[1].data[2] = 0xDEF0;
}
Marshal.StructureToPtr(txRec1553[1], recPtr, true);
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
txRec1553[2].dataSize = 6;
txRec1553[2].repeatCount = 1;
txRec1553[2].control = 0;
txRec1553[2].data[0] = 0x0000;
txRec1553[2].data[1] = 0x1234;
txRec1553[2].data[2] = 0x5678;
}
Marshal.StructureToPtr(txRec1553[2], recPtr, true);
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
recPtr = txBuffer;
txRec1553[0].data = new UInt16[36];
txRec1553[0].repeatCount = 1;
txRec1553[0].dataSize = 8;
txRec1553[0].data[0] = 0x0000;
txRec1553[0].data[1] = 0x4444;
txRec1553[0].data[2] = 0x5555;
txRec1553[0].data[3] = 0x6666;
Marshal.StructureToPtr(txRec1553[0], recPtr, true);
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting BC and RT\n\r");
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
do
{
rc =
mxfMIL1553RxAcqRead(bcBufferRx, 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\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 > 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++;
} while (loop < 10);
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
Console.Write("Stopping BC and RT\n\r");
for (msg = 0; msg < 4 && rc == MAXT_SUCCESS; msg++)
{
if (rc == MAXT_SUCCESS)
}
}
Console.Write("Terminating\n");
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");
}
}
for (indexBuffer = 0; indexBuffer < 4 && rtBuffer[indexBuffer] > 0; indexBuffer++)
{
if (rtBuffer[indexBuffer] != 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
if (bcBufferRx > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}