#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_embedded_update_host
{
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 module1553;
public UInt64 bcChannel;
public UInt64 bmChannel;
public UInt64 commTxBuffer;
public UInt64 commRxBuffer;
public UInt64 commTxUpdateDataBuffer;
}
public struct EX_COMMAND
{
public UInt32 command;
public UInt32 paramNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public UInt32[] param;
}
static void Main(string[] args)
{
TEST_INFO info = new TEST_INFO();
UInt32 rc;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
var temp = new UInt64[10];
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.module1553 = temp[0];
if (rc == MAXT_SUCCESS && moduleCount > 0)
rc =
mxfModuleChannelAllGet(info.module1553, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, 1, out channelCount, temp);
info.bcChannel = temp[0];
if (rc == MAXT_SUCCESS && channelCount > 0)
rc =
mxfModuleChannelAllGet(info.module1553, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, 1, out channelCount, temp);
info.bmChannel = temp[0];
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
rc = hostPart(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 = 3;
return Convert.ToUInt32(true);
}
return Convert.ToUInt32(false);
}
public static UInt32 commandSend(UInt64 txBuffer, UInt64 rxBuffer, EX_COMMAND cmd)
{
msg.
data =
new UInt32[256];
IntPtr msgPtr = IntPtr.Zero;
UInt64 readCnt = 0;
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)
{
while (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS && readCnt > 0)
{
if ((msg.
dataSize >= 8) && (msg.
data[0] == EX_USER_COMMAND_ACK))
break;
}
}
}
Marshal.FreeHGlobal(msgPtr);
return rc;
}
public static UInt32 commandRead(UInt64 rxBuffer, UInt64 readCnt, EX_COMMAND cmd)
{
msg.
data =
new UInt32[256];
IntPtr msgPtr = IntPtr.Zero;
UInt64 byteCnt;
UInt32 rc;
try
{
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
cmd = new EX_COMMAND();
cmd.param = new UInt32[32];
rc =
mxfDeviceCommBufferRead(rxBuffer, 1, (UInt64)Marshal.SizeOf(typeof(EX_COMMAND)), out readCnt, out byteCnt, msgPtr);
if (rc == MAXT_SUCCESS && readCnt > 0)
{
cmd.command = msg.
data[0];
cmd.paramNum = msg.
data[1];
for (UInt64 iData = 0; iData < cmd.paramNum * 4; iData++)
cmd.param[iData] = msg.
data[iData + 2];
}
Marshal.FreeHGlobal(msgPtr);
return rc;
}
public static UInt32 hostPart(TEST_INFO info)
{
EX_COMMAND cmd = new EX_COMMAND();
cmd.param = new UInt32[32];
commRec.
data =
new UInt32[256];
IntPtr recPtr = IntPtr.Zero;
UInt32 rc = 0;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 txBufferSize = 0;
UInt64 data;
IntPtr rxHostBuffer = IntPtr.Zero;
UInt64 rxBufferSize = 0;
#if LOOPBACK
#endif
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)
{
txBufferSize = 10 * 1024;
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch
{
rc = MAXT_ERROR_MEM;
}
}
}
if (rc == MAXT_SUCCESS)
{
string filename = "mil1553_embedded_update_embedded-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)
{
UInt32 dataSize = 18;
UInt32 numData = (dataSize - 2) / 2;
UInt16[] data16 = new UInt16[numData + 1];
recPtr = txHostBuffer;
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
for (int i = 1; i <= numData; i++)
data16[i] = (UInt16)data;
Buffer.BlockCopy(data16, 0, commRec.
data, 0, (
int)(numData + 1) * 2);
for (int i = 0; i <= numData / 2; i++)
commRec.
data[i] = dwordSwap(commRec.
data[i]);
Marshal.StructureToPtr(commRec, recPtr, true);
}
Console.Write("Transmitting simulation data...\n\r");
}
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)
{
cmd.command = 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);
return rc;
}
public static UInt32 dwordSwap(UInt32 dword)
{
return ((dword & 0x0000ffff) << 16 | (dword & 0xffff0000) >> 16);
}
}
}