MX Foundation 4
hdlc_aperiodic.cs
/*******************************************************************************
//
// File:
// hdlc_aperiodic.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 shows how to use the different types of aperiodic transmissions
// with HDLC channels.
//
// 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_aperiodic
{
const int BUFFER_SIZE = 1 * 1024 * 1024; // 1MB
const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
UInt64 module = 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;
UInt64 dev, mod, port;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
// 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.WriteLine("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine("Press a key to terminate");
Console.Read();
return;
}
//Configuration of the Multi port in HDLC
if (rc == MAXT_SUCCESS)
// Initialize the server
Console.WriteLine();
Console.WriteLine("Starting");
if (rc == MAXT_SUCCESS)
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);
if (rc == MAXT_SUCCESS)
rc = mxfChannelInfoGet(txChannel, out device, out module);
// Sets tx interframe time fill
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_TX_INTERFRAME_TIME_FILL, VMXF_HDLC_TX_INTERFRAME_TIME_FILL_NONE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_INTERNAL_CLOCK_FREQ, 1000000);
// Sets tx module FIFO threshold
if (rc == MAXT_SUCCESS)
{
rc = mxfAttributeUint64Set(module, KMXF_HDLC_MODULE_TX_FIFO_AF, 200);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module, KMXF_HDLC_MODULE_TX_FIFO_AE, 100);
}
// Enables loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Enables HDLC
if (!rc)
rc = mxfHDLCChannelEnable(txChannel, VMXF_HDLC_CLOCK_SOURCE_INTERNAL);
if (!rc)
rc = mxfHDLCChannelEnable(rxChannel, VMXF_HDLC_CLOCK_SOURCE_EXTERNAL);
// Allocates buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = BUFFER_SIZE;
// 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 = BUFFER_SIZE;
// 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 nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
// Gets the physical port location
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(rxChannel, out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Acquisition Channel (RX) location={0}.{1}.{2}", dev, mod, port);
}
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(txChannel, out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Transmitter Channel (TX) location={0}.{1}.{2}", dev, mod, port);
}
// Starts the acquisition process
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqModeSet(rxBuffer, MXF_RXACQ_MODE_LINEAR);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
mxfSleep(100);
if (rc == MAXT_SUCCESS)
{
// Starts HDLC data transmission
rc = StartAperiodicTransmissionDefault(txBuffer, txHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = StartAperiodicTransmissionAbsolute(device, txBuffer, txHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = StartAperiodicTransmissionRecordAbsolute(device, txBuffer, txHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = StartAperiodicTransmissionRecordRelative(txBuffer, rxHostBuffer);
if (rc == MAXT_SUCCESS)
rc = ReadAcquisitionData(rxBuffer, rxHostBuffer);
}
// Stops acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
// 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;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 ReadAcquisitionData(UInt64 rxBuffer, IntPtr rxHostBuffer)
{
UInt64 status, msgsCount, bytesCount;
UInt32 rc;
// Reads and displays records
Console.WriteLine("Receiving ...");
rc = mxfHDLCRxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, rxHostBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("String received count = {0}", msgsCount);
// Displays received strings
DisplayDataArray(msgsCount, rxHostBuffer);
}
else
Console.WriteLine("Acquisition read failed; rc=0x{0:x8}", rc);
return rc;
}
private static UInt32 StartAperiodicTransmissionDefault(UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt64 data, word;
UInt32 rc = 0;
UInt16 ramp = 0;
rec.data = new UInt16[2048];
// In the example below the record are sent back-to-back 100 ms in the future.
Console.WriteLine();
Console.WriteLine("Aperiodic transmission (Relative Start Time-Default)");
//Prepares records to send for basic transmission/reception test
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
if (data == 0)
rec.control = MXF_HDLC_TX_REC_CTRL_CLOSING_FLAG_NOT_SEND | MXF_HDLC_TX_REC_CTRL_FCS_NOT_SEND | MXF_HDLC_TX_REC_CTRL_FRAMESIZE_NOT_SEND;
else if (data == 1)
rec.control = MXF_HDLC_TX_REC_CTRL_OPENING_FLAG_NOT_SEND;
else
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = (data != 0) ? (UInt32)64 : (UInt32)4096;
rec.reserved = 0;
for (word = 0; word < rec.dataSize / 2; word++, ramp++)
{
rec.data[word] = ramp;
}
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
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);
return rc;
}
private static UInt32 StartAperiodicTransmissionAbsolute(UInt64 device, UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt32 rc;
UInt32 delay100ms = 100000000;
UInt64 currentTime;
UInt64 data, word;
rec.data = new UInt16[2048];
// Get the current time
rc = mxfDeviceTimerGet(device, out currentTime);
if (rc != MAXT_SUCCESS)
return rc;
// In the example below the option MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME
// is used. In this case the records are sent freely (no time clock control)
// with a start transmission time based on the device clock + 100ms in the future.
Console.WriteLine();
Console.WriteLine("Aperiodic transmission (Absolute)");
if (rc == MAXT_SUCCESS)
{
//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 = (UInt32)(12 + data);
rec.reserved = 0;
for (word = 0; word < rec.dataSize / 2; word++)
{
rec.data[word] = (UInt16)(0x1111 * data);
}
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
// Transmit the array of records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting ...");
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_ABSOLUTE_START_TIME, currentTime + delay100ms, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(1000);
return rc;
}
private static UInt32 StartAperiodicTransmissionRecordAbsolute(UInt64 device, UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt64 currentTime;
UInt32 rc;
UInt64 data, word;
rec.data = new UInt16[2048];
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME
// In this case the records are sent based on the absolute clock
// timing specified in the timetag field for each records.
Console.WriteLine();
Console.WriteLine("Aperiodic transmission (Absolute with timetag)");
// Get the current time
rc = mxfDeviceTimerGet(device, out currentTime);
if (rc != MAXT_SUCCESS)
return rc;
currentTime += 100000000;
// Set the record
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record are sent with a timetag of 10 ms interval.
if (rc == MAXT_SUCCESS)
{
//Prepares recordw to send for basic transmission/reception test
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = currentTime;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 16;
rec.reserved = 0;
for (word = 0; word < rec.dataSize / 2; word++)
{
rec.data[word] = (UInt16)(0x1111 * data);
}
currentTime += 10000000;
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
// Transmit the array of records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting ...");
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_ABSOLUTE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(1000);
return rc;
}
private static UInt32 StartAperiodicTransmissionRecordRelative(UInt64 txBuffer, IntPtr txHostBuffer)
{
IntPtr recPtr = txHostBuffer;
UInt64 currentTime;
UInt32 rc = 0;
UInt64 data, word;
rec.data = new UInt16[2048];
// In the example below the timetag in the record is set
// and the option MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME.
// The relative time between each records is set in the
// timetag field of the record array.
Console.WriteLine();
Console.WriteLine("Aperiodic transmission (Record Relative)");
// Set the record
// The scheduling timetag is as follow:
// clock current time + 100 ms
// Each record are sent with a timetag a 10000 uS (10 ms) interval.
if (rc == MAXT_SUCCESS)
{
//Prepares records to send for basic transmission/reception test
for (data = 0, currentTime = 100000000; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = currentTime;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 32;
rec.reserved = 0;
for (word = 0; word < rec.dataSize / 2; word++)
{
rec.data[word] = (UInt16)(0x1010 * data);
}
currentTime += 10000000;
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
// Transmit the array of records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting ...");
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(1000);
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_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;
UInt32 rc = MAXT_SUCCESS;
IntPtr recPtr = rec;
{
data= new UInt16[2048]
};
Console.WriteLine();
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.WriteLine();
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
}
}
Updated 10/23/2023