MX Foundation 4
csdb_rx_acquisition_trigger.cs
/*******************************************************************************
//
// File:
// csdb_rx_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 a basic periodic transmission using the periodic
// scheduling service. The acquisition process is then started and waits for
// bit 7 value of byte 1 of label 07 to be 0.
//
// Hardware Requirements:
// - MAXT Flex with loopback between first TX and RX CSDB Enhanced channels.
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace csdb_example
{
class csdb_rx_acquisition_trigger
{
const int BUFFER_SIZE = 1 * 1024 * 1024;// 1 MiB
const int LABEL = 0x7;
const int SI = 0;
const int BLOCKCOUNT = 6;
public static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[2];
UInt64 count = 0;
IntPtr hostBuffer = IntPtr.Zero;
UInt64 condList = 0;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
// Connects to services and initialize environment
#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.WriteLine();
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
Console.WriteLine();
Console.WriteLine("Starting");
// initialize init callback handler to set IPM-ASYNC-EH first TX and RX channel to CSDB
if (rc == MAXT_SUCCESS)
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get the module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, out count, module);
// Get the first CSDB Protocol RX channel (RX logical #0)
if (rc == MAXT_SUCCESS)
rc = mxfChannelAllGet(server, MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_ASYNC_EH, 1, out count, rxChannel);
// Get the first CSDB Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfChannelAllGet(server, MXF_CLASS_CSDB, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_ASYNC_EH, 1, out count, txChannel);
// If channel not found, return an error
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Configure RX and TX channel to 12.5Kbps, blockcount 6, parity odd, one stop bit
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_SPEED, 12500);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_PARITY, VMXF_CSDB_PARITY_ODD);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_PARITY, VMXF_CSDB_PARITY_ODD);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_STOPBITSIZE, 1);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_STOPBITSIZE, 1);
// Enable loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Set timebase to 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Alloc host buffer
if (rc == MAXT_SUCCESS)
{
try
{
hostBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Allocate TX sync block buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 0xA5, 4096, out txBuffer[0], IntPtr.Zero);
// Allocate TX periodic update buffer
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], LABEL, BUFFER_SIZE, out txBuffer[1], IntPtr.Zero);
// Configure trigger
// Create the condition list
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigConditionListAlloc(server, out condList);
// The condition will be triggered on bit 7 value of byte 1 of LABEL/SI
if (rc == MAXT_SUCCESS)
{
condParam.mask = 0x000083ff;
condParam.data = (SI << 8) | LABEL;
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 30 which allows some records to be read before the condition was triggered.
// For the CSDB protocol this number is rounded to the closest internal acquisition buffer size.
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigSet(rxBuffer, condList, 30);
// Start the acquisition process
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Acquisition started");
}
}
// Start data transmission
if (rc == MAXT_SUCCESS)
{
rc = PeriodicScheduling(txChannel[0], txBuffer, hostBuffer);
if (rc == MAXT_SUCCESS)
{
// Read data in acquisition buffer
rc = ReadAcquisitionData(rxBuffer, hostBuffer);
}
}
// Stop and flush unread data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqClear(rxBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Acquisition stopped");
}
}
// Free trigger condition list
if (rc == MAXT_SUCCESS)
// Catch any previous error
if (rc != MAXT_SUCCESS)
{
StringBuilder buffer = new StringBuilder(256);
if (mxfSystemErrorStringGet(server, rc, 256, buffer) != MAXT_SUCCESS)
buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
Console.WriteLine(buffer);
}
Console.WriteLine();
Console.WriteLine("Terminating");
// Free all buffers and terminate
if (device != 0)
if (hostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(hostBuffer);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
/***************************************************************************************************************/
// PeriodicScheduling
/***************************************************************************************************************/
private static UInt32 PeriodicScheduling(UInt64 txChannel, UInt64[] txBuffer, IntPtr recPtr)
{
UInt64 schedule;
UInt64 msg = 0;
UInt32 rc;
UInt64 i;
{
data = new byte[12]
};
// Create the periodic scheduler
rc = mxfTxPeriodicScheduleNew(txChannel, out schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000, 0, out msg);
// Define the number of buffer for the list and link to it
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Set records for the buffers
if (rc == MAXT_SUCCESS)
{
// Set the sync block
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.reserved = 0;
for (i = 0; i < BLOCKCOUNT; i++)
recCsdb.data[i] = 0xa5;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer[0], 1, recPtr);
// Set the message block
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.data[0] = LABEL;
recCsdb.data[1] = 0x80 | SI;
for (i = 2; i < BLOCKCOUNT; i++)
recCsdb.data[i] = (byte)i;
recCsdb.reserved = 0;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recPtr);
}
// Run the scheduler
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Running periodic transmission, please wait...");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Wait 0.5 second
if (rc == MAXT_SUCCESS)
{
mxfSleep(500);
// fire trigger
recCsdb.control = 0;
recCsdb.data[1] = SI;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recPtr);
}
// Wait 0.5 second
if (rc == MAXT_SUCCESS)
{
mxfSleep(500);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmission stopped");
}
return rc;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 ReadAcquisitionData(UInt64 rxBuffer, IntPtr recCsdb)
{
IntPtr recPtr = recCsdb;
UInt64 status, msgsCount, bytesCount;
UInt64 i, j, trigTime = 0;
UInt32 rc;
//char szTime[1024];
UInt64 usec;
{
data = new byte[12]
};
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(rxBuffer, out status, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
{
if ((status & MXF_RXACQ_STATUS_TRIG_OCCURRED) == MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Display the acquisition trigger time
rc = mxfRxAcqTrigTimeGet(rxBuffer, out trigTime);
if (rc == MAXT_SUCCESS)
{
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
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
Console.WriteLine("Trigger not fired");
}
// Read and display records
rc = mxfCSDBRxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, recCsdb);
for (j = 0; (j < msgsCount) && (rc == MAXT_SUCCESS); j++)
{
rec = (MXF_CSDB_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_CSDB_DATAREC));
Console.Write("{0:D2}: Timetag={1:D16}: CSDB data=[ ", j, rec.timeTag);
for (i = 0; i < BLOCKCOUNT; i++)
Console.Write("0x{0:X2} ", rec.data[i]);
Console.Write("] {0}\n\r", trigTime == rec.timeTag ? "*" : "");
rc = mxfNextRecordPtrGet(MXF_CLASS_CSDB, MXF_RECTYPE_DATAREC, recPtr, out recPtr);
}
return rc;
}
private static UInt32 initHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
UInt64 device;
MXF_DEVICE_INFO deviceInfo = new MXF_DEVICE_INFO();
UInt32 rc;
if (attrib == KMXF_CHANNEL_CLASS)
{
rc = mxfSystemDeviceGet(server, deviceIndex, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceInfoGet(device, out deviceInfo);
if ((rc == MAXT_SUCCESS) && (deviceInfo.modules[moduleIndex].type == MXF_MODULE_ASYNC_EH))
{
// Sets IPM-ASYNC-EH first TX and RX channel to CSDB
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_CSDB;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
}
}
Updated 10/23/2023