MX Foundation 4
mil1553_bm_acquisition_trigger.cs
/*******************************************************************************
//
// File:
// mil1553_bm_acquisition_trigger.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2019, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This demo illustrates how to use the trigger functionality
// to start acquisition on a receive channel.
//
// The program first starts the acquisition and waits for command BC-RT 10 SA 1 WC 4.
// Aperiodic transmission is then done to fire the trigger.
//
// Hardware Requirements:
// - MAXT Flex1553-PCIe or FlexMulti or 500 series carrier with IPM-1553-MRT
//
*******************************************************************************/
#define LOCAL
#define LOOPBACK
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace mil1553_example
{
public class mil1553_bm_acquisition_trigger
{
const UInt64 MAX_TX_RECORDS_TO_TRANSMIT = 100;
const UInt64 BUFFER_SIZE = MAX_TX_RECORDS_TO_TRANSMIT * 104; // Space for at least 100 records => Size of MXF_MIL1553_DATAREC = 104 bytes
/***************************************************************************************************************/
// Main
/***************************************************************************************************************/
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 count = 0;
var bm = new UInt64[1];
var bc = new UInt64[1];
UInt64 bmBuffer = 0;
UInt64 bcBuffer = 0;
IntPtr rec1553 = IntPtr.Zero;
UInt64 dev, mod, port;
UInt64 condList = 0;
UInt16 triggerData;
// Connect to services local or remote
#if (LOCAL)
rc = mxfServerConnect("0.0.0.0", "", "", Convert.ToUInt64(false), out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(false), out server);
#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;
}
// Initialize the server
Console.Write("\nStarting\n");
rc = mxfSystemInit(server);
// Obtain the first MIL1553 Protocol BM channel(BM logical #0)
if (rc == MAXT_SUCCESS)
rc = mxfChannelAllGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_BM_CHANNEL, MXF_MODULE_ALL, 1, out count, bm);
// Obtain the first MIL1553 Protocol BC channel (BC logical #0)
if (rc == MAXT_SUCCESS && count > 0)
rc = mxfChannelAllGet(server, MXF_CLASS_MIL1553, MXF_SCLASS_BC_CHANNEL, MXF_MODULE_ALL, 1, out count, bc);
// If channel not found, return an error
if (rc == MAXT_SUCCESS && count == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Set timebase to computer time 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Get the physical port location
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(bm[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("Acquisition Channel (BM) location={0}.{1}.{2}\n", dev, mod, port);
}
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(bc[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.Write("Transmitter Channel (BC) location={0}.{1}.{2}\n", dev, mod, port);
}
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(bm[0], KMXF_MIL1553_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(bm[0], BUFFER_SIZE, out bmBuffer, IntPtr.Zero);
// Allocate TX Aperiodic buffer (1MiB)
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicBufferAlloc(bc[0], 0, BUFFER_SIZE, out bcBuffer, IntPtr.Zero);
// Allocate host buffer
if (rc == MAXT_SUCCESS)
{
try
{
rec1553 = Marshal.AllocHGlobal((int)BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Configure trigger
// Create the condition list
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigConditionListAlloc(server, out condList);
// The condition will be triggered on BC-RT 10 SA 1 WC 4 command
if (rc == MAXT_SUCCESS)
{
mxfMIL1553CommandCompose(10, 1, MXF_MIL1553_COMMAND_DIR_RX, 4, out triggerData);
condParam.mask = 0x0000ffff;
condParam.data = triggerData;
condParam.offset = 0;
condParam.options = MXF_RXACQ_TRIG_COND_RDATA_OPTIONS_EQUAL;
rc = mxfRxAcqTrigConditionAdd(condList, MXF_RXACQ_TRIG_COND_ID_RDATA_DW, ref condParam);
}
// Set the trigger with a pretrig count of 3 which allows some records to be read before the condition was triggered.
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigSet(bmBuffer, condList, 3);
// Start the acquisition process
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(bmBuffer, MXF_RXACQ_MODE_LINEAR);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStart(bmBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition started\n\r");
}
// Start ASYNC data transmission
if (rc == MAXT_SUCCESS)
{
rc = TransmitAperiodicData(bcBuffer, rec1553);
if (rc == MAXT_SUCCESS)
{
// Read ASYNC data in acquisition buffer
rc = ReadAcquisitionData(bmBuffer, rec1553);
}
}
// Stop and flush unread data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(bmBuffer);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqClear(bmBuffer);
if (rc == MAXT_SUCCESS)
Console.Write("\nAcquisition stopped\n\r");
}
// Free trigger condition list
if (rc == MAXT_SUCCESS)
// Catch any previous error
if (rc != MAXT_SUCCESS)
{
var buffer = new StringBuilder(200);
if (mxfSystemErrorStringGet(server, rc, (UInt32)buffer.Capacity, buffer) != MAXT_SUCCESS)
{
buffer.Clear();
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.Write(buffer + "\n\r");
}
if (bmBuffer > 0)
{
rc = mxfRxAcqBufferFree(bmBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
if (bcBuffer > 0)
{
rc = mxfTxAperiodicBufferFree(bcBuffer);
if (rc != MAXT_SUCCESS)
Console.Write("Free buffer failed !\n\r");
}
Console.Write("\nTerminating\n");
if (rec1553 != IntPtr.Zero)
Marshal.FreeHGlobal(rec1553);
Console.Write("\nPress enter to terminate\n");
Console.ReadKey();
return;
}
/***************************************************************************************************************/
// TransmitAperiodicData
/***************************************************************************************************************/
private static UInt32 TransmitAperiodicData(UInt64 bcBuffer, IntPtr rec1553)
{
UInt32 rc = 0;
UInt64 data;
var rec = new MXF_MIL1553_DATAREC();
rec.data = new UInt16[36];
IntPtr recPtr = rec1553;
// Prepare data
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT && rc == MAXT_SUCCESS; data++)
{
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 10;
rec.reserved = 0;
rec.service.txAperiodic.delay = 0;
rec.service.txAperiodic.errorIndex = 0;
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553CommandCompose(data % 32, 1, MXF_MIL1553_COMMAND_DIR_RX, 4, out rec.data[0]);
rec.data[1] = 0x1111;
rec.data[2] = 0x2222;
rec.data[3] = 0x3333;
rec.data[4] = 0x4444;
Marshal.StructureToPtr(rec, recPtr, true);
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
Console.Write("Transmitting ...\n");
if (rc == MAXT_SUCCESS)
rc = mxfTxAperiodicWrite(bcBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, rec1553);
// Wait a little for transmission to occur
if (rc == MAXT_SUCCESS)
rc = mxfSleep(1000);
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 ReadAcquisitionData(UInt64 bmBuffer, IntPtr rec1553)
{
var rec = new MXF_MIL1553_DATAREC();
IntPtr recPtr = IntPtr.Zero;
UInt64 status, msgsCount, bytesCount, freeBytesCount;
UInt64 trigTime = 0;
UInt32 rc = 0;
UInt64 usec;
var offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);
var time = new DateTime(1970, 1, 1).AddTicks(offset.Ticks); //create a time object set to jan 01 1970 in current timezone
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(bmBuffer, out status, out msgsCount, out bytesCount, out freeBytesCount);
if (rc == MAXT_SUCCESS && (status & MXF_RXACQ_STATUS_TRIG_OCCURRED) > 0)
{
// Display the acquisition trigger time
rc = mxfRxAcqTrigTimeGet(bmBuffer, out trigTime);
if (rc == MAXT_SUCCESS)
{
time = time.AddMilliseconds(trigTime / 1000);
usec = trigTime % 1000;
Console.WriteLine("Event triggered at {0}:{1:000}", time.ToString("yyyy-MM-dd HH:mm:ss:fff"), usec);
}
}
else if (rc == MAXT_SUCCESS)
Console.Write("Trigger not fired\n");
// Read and display records
if (rc == MAXT_SUCCESS)
rc = mxfMIL1553RxAcqRead(bmBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, rec1553);
recPtr = rec1553;
for (UInt64 j = 0; j < msgsCount && rc == MAXT_SUCCESS; j++)
{
rec = (MXF_MIL1553_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_MIL1553_DATAREC));
Console.Write(" {0:00}: Timetag={1:000000000000}, Size={2}\n Data=", j, rec.timeTag, rec.dataSize);
for (UInt64 word = 0; word < rec.dataSize / 2; word++)
Console.Write("{0:x4} ", rec.data[word]);
Console.Write("{0}\n", trigTime == rec.timeTag ? "*" : "");
rc = mxfMIL1553NextDataRecordPtrGet(recPtr, out recPtr);
}
return rc;
}
}
}
Updated 10/23/2023