#define LOCAL
#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_aperiodic_event
{
const UInt64 FRAME_RATE = 20000000;
const UInt32 ALMOST_FULL = 20;
const UInt32 ALMOST_EMPTY = 10;
private static UInt64 startTime = 0;
static void Main(string[] args)
{
UInt32 rc;
UInt64 deviceCount;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
UInt64 bm = 0;
var bcBufferTx = new UInt64[1];
var bmBufferRx = new UInt64[1];
UInt64 asyncEvent = 0;
MXF_ASYNCEVENT_HANDLER txHandler = TxHandler;
IntPtr recPtr = IntPtr.Zero;
UInt32 txBufferSize;
IntPtr txBuffer = IntPtr.Zero;
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
UInt64 card, mod, port;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount;
UInt64 rxRec;
UInt32 loop = 0;
UInt32 data;
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)
{
if (rc == MAXT_SUCCESS && moduleCount == 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)
Console.Write("BM location is Device:{0} Module:{1} Port:{2}\n", card, mod, port);
if (rc == MAXT_SUCCESS)
Console.Write("BC location is Device:{0} Module:{1} Port:{2}\n", card, mod, port);
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
#endif
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)
startTime += 100000000;
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
condition[0].reserved = 0;
condition[0].condID = MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD;
condition[0].condition.txAperiodicBufferThreshold.almostEmpty = ALMOST_EMPTY;
condition[0].condition.txAperiodicBufferThreshold.almostFull = ALMOST_FULL;
condition[0].condition.txAperiodicBufferThreshold.buffer = bcBufferTx[0];
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
do
{
rc =
mxfMIL1553RxAcqRead(bmBufferRx[0], 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
recPtr = rxBuffer;
for (rxRec = 0; rxRec < msgCount && rc == MAXT_SUCCESS; rxRec++)
{
Console.Write("\n\r{0}:\t", rxRec1553.timeTag);
Console.Write("Control: 0x{0:x8}", rxRec1553.control);
for (data = 0; data < rxRec1553.dataSize / 2; data++)
{
if (data > 0 && data % 5 == 0)
Console.Write("\n\r\t\t\t");
Console.Write(" 0x{0:x4}", rxRec1553.data[data]);
}
Console.Write("\n\r");
}
loop++;
} while (loop < 10);
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
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 (bcBufferTx[0] > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bmBufferRx[0] > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
private static UInt32 TxHandler(UInt64 asyncEvent, IntPtr pParam)
{
UInt32 rc = 0;
UInt64 count = 0;
txRec1553.data = new UInt16[36];
IntPtr recPtr = pParam;
if (rc == MAXT_SUCCESS && count > 0)
{
if (pendingInfo[0].condID == MXF_ASYNCEVENT_COND_TXAPERIODIC_BUFFER_THRESHOLD)
{
UInt64 cmd = 0;
for (cmd = 0; cmd < pendingInfo[0].condition.txAperiodicBufferThreshold.almostFull - 1; cmd++)
{
if (cmd % 2 > 0)
{
txRec1553.timeTag = startTime + 1000000;
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10;
txRec1553.data[1] = 0x0000;
txRec1553.data[2] = 0x1111;
txRec1553.data[3] = 0x2222;
txRec1553.data[4] = 0x3333;
}
else
{
startTime += FRAME_RATE;
txRec1553.timeTag = startTime;
txRec1553.repeatCount = 1;
txRec1553.dataSize = 10;
txRec1553.data[1] = 0x3333;
txRec1553.data[2] = 0x2222;
txRec1553.data[3] = 0x1111;
txRec1553.data[4] = 0x0000;
}
Marshal.StructureToPtr(txRec1553, recPtr, false);
}
rc =
mxfMIL1553TxAperiodicWrite(pendingInfo[0].condition.txAperiodicBufferThreshold.buffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, cmd, pParam);
}
}
return rc;
}
}
}