MX Foundation 4
hdlc_tx_error_injection.cs
/*****************************************************************************
//
// File:
// hdlc_tx_error_injection.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 with an error injection in transmission.
//
// 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_tx_error_injection
{
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;
UInt64 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 and internal clock frequency to be sure of the used values
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_ENABLE);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_HDLC_FRAME_SIZE_ENABLE, VMXF_ENABLE);
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 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 with an error injection in transmission for the 3rd record
for (data = 0; (data < MAX_TX_RECORDS_TO_TRANSMIT) && (rc == MAXT_SUCCESS); data++)
{
rec.timeTag = 0;
rec.control = (data != 2) ? 0 : MXF_HDLC_TX_REC_CTRL_FCS_NOT_SEND;
rec.repeatCount = 1;
rec.dataSize = 8;
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);
}
}
if (rc == MAXT_SUCCESS)
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);
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);
// 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.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:D4} ", iRec, p.timeTag, p.control, p.dataSize);
for (iData = 0; iData < p.dataSize / 2; iData++)
{
if ((p.dataSize % 2) == 0)
Console.Write("{0:x4} ", p.data[iData]);
else
Console.Write("{0:x2} {1:x2} ", (byte)p.data[iData], (byte)(p.data[iData] >> 8));
if ((((iData + 1) % 8) == 0) && (iData + 1 < p.dataSize / 2))
Console.Write("\n ");
}
Console.WriteLine();
if ((p.control & MXF_HDLC_RX_REC_CTRL_CLOSING_ERROR) == MXF_HDLC_RX_REC_CTRL_CLOSING_ERROR)
Console.Write("Closing error ");
if ((p.control & MXF_HDLC_RX_REC_CTRL_ABORT) == MXF_HDLC_RX_REC_CTRL_ABORT)
Console.Write("Abort error ");
if ((p.control & MXF_HDLC_RX_REC_CTRL_DECODING_ERROR) == MXF_HDLC_RX_REC_CTRL_DECODING_ERROR)
Console.Write("Decoding error ");
if ((p.control & MXF_HDLC_RX_REC_CTRL_FCS_ERROR) == MXF_HDLC_RX_REC_CTRL_FCS_ERROR)
Console.Write("FCS error ");
if ((p.control & MXF_HDLC_RX_REC_CTRL_FRAMESIZE_ERROR) == MXF_HDLC_RX_REC_CTRL_FRAMESIZE_ERROR)
Console.Write("Frame Size error ");
Console.WriteLine();
rc = mxfHDLCNextDataRecordPtrGet(recPtr, out recPtr);
}
}
}
}
Updated 10/23/2023