MX Foundation 4
hdlc_trigger.cs
/*****************************************************************************
//
// File:
// hdlc_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 example demonstrates the usage of HDLC channel class
// for transmission and reception using a trigger.
//
// Hardware requirements:
// - MAXT FlexMulti or 500 series carrier with IPM-MULTI
// - loopback between TX0 and RX0 and TX4 and RX7 Multi channels.
//
*****************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace hdlc_example
{
class hdlc_trigger
{
const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
public static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 rxChannel = 0;
UInt64 txChannel = 0;
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
uint txBufferSize = 0;
uint rxBufferSize = 0;
IntPtr rxHostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
IntPtr recPtr = IntPtr.Zero;
UInt64 rxAcqStatus = 0;
UInt64 msgCount = 0;
UInt64 byteCount = 0;
UInt64 data, word = 0;
UInt64 condList = 0;
UInt64 usec;
UInt64 trigTime;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
{
data = new UInt16[2048]
};
// 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
//Configuration of the Multi port in HDLC
// Initializes MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Starting ...");
rc = mxfSystemInit(server);
}
// Gets handle of first HDLC RX channel
if (rc == MAXT_SUCCESS)
rc = mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_RX_CHANNEL, MXF_MODULE_MULTI_EH, 0, out rxChannel);
// Gets handle of first HDLC TX channel
if (rc == MAXT_SUCCESS)
rc = mxfChannelGet(server, MXF_CLASS_HDLC, MXF_SCLASS_TX_CHANNEL, MXF_MODULE_MULTI_EH, 0, out txChannel);
// Sets frame size and internal clock frequency to be sure of the used values
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_DISABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_DISABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_INTERNAL_CLOCK_FREQ, 100000);
// Enables HDLC
if (!rc)
rc = mxfHDLCChannelEnable(txChannel, VMXF_HDLC_CLOCK_SOURCE_INTERNAL);
if (!rc)
rc = mxfHDLCChannelEnable(rxChannel, VMXF_HDLC_CLOCK_SOURCE_EXTERNAL);
// Enables loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocates buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = (uint)MAX_TX_RECORDS_TO_TRANSMIT * (uint)Marshal.SizeOf(typeof(MXF_HDLC_DATAREC));
// Allocates TX Aperiodic static buffer for HIGH priority queue
rc = mxfTxAperiodicBufferAlloc(txChannel, MXF_TXAPERIODIC_PRIORITY_HIGH, txBufferSize, out txBuffer, IntPtr.Zero);
// Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
txHostBuffer = Marshal.AllocHGlobal((int)txBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Allocates buffer for RX data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = (uint)MAX_TX_RECORDS_TO_TRANSMIT * (uint)Marshal.SizeOf(typeof(MXF_HDLC_DATAREC));
// Allocates RX acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel, rxBufferSize, out rxBuffer, IntPtr.Zero);
// Host buffer allocation
if (rc == MAXT_SUCCESS)
{
try
{
rxHostBuffer = Marshal.AllocHGlobal((int)rxBufferSize);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
}
// Sets timebase to RTC usec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_COMPUTER_USEC);
// Configures trigger
// Creates the condition list
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqTrigConditionListAlloc(server, out condList);
// The condition will be triggered on the sixth record
if (rc == MAXT_SUCCESS)
{
condParam.mask = 0x0000000F;
condParam.data = 0x00000005;
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(rxBuffer, condList, 3);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_CIRCULAR);
// Starts acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
{
// Prepares string array (for later when the basic test works)
recPtr = txHostBuffer;
//Prepares records to send for basic transmission/reception test
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 8;
rec.reserved = 0;
for (word = 0; word < rec.dataSize / 2; word++)
{
rec.data[word] = (UInt16)(0x0001 * data);
}
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
//if (!rc)
//DisplayDataArray(MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Transmitting ...");
// Transmits strings on relative record time
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(1000);
// Check trigger happened
rc = mxfRxAcqBufferStatusGet(rxBuffer, out rxAcqStatus, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
if (rc == MAXT_SUCCESS)
{
if ((rxAcqStatus & MXF_RXACQ_STATUS_TRIG_OCCURRED) == MXF_RXACQ_STATUS_TRIG_OCCURRED)
{
// Displays 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");
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Receiving ...");
// Reads rx buffer
rc = mxfHDLCRxAcqRead(rxBuffer, 0, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxHostBuffer);
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("String received count = {0} ", msgCount);
// Displays received strings
DisplayDataArray(msgCount, rxHostBuffer);
}
// Stops acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
// Free trigger condition list
if (rc == MAXT_SUCCESS)
// Disables HDLC
if (txChannel)
if (rxChannel)
// Frees device and host buffers
if (txBuffer != 0)
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
if (rxHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(rxHostBuffer);
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("Terminating ...");
// Unloads MX Foundation library
// Disconnects from MX Foundation library
Console.WriteLine();
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
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_MULTI_EH) || (deviceInfo.modules[moduleIndex].type == MXF_MODULE_MULTI)))
{
// Sets IPM-MULTI-EH first TX and RX channel to HDLC
if ((channelIndex == 0) || (channelIndex == deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_HDLC;
return Convert.ToUInt32(true);
}
else if ((channelIndex == 4) || (channelIndex == deviceInfo.modules[moduleIndex].txCount + 4))
{
value = MXF_CLASS_CLOCK;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
private static void DisplayDataArray(UInt64 recNum, IntPtr rec)
{
UInt64 iRec,
iData;
IntPtr recPtr = rec;
UInt32 rc = MAXT_SUCCESS;
{
data = new UInt16[2048]
};
Console.Write("\n");
for (iRec = 0; (iRec < recNum) && (rc == MAXT_SUCCESS); iRec++)
{
p = (MXF_HDLC_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_HDLC_DATAREC));
Console.Write("{0:D3} {1:D10} 0x{2:x8} {3:D2} ", iRec, p.timeTag, p.control, p.dataSize);
for (iData = 0; iData < p.dataSize / 2; iData++)
{
Console.Write("{0:x4} ", p.data[iData]);
if ((((iData + 1) % 8) == 0) && (iData + 1 < p.dataSize / 2))
Console.Write("\n ");
}
Console.Write("\n");
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
}
}
Updated 10/23/2023