#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_bc_extclock
{
private const UInt64 M_ADDRESS = 5;
private const UInt64 M_SUBADDR = 3;
static void Main(string[] args)
{
UInt64 server;
UInt64 device = 0;
var mil1553Module = new UInt64[1];
var discreteModule = new UInt64[1];
UInt64 bc = 0;
UInt64 discreteOut = 0;
#if LOOPBACK
UInt64 discreteIn = 0;
#endif
UInt64 acq1553Buffer = 0;
UInt64 txDiscreteBuffer = 0;
UInt64 txMsg1553Buffer = 0;
UInt64 txMsg1553Buffer2 = 0;
txRec1553.data = new UInt16[36];
UInt32 rc;
UInt64 appCount;
UInt64 moduleCount = 0;
UInt32 txBufferSize = 0;
IntPtr txBuffer = IntPtr.Zero;
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
IntPtr recPtr = IntPtr.Zero;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt64 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt32 data;
var errorString = new StringBuilder(200);
#if LOCAL
#else
rc =
mxfServerConnect(
"192.168.0.1",
"admin",
"admin", Convert.ToUInt64(
false), out server);
#endif
Console.Write("Starting ...\n\r");
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
{
if (appCount > 0)
else
}
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
#if LOOPBACK
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
#endif
if (rc == MAXT_SUCCESS)
{
txBufferSize = 64 * 1024;
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)
if (rc == MAXT_SUCCESS)
{
minorFrame[0].modulo = 1;
minorFrame[0].buffer = txMsg1553Buffer;
if (rc == MAXT_SUCCESS)
{
minorFrame[1].modulo = 1;
minorFrame[1].buffer = txMsg1553Buffer2;
}
if (rc == MAXT_SUCCESS)
{
minorFrame[2].modulo = 1;
minorFrame[2].buffer = txMsg1553Buffer2;
}
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.dataSize = 34;
txRec1553.data[0] = 0x0000;
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, true);
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
majorProperties.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_PROPERTIES_OPT_EXTCLOCK_MODE_RISING;
}
if (rc == MAXT_SUCCESS)
{
UInt32 pulseCount;
UInt64 timer = 0;
UInt64 c50ms = 50000000;
rc =
mxfTxAperiodicBufferAlloc(discreteOut, MXF_TXAPERIODIC_PRIORITY_NORMAL, txBufferSize, out txDiscreteBuffer, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
for (pulseCount = 0; rc == MAXT_SUCCESS && pulseCount < 100; pulseCount++)
{
recPtr = txBuffer;
timer += c50ms;
dioRec[0].timeTag = timer;
dioRec[0].repeatCount = 1;
dioRec[0].control = 0;
dioRec[0].edge = 0xFFFF;
dioRec[0].data = 0x0010;
dioRec[0].highDuration = 0;
dioRec[0].lowDuration = 0;
Marshal.StructureToPtr(dioRec[0], recPtr, true);
timer += c50ms;
dioRec[1].timeTag = timer;
dioRec[1].repeatCount = 1;
dioRec[1].control = 0;
dioRec[1].edge = 0xFFFF;
dioRec[1].data = 0x0000;
dioRec[1].highDuration = 0;
dioRec[1].lowDuration = 0;
Marshal.StructureToPtr(dioRec[1], recPtr, true);
if (rc == MAXT_SUCCESS)
if (rc != MAXT_SUCCESS)
Console.Write("mxfDiscreteTxAperiodicWrite() error; rc=0x{0:x8} (count={1})\n\r", rc, pulseCount);
}
}
}
if (rc == MAXT_SUCCESS)
{
do
{
rc =
mxfMIL1553RxAcqRead(acq1553Buffer, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("mxfMIL1553RxAcqRead() error; rc=0x{0:x8}\n\r", rc);
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) && (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;
}
}
}
}
loop++;
} while (loop < 25);
}
Console.Write("Stopping\n\r");
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
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");
}
if (rc == MAXT_SUCCESS)
{
if (appCount > 1)
else
{
if (txMsg1553Buffer > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (txMsg1553Buffer2 > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (acq1553Buffer > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (txDiscreteBuffer > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
}
Console.Write("\nPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}