using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
class arinc429_rx_event_handler
{
const int BUFFER_SIZE = 4096;
const int LABEL = 5;
const int SDI = 0;
public static UInt32 RXmsgTotal;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
UInt64[] rxChannel = new UInt64[1], txChannel = new UInt64[1];
UInt64 asyncEvent = 0;
MXF_ASYNCEVENT_HANDLER asyncEventHandler = AsyncEventHandler;
UInt64 rxBuffer = 0;
UInt64[] txBuffer = new UInt64[2];
UInt64 count, indexBuffer;
IntPtr hostBuffer = IntPtr.Zero;
IntPtr rxRecBuffer = IntPtr.Zero;
#if LOCAL
#else
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
Console.Write("\nStarting\n");
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 (LOOPBACK)
if (rc == MAXT_SUCCESS)
#endif
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
if (rc == MAXT_SUCCESS)
{
try
{
rxRecBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
asyncEventInfo[0].condID = MXF_ASYNCEVENT_COND_RX_MSG;
asyncEventInfo[0].condition.rxMsg.channel = rxChannel[0];
asyncEventInfo[1].condID = MXF_ASYNCEVENT_COND_RX_ERROR;
asyncEventInfo[1].condition.rxErr.channel = rxChannel[0];
}
if (rc == MAXT_SUCCESS)
{
msgid[0].label = LABEL;
msgid[0].sdi = SDI;
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
Console.Write("Sampling started\n\r");
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
TX429PeriodicScheduling(txChannel[0], txBuffer, hostBuffer);
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
{
buffer.Clear();
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(buffer + "\n\r");
}
if (rxBuffer > 0)
for (indexBuffer = 0; indexBuffer < 2; indexBuffer++)
{
if (txBuffer[indexBuffer] > 0)
}
if (hostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(hostBuffer);
if (rxRecBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxRecBuffer);
Console.Write("\nPress enter to terminate\n");
Console.ReadKey();
return;
}
private static UInt32 AsyncEventHandler(UInt64 asyncEvent, IntPtr param)
{
UInt64 channel;
UInt64 pendingCount;
UInt64 label, sdi, status;
UInt64 i;
UInt32 rc = MAXT_SUCCESS;
UInt64 dev, mod, port;
UInt64 sampBuffer;
IntPtr recPtr = param;
for (i = 0; rc == MAXT_SUCCESS && i < pendingCount; i++)
{
switch (pendingList[i].condID)
{
case MXF_ASYNCEVENT_COND_RX_MSG:
{
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
Console.Write(
"Msg {0:00} received with label={1} sdi={2} (0x{3:x8})\n", RXmsgTotal++, label, sdi, rec.
data);
}
else
Console.Write("Error getting latest value rc = 0x%08x\n", rc);
break;
}
case MXF_ASYNCEVENT_COND_RX_ERROR:
Console.Write("Status 0x{0:x8} received on channel {1}.{2}.{3}\n", status, dev, mod, port);
break;
default:
Console.Write("Unknown condID 0x{0:x})", pendingList[i].condID);
break;
}
}
return rc;
}
private static UInt32 TX429PeriodicScheduling(UInt64 txChannel, UInt64[] txBuffer, IntPtr recPtr)
{
UInt64 schedule;
UInt64 msg = 0;
UInt64 label, sdi, data, ssm, parity;
UInt32 rc;
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
label = LABEL;
sdi = SDI;
data = 0x7fe;
parity = VMXF_A429_PARITY_ODD;
ssm = 1;
Marshal.StructureToPtr(rec429, recPtr, false);
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
rec429.timeTag = 0;
rec429.control = 0;
rec429.repeatCount = 1;
rec429.reserved = 0;
label = LABEL + 1;
sdi = SDI;
data = 0x12;
parity = VMXF_A429_PARITY_ODD;
ssm = 1;
Marshal.StructureToPtr(rec429, recPtr, true);
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Running periodic transmission, please wait...\n\r");
}
if (rc == MAXT_SUCCESS)
{
}
if (rc == MAXT_SUCCESS)
Console.Write("\n\rTransmission stopped\n\r");
return rc;
}
}
}