MX Foundation 4
hdlc.cs
/*****************************************************************************
//
// File:
// hdlc.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 basic usage of HDLC channel class
// for transmission and reception.
//
// Hardware requirements:
// - MAXT FlexMulti with loopback between TX0 and RX0 and TX4 and RX7 Enhanced 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
{
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;
UInt32 word;
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
if (rc == MAXT_SUCCESS)
// 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, FCS type, interframe fill type, encoding type, clock source and internal clock frequency attributes
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(rxChannel, KMXF_HDLC_FCS_TYPE, VMXF_HDLC_TX_RX_FCS_CCITT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_FCS_TYPE, VMXF_HDLC_TX_RX_FCS_CCITT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_DATA_ENCODING, VMXF_HDLC_ENCODING_NRZ);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_DATA_ENCODING, VMXF_HDLC_ENCODING_NRZ);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_CLOCK_SOURCE, VMXF_HDLC_CLOCK_SOURCE_EXTERNAL);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_CLOCK_SOURCE, VMXF_HDLC_CLOCK_SOURCE_INTERNAL);
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, 100000);
// Enables loopback
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocates 1 MB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 1 * 1024 * 1024;
// 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 1 MB buffer for RX data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 1 * 1024 * 1024;
// 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);
// Starts acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
{
recPtr = txHostBuffer;
//Prepares records to send for basic transmission/reception test
for (data = 0; (data < MAX_TX_RECORDS_TO_TRANSMIT) && (rc == MAXT_SUCCESS); data++)
{
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 64;
rec.reserved = 0;
for (word = 0; word < rec.dataSize / 2; word++)
{
rec.data[word] = (UInt16)(0x0101 * 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 messages
rc = mxfHDLCTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(5000);
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);
if (rc == MAXT_SUCCESS)
{
// Displays received strings
DisplayDataArray(msgCount, rxHostBuffer);
}
// Stops acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
// 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))
{
// 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 03/30/2022