#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace discrete_example
{
public struct EVENT_INFO
{
public IntPtr rxHostBuffer;
public IntPtr txHostBuffer;
public UInt64 bufferTx;
public UInt64 moduleType;
}
class discrete_fifo
{
static void Main(string[] args)
{
UInt32 rc;
UInt64 channelCount = 0;
UInt64 server, device;
UInt64 module = 0;
var rx = new UInt64[1];
var tx = new UInt64[1];
UInt64 bufferTx = 0;
UInt64 bufferRx = 0;
UInt64 asyncEvent = 0;
MXF_ASYNCEVENT_HANDLER eventHandler = EventHandler;
UInt32 txBufferSize;
IntPtr txBuffer = IntPtr.Zero;
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
EVENT_INFO eventInfo = new EVENT_INFO();
StringBuilder errorString = new StringBuilder();
UInt64 moduleType = 0;
#if LOCAL
#else
#endif
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n");
}
if (rc == MAXT_SUCCESS)
rc =
mxfChannelAllGet(server, MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ALL, 1, out channelCount, rx);
if (rc == MAXT_SUCCESS && channelCount > 0)
rc =
mxfChannelAllGet(server, MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ALL, 1, out channelCount, tx);
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
if (moduleType != MXF_MODULE_DIOFIFO_EH)
{
}
}
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)
{
IntPtr eventInfoPtr = IntPtr.Zero;
try
{
eventInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(EVENT_INFO)));
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
eventInfo.rxHostBuffer = rxBuffer;
eventInfo.txHostBuffer = txBuffer;
eventInfo.bufferTx = bufferTx;
eventInfo.moduleType = moduleType;
Marshal.StructureToPtr(eventInfo, eventInfoPtr, false);
}
if (rc == MAXT_SUCCESS)
{
condition[0].reserved = 0;
condition[0].condID = MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD;
condition[0].condition.txAperiodicBufferThreshold.almostEmpty = 0;
condition[0].condition.txAperiodicBufferThreshold.almostFull = 1;
condition[0].condition.txAperiodicBufferThreshold.buffer = bufferRx;
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
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)
{
Console.Write(" {0} ERROR # 0x{1:X8}", errorString, rc);
Console.Write("{0}\n\r", errorString);
}
if (bufferRx > 0)
if (bufferTx > 0)
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
private static UInt32 EventHandler(UInt64 hAsyncEvent, IntPtr pParam)
{
var eventInfo = new EVENT_INFO();
UInt32 rc;
UInt64 count = 0;
IntPtr txRecPtr = IntPtr.Zero;
IntPtr rxRecPtr = IntPtr.Zero;
UInt64 status, msgCount, byteCount;
UInt64 channel;
UInt64 device = 0;
if (rc == MAXT_SUCCESS && count > 0)
{
if (pendingInfo[0].condID == MXF_ASYNCEVENT_COND_RXACQ_BUFFER_THRESHOLD)
{
eventInfo = (EVENT_INFO)Marshal.PtrToStructure(pParam, typeof(EVENT_INFO));
rxRecPtr = eventInfo.rxHostBuffer;
txRecPtr = eventInfo.txHostBuffer;
rc =
mxfDiscreteRxAcqRead(pendingInfo[0].condition.rxAcqBufferThreshold.buffer, 1, (UInt64)Marshal.SizeOf(typeof(
MXF_DISCRETE_DATAREC)), out status, out msgCount, out byteCount, eventInfo.rxHostBuffer);
if (rc == MAXT_SUCCESS && msgCount > 0)
{
txRec.repeatCount = 1;
txRec.data = rxRec.data;
if (eventInfo.moduleType == MXF_MODULE_DIOFIFO_EH)
{
txRec.edge = rxRec.edge > 0 ? rxRec.edge : 0xffff;
}
else
{
txRec.edge = 0;
}
Marshal.StructureToPtr(txRec, txRecPtr, true);
if (rc == MAXT_SUCCESS)
{
if (eventInfo.moduleType == MXF_MODULE_DIOFIFO_EH)
{
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
Console.Write("Set led #0 state to {0}\n", (rxRec.data & 0x01) > 0 ? "OFF" : "ON");
rc =
mxfDeviceLedSet(device, 0x01, (UInt64)((rxRec.data & 0x01) > 0 ? 0 : 1));
}
}
}
}
}
}
if (rc != MAXT_SUCCESS)
Console.Write("event handler rc=0x%08x\n", rc);
return rc;
}
}
}