MX Foundation 4
async_tx_error_injection.cs
/*****************************************************************************
//
// File:
// async_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 how to inject errors and trig a discrete line
// using the ASYNC record control field. Note that this uses RS-422 instead
// of the default RS-485.
//
// Hardware requirements:
// - MAXT Flex device with ASYNC support
//
*****************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace async_example
{
class async_tx_error_injection
{
private const int MAX_TX_RECORDS_TO_TRANSMIT = 8;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
var module = new UInt64[1];
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
UInt64 txBuffer = 0;
var rec = new MXF_ASYNCEH_DATAREC();
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
uint txBufferSize;
uint rxBufferSize = 0;
IntPtr rxHostBuffer = IntPtr.Zero;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 rxAcqStatus = 0;
UInt64 msgCount = 0;
UInt64 byteCount = 0;
UInt64 data, byteByte;
UInt64 freeBytes;
#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;
}
// Initialize the server
Console.WriteLine();
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
//Get Module handle
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, out moduleCount, module);
// If module not found, return an error
if (rc == MAXT_SUCCESS && moduleCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
//Get handle of first Async RX Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rxChannel);
//Get handle of first Async Tx Channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
//Enables Loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_ASYNCEH_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
//Allocates buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 10 * 1024;
//Allocates Tx Aperiodic static buffer for HIGH priority queue
rc = mxfTxAperiodicBufferAlloc(txChannel[0], 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;
}
}
}
//Allocate buffer for Rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 10 * 1024;
//Allocates Rx acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel[0], rxBufferSize, out rxBuffer, IntPtr.Zero);
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);
//Set electrical interface to RS-422
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_ELECTRICAL_INTERFACE, VMXF_ASYNCEH_ELECTRICAL_INTERFACE_RS422);
//Set RX and TX channel speed to 1mbps
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_ASYNCEH_SPEED, 1000000);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_ASYNCEH_SPEED, 1000000);
//Set RX string gap to 1 bit
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_ASYNCEH_RX_STRING_GAP, 1);
//Enable Tx0 discrete trigger on transmission (discrete #1)
if (rc == MAXT_SUCCESS)
rc = mxfChannelDiscreteOutputTriggerEnableSet(txChannel[0], MXF_ASYNCEH_DISCRETE_OUTPUT_TRIG_ON_TX, VMXF_ENABLE, 1);
//Start Acquisition
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqStart(rxBuffer, MXF_RXACQ_FLAG_DEFAULT, 0, 0);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Acquisition started.");
}
if (rc == MAXT_SUCCESS)
mxfSleep(100);
rec.data = new byte[256];
if (rc == MAXT_SUCCESS)
{
UInt64 startTime = 100 * 1000 * 1000; //100 msec
UInt64 delay = 1 * 1000 * 1000; //1 msec
IntPtr recPtr = txHostBuffer;
// Prepare string array (8 bytes per string with delay of 1 msec between each)
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = startTime + ((data + 1) * delay);
if (data == 2) // send this record with a long word size error and discrete output trigger
rec.control = (uint)(MXF_ASYNCEH_TX_REC_CTRL_LONG_WORD_SIZE | MXF_ASYNCEH_TX_REC_CTRL_DISCRETE_OUTPUT_TRIG);
else
rec.control = 0;
rec.repeatCount = 1;
rec.dataSize = 8;
for (byteByte = 0; byteByte < rec.dataSize; byteByte++)
rec.data[byteByte] = (byte)(0x11 * byteByte);
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfASYNCEHNextDataRecordPtrGet(recPtr, out recPtr);
}
}
//Starts transmit
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmitting...");
rc = mxfASYNCEHTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_USE_RECORD_RELATIVE_TIME, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
mxfSleep(5000);
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferStatusGet(rxBuffer, out rxAcqStatus, out msgCount, out byteCount, out freeBytes);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Receiving...");
rc = mxfASYNCEHRxAcqRead(rxBuffer, 0xFFFFFFFF, rxBufferSize, out rxAcqStatus, out msgCount, out byteCount, rxHostBuffer);
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("String received count = {0}", msgCount);
}
//Display received strings
if (rc == MAXT_SUCCESS)
DisplayDataArray(msgCount, rxHostBuffer);
//Stops Acquisition
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
//Free device and host buffers
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
if (txBuffer != 0)
//Get 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...");
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
private static void DisplayDataArray(UInt64 recNum, IntPtr rxHostBuffer)
{
UInt64 iRec, iData;
IntPtr p = rxHostBuffer;
Console.WriteLine();
for (iRec = 0; iRec < recNum; iRec++)
{
rec = (MXF_ASYNCEH_DATAREC)Marshal.PtrToStructure(p, typeof(MXF_ASYNCEH_DATAREC));
Console.WriteLine("{0}: Timetag: {1} Control: {2:x8} Size: {3}", iRec, rec.timeTag, rec.control, rec.dataSize);
for(iData = 0; iData < rec.dataSize/2; iData++)
{
Console.Write("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
}
}
Updated 03/30/2022