#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_embedded_async_to_1553
{
private const UInt64 MAX_TX_RECORDS_TO_TRANSMIT = 8;
private const UInt32 EX_USER_COMMAND_ID_START = 0x80000001;
private const UInt32 EX_USER_COMMAND_ID_STOP = 0x80000002;
private const UInt32 EX_USER_COMMAND_ID_READ = 0x80000003;
private const UInt32 EX_USER_COMMAND_ID_TERMINATE = 0x80000005;
private const UInt32 EX_USER_COMMAND_ACK = 0xA5A5A5A5;
private const UInt64 RT_ADDRESS = 1;
private const UInt64 RT_SUBADDRESS = 2;
public struct TEST_INFO
{
public UInt64 server;
public UInt64 device;
public UInt64 moduleAsync;
public UInt64 module1553;
public UInt64 rxChannel;
public UInt64 txChannel;
public UInt64 bcChannel;
public UInt64 bmChannel;
public UInt64 commTxBuffer;
public UInt64 commRxBuffer;
}
public struct EX_COMMAND
{
public UInt32 command;
public UInt32 paramNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public UInt32[] param;
}
public struct EVENT_INFO
{
public IntPtr rxHostBuffer;
public IntPtr bcHostBuffer;
public UInt64 rxBufferSize;
public UInt64 bcBuffer;
}
static void Main(string[] args)
{
TEST_INFO info = new TEST_INFO();
UInt32 rc;
UInt64 moduleCount = 0;
var tempAr = new UInt64[10];
UInt64 channelCount = 0;
rc =
mxfServerConnect(
"0.0.0.0",
"",
"", Convert.ToUInt64(
false), out info.server);
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n\r");
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
info.moduleAsync = tempAr[0];
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc =
mxfModuleChannelAllGet(info.moduleAsync, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, tempAr);
info.rxChannel = tempAr[0];
if (rc == MAXT_SUCCESS && channelCount > 0)
rc =
mxfModuleChannelAllGet(info.moduleAsync, MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, tempAr);
info.txChannel = tempAr[0];
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
info.module1553 = tempAr[0];
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc =
mxfModuleChannelAllGet(info.module1553, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, out channelCount, tempAr);
info.bcChannel = tempAr[0];
if (rc == MAXT_SUCCESS && channelCount > 0)
rc =
mxfModuleChannelAllGet(info.module1553, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, 1, out channelCount, tempAr);
info.bmChannel = tempAr[0];
if (rc == MAXT_SUCCESS && channelCount > 0)
rc = MAXT_ERROR_NOT_FOUND;
rc = testHostPart(info);
if (rc != MAXT_SUCCESS)
{
StringBuilder errorString = new StringBuilder(200);
{
errorString.Clear();
errorString.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(errorString + "\n\r");
}
Console.Write("Terminating ...\n\r");
Console.Write("Press enter to terminate\n\r");
Console.ReadKey();
return;
}
public static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
if (attrib == KMXF_DEVICE_COMM_QUEUE_NUM)
{
value = 2;
return 1;
}
return 0;
}
public static UInt32 commandSend(UInt64 txBuffer, UInt64 rxBuffer, EX_COMMAND cmd)
{
msg.
data =
new UInt32[256];
IntPtr msgPtr = IntPtr.Zero;
UInt64 readCnt;
UInt64 byteCnt;
UInt32 rc = 0;
try
{
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
msg.
data[0] = cmd.command;
msg.
data[1] = cmd.paramNum;
for (int i = 0; i < cmd.paramNum; i++)
{
msg.
data[i] = cmd.param[i];
}
Marshal.StructureToPtr(msg, msgPtr, true);
if (rc == MAXT_SUCCESS && rxBuffer > 0)
{
msg.
data =
new UInt32[256];
while (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS && readCnt > 0)
{
if (msg.
data[0] == EX_USER_COMMAND_ACK)
break;
}
}
}
Marshal.FreeHGlobal(msgPtr);
return rc;
}
public static UInt32 testHostPart(TEST_INFO info)
{
EX_COMMAND cmd = new EX_COMMAND();
cmd.param = new UInt32[32];
recAsync.
data =
new byte[256];
IntPtr recPtr;
UInt64 txBuffer = 0;
UInt32 rc;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 txBufferSize = 0;
UInt64 msgCount = 0;
UInt64 byteCount = 0;
UInt64 data;
UInt64 ibyte;
int word;
IntPtr rxHostBuffer = IntPtr.Zero;
UInt64 rxBufferSize = 0;
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
#if LOOPBACK
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
#endif
if (rc == MAXT_SUCCESS)
{
txBufferSize = 10 * 1024;
if (rc == MAXT_SUCCESS)
{
txBufferSize = 10 * 1024;
try
{
txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch
{
rc = MAXT_ERROR_MEM;
}
}
}
if (rc == MAXT_SUCCESS)
{
txBufferSize = 10 * 1024;
}
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 10 * 1024;
if (rc == MAXT_SUCCESS)
{
try
{
rxHostBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch
{
rc = MAXT_ERROR_MEM;
}
}
}
if (rc == MAXT_SUCCESS)
{
UInt64 startTime = 500 * 1000 * 1000;
UInt64 delay = 100 * 1000 * 1000;
byte value = 0;
recPtr = txHostBuffer;
for (data = 0; rc == MAXT_SUCCESS && data < MAX_TX_RECORDS_TO_TRANSMIT; data++, value++)
{
recAsync.
timeTag = startTime + (delay * data);
for (ibyte = 0; ibyte < recAsync.
dataSize; ibyte++)
recAsync.
data[ibyte] = value;
Marshal.StructureToPtr(recAsync, recPtr, true);
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Transmitting simulation data...\n\r");
rc =
mxfASYNCEHTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
}
if (rc == MAXT_SUCCESS)
{
string fileName = ("mil1553_embedded_async_to_1553-flex.mxf");
}
if (rc == MAXT_SUCCESS)
{
cmd.command = EX_USER_COMMAND_ID_START;
cmd.paramNum = 0;
rc = commandSend(info.commTxBuffer, info.commRxBuffer, cmd);
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
cmd.command = EX_USER_COMMAND_ID_STOP;
cmd.paramNum = 0;
rc = commandSend(info.commTxBuffer, info.commRxBuffer, cmd);
}
if (rc == MAXT_SUCCESS)
{
cmd.command = EX_USER_COMMAND_ID_READ;
cmd.paramNum = 0;
rc = commandSend(info.commTxBuffer, info.commRxBuffer, cmd);
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
Console.Write("String received count = {0} \n\r", msgCount);
if (rc == MAXT_SUCCESS)
{
byte[] dataByte;
recPtr = rxHostBuffer;
for (data = 0; data < msgCount; data++)
{
Console.Write(
" {0:00}: Timetag={1}, Size={2}, Data=", data, commRec.
timeTag, commRec.
dataSize);
dataByte =
new byte[(int)commRec.
dataSize];
Buffer.BlockCopy(commRec.
data, 0, dataByte, 0, dataByte.Length);
for (word = 0; word < dataByte.Length; word++)
Console.Write("{0:X2}", dataByte[word]);
Console.Write("\n\r");
}
}
}
if (rc == MAXT_SUCCESS)
{
cmd.command = (UInt32)EX_USER_COMMAND_ID_TERMINATE;
cmd.paramNum = 0;
rc = commandSend(info.commTxBuffer, info.commRxBuffer, cmd);
}
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
if (rxHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxHostBuffer);
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
return rc;
}
}
}