#define LOOPBACK
#define LOCAL
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_manyErrorInjections
{
const UInt32 BITCNT_SHIFT = 18;
const UInt32 RT_ADRS = 20;
const UInt32 RT_SUBADRS = 3;
const UInt32 NB_BC_BUFFERS = 4;
const UInt32 NB_RT_BUFFERS = 2;
static void Main(string[] args)
{
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var bc = new UInt64[1];
var bm = new UInt64[1];
var rt = new UInt64[NB_RT_BUFFERS];
var bcBufferTx = new UInt64[NB_BC_BUFFERS];
UInt64 bmBufferRx = 0;
var rtBuffer = new UInt64[NB_RT_BUFFERS];
UInt32 txBufferSize = 0;
IntPtr txBuffer = IntPtr.Zero;
txRec1553.data = new UInt16[36];
UInt32 rxBufferSize = 0;
IntPtr rxBuffer = IntPtr.Zero;
err.errors = new UInt16[34];
var errorString = new StringBuilder(200);
UInt64 bus = MXF_MIL1553_BUS_A | MXF_MIL1553_BUS_B;
UInt64 rxAcqStatus;
UInt64 msgCount;
UInt64 byteCount, freeBytes, pendingBytes, indexBuffer;
UInt64 rxRec;
UInt32 loop = 0;
UInt64 address, subAddress, dir, wordCount;
UInt64 deviceCount = 0;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt32 rc;
var timetagArray = new UInt64[NB_BC_BUFFERS];
UInt64 index = 0;
#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 && deviceCount > 0)
{
if (rc == MAXT_SUCCESS && moduleCount == 0)
}
if (rc == MAXT_SUCCESS && moduleCount > 0)
if (rc == MAXT_SUCCESS && channelCount > 0)
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
#if LOOPBACK
if (rc == MAXT_SUCCESS)
#endif
if (rc == MAXT_SUCCESS)
{
txBufferSize = 1024;
for (index = 0; index < NB_BC_BUFFERS && rc == MAXT_SUCCESS; index++)
{
}
for (index = 0; index < NB_RT_BUFFERS && rc == MAXT_SUCCESS; index++)
{
if (rc == MAXT_SUCCESS)
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)
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)
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
{
err.errors[33] = 32;
}
}
if (rc == MAXT_SUCCESS)
{
minorFrame[0].modulo = 1;
minorFrame[0].buffer = bcBufferTx[0];
minorFrame[0].retryHaltOptions = 0;
if (rc == MAXT_SUCCESS)
minorFrame[1].modulo = 1;
minorFrame[1].buffer = bcBufferTx[1];
minorFrame[1].retryHaltOptions = 0;
if (rc == MAXT_SUCCESS)
minorFrame[2].modulo = 1;
minorFrame[2].buffer = bcBufferTx[3];
minorFrame[2].retryHaltOptions = 0;
minorFrame[2].options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MSG_OPT_BUS_B;
if (rc == MAXT_SUCCESS)
minorFrame[3].modulo = 1;
minorFrame[3].buffer = bcBufferTx[2];
minorFrame[3].retryHaltOptions = 0;
if (rc == MAXT_SUCCESS)
{
propertiesMinorFrame.repeatCount = 1;
propertiesMinorFrame.modulo = 1;
propertiesMinorFrame.options = MXF_MIL1553_TXPERIODIC_MJRFRAME_MINOR_PROPERTIES_OPT_BRANCH_END;
}
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 1;
txRec1553.dataSize = 66;
txRec1553.data[0] = 0x0000;
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 0;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 66;
txRec1553.data[0] = 0x0000;
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 0;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 66;
txRec1553.data[0] = 0x0000;
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = MXF_MIL1553_TXPERIODIC_REC_CTRL_ERROR_INJ;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 2;
txRec1553.dataSize = 0;
for (index = 0; index < 36; index++)
txRec1553.data[index] = 0;
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
txRec1553.repeatCount = 1;
txRec1553.control = 0;
txRec1553.service.txPeriodicUpdateMsg.errorIndex = 0;
txRec1553.dataSize = 66;
txRec1553.data[0] = 0x0000;
for (index = 1; index < txRec1553.dataSize / 2; index++)
txRec1553.data[index] = (UInt16)(0x0101 * (UInt16)index);
Marshal.StructureToPtr(txRec1553, txBuffer, true);
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting BC - RT\n\r");
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
IntPtr recPtr = IntPtr.Zero;
do
{
rc =
mxfMIL1553RxAcqRead(bmBufferRx, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("mxfMIL1553RxAcqRead() error; rc={0:x8}", rc);
recPtr = rxBuffer;
for (rxRec = 0; rxRec < msgCount && rc == MAXT_SUCCESS; rxRec++)
{
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
{
Console.Write("\n\r{0}:\t", rxRec1553[0].timeTag);
timetagArray[rxRec] = rxRec1553[0].timeTag;
switch (msgInfo[0].msgType)
{
case MXF_MIL1553_MSGINFO_TYPE_BCRT:
Console.Write("BC to RT{0} SA{1} WC{2} DataSize : {3} Bus{4} (0x{5:x4}) Control : 0x{6:x8}\n\r", address, subAddress, wordCount, (rxRec1553[0].dataSize / 2) - 2, (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B) > 0 ? "B" : "A", rxRec1553[0].data[0], rxRec1553[0].control);
Console.Write("\t\tParity {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tData length {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_DATA_LENGTH_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tBit Count {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_EXTRA_BIT_ERROR) > 0 ? "Error" : "OK");
break;
case MXF_MIL1553_MSGINFO_TYPE_RTBC:
Console.Write("RT{0} SA{1} to BC WC{2} DataSize : {3} Bus{4} (0x{5:x4}) Control : 0x{6:x8}\n\r", address, subAddress, wordCount, (rxRec1553[0].dataSize / 2) - 2, (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_BUS_B) > 0 ? "B" : "A", rxRec1553[0].data[0], rxRec1553[0].control);
Console.Write("\t\tParity {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_PARITY_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tData length {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_DATA_LENGTH_ERROR) > 0 ? "Error" : "OK");
Console.Write("\t\tBit Count {0}\n\r", (rxRec1553[0].control & MXF_MIL1553_RX_REC_CTRL_MSG_EXTRA_BIT_ERROR) > 0 ? "Error" : "OK");
break;
}
}
}
}
loop++;
} while (loop < 10);
}
if (rc == MAXT_SUCCESS)
{
if (rc == MAXT_SUCCESS)
}
if (rc == MAXT_SUCCESS)
{
Console.Write("\nStopping BC - RT\n\r");
}
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
Console.Write("Timetag Diff between RT20 SA3 -> BC and RT10 SA3 -> BC : {0} usec\n", (timetagArray[2] - timetagArray[1]) / 1000);
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");
}
for (indexBuffer = 0; indexBuffer < 1 && bcBufferTx[indexBuffer] > 0; indexBuffer++)
{
if (bcBufferTx[indexBuffer] != 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
for (indexBuffer = 0; indexBuffer < 1 && rtBuffer[indexBuffer] > 0; indexBuffer++)
{
if (rtBuffer[indexBuffer] != 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
}
if (bmBufferRx > 0)
{
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
Console.Write("\n\rPress enter to terminate\n\r");
Console.ReadKey();
return;
}
}
}