MX Foundation 4
async_flow_control_hw.cs
/*****************************************************************************
//
// File:
// async_flow_control_hw.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 ASYNC enhanced channel class
// for transmission and reception with hardware flow control (RTS/CTS).
// Note that this uses RS-422 instead of the default RS-485.
//
// Hardware requirements:
// - MAXT Flex device with ASYNC support
//
*****************************************************************************/
#define LOCAL
//#define LOOPBACK
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace async_example
{
class async_flow_control_hw
{
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 rxChannels = new UInt64[4];
var txChannels = new UInt64[4];
UInt64 rxChannel;
UInt64 txChannel;
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;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _initHandler = initHandler;
#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;
}
if (rc == MAXT_SUCCESS)
rc = mxfSystemInitAttributeUint64CallbackHandler(server, _initHandler);
// Initialize the server
Console.WriteLine();
Console.WriteLine("Starting");
rc = mxfSystemInit(server);
if (rc == MAXT_ERROR_MANY_MASTER)
rc = MAXT_SUCCESS;
// Get the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Get handle of first ASYNC-EH module
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 ASYNC-EH RX channels
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_RX_CHANNEL, 4, out channelCount, rxChannels);
// Get handle of ASYNC-EH TX channels
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_ASYNC_ENHANCED, MXF_SCLASS_TX_CHANNEL, 4, out channelCount, txChannels);
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
rxChannel = rxChannels[0];
txChannel = txChannels[0];
//Enables Loopback
#if (LOOPBACK)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
//Allocates 4KB buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = 10 * 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;
}
}
}
//Allocate 4kb for Rx data
if (rc == MAXT_SUCCESS)
{
rxBufferSize = 10 * 1024;
//Allocates Rx acquisition static buffer
rc = mxfRxAcqBufferAlloc(rxChannel, 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, KMXF_ASYNCEH_SPEED, 1000000);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_SPEED, 1000000);
//Set RX string gap to 1 bit
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_STRING_GAP, 1);
//Set RX Threshold to 32 words and aging timeout to ~16msec
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_ASYNCEH_MODULE_RX_FIFO_AF, 32);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_ASYNCEH_MODULE_RX_FIFO_AGING, 16000000);
//Set flow control association for transmission (RX0 with RX1)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_RX_FLOWCTRL_TX_INDEX, 1);
//Set flow control association for detection (TX0 with RX1)
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_TX_FLOWCTRL_RX_INDEX, 1);
//Set module flow control RX FIFO threshold
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_ASYNCEH_MODULE_RX_FLOWCTRL_FIFO_AE, 1);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(module[0], KMXF_ASYNCEH_MODULE_RX_FLOWCTRL_FIFO_AF, 32);
//Set rx/tx handshake mode to HIGH level
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel, KMXF_ASYNCEH_HANDSHAKE_MODE, VMXF_ASYNCEH_HANDSHAKE_HIGH);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel, KMXF_ASYNCEH_HANDSHAKE_MODE, VMXF_ASYNCEH_HANDSHAKE_HIGH);
//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
// Prepare string array (8 bytes per string with delay of 1 msec between each)
IntPtr recPtr = txHostBuffer;
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = startTime + (data * delay);
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(1000);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Receiving...");
rc = mxfASYNCEHRxAcqRead(rxBuffer, 0, 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);
Console.Write("Data: ");
for(iData = 0; iData < rec.dataSize/2; iData++)
{
Console.Write("{0:x2}", rec.data[iData]);
}
Console.WriteLine();
}
}
private static UInt32 initHandler(UInt64 server, UInt64 cardIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
HMXF_DEVICE device;
MXF_DEVICE_INFO deviceInfo;
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_ASYNC_EH))
{
if ((channelIndex == 1) || (channelIndex == 1 + deviceInfo.modules[moduleIndex].txCount))
{
value = MXF_CLASS_HANDSHAKE;
return Convert.ToUInt32(true);
}
else
{
value = MXF_CLASS_CSDB;
return Convert.ToUInt32(true);
}
}
}
return Convert.ToUInt32(false);
}
}
}
Updated 03/30/2022