MX Foundation 4
csdb_periodic.cs
/******************************************************************************
//
// File:
// csdb_periodic.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 set a basic periodic transmission
// based on the Collins Standard Digital Bus (CSDB) protocol.
//
// Hardware requirements:
// - MAXT Flex with loopback between first TX and RX CSDB Enhanced channels.
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace csdb_example
{
class csdb_periodic
{
const int BUFFER_SIZE = 4096; // 4KB
const int TX_MSG_LABEL = 5;
const int TX_MSG_SI = 0;
const int BLOCKCOUNT = 6;
public static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
UInt64 count = 0;
var rxChannel = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 rxBuffer = 0;
var txBuffer = new UInt64[2];
IntPtr recPtr = IntPtr.Zero;
UInt64 dev, mod, port, indexBuffer;
MXF_SYSTEM_INIT_ATTRIBUTE_UINT64_HANDLER _InitCSDBProtocolHandler = InitCSDBProtocolHandler;
{
data = new byte[12]
};
// 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
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");
// initialize init callback handler to set TX and RX channel to CSDB
rc = mxfSystemInitAttributeUint64CallbackHandler(server, InitCSDBProtocolHandler);
if (rc == MAXT_SUCCESS)
rc = mxfSystemInit(server);
// Get the first device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_ASYNC_EH, 1, out count, module);
// Obtain the first CSDB Protocol RX channel (RX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_CSDB, MXF_SCLASS_RX_CHANNEL, 1, out count, rxChannel);
// Obtain the first CSDB Protocol TX channel (TX logical #0)
if ((rc == MAXT_SUCCESS) && (count != 0))
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_CSDB, MXF_SCLASS_TX_CHANNEL, 1, out count, txChannel);
// If module or channel not found, return an error
if ((rc == MAXT_SUCCESS) && (count == 0))
rc = MAXT_ERROR_NOT_FOUND;
// Set CSDB block count
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(txChannel[0], KMXF_CSDB_BLOCKCOUNT, BLOCKCOUNT);
// Set timebase to 64-bit microseconds
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_USEC);
// Get the physical port location
if (rc == MAXT_SUCCESS)
{
rc = mxfChannelLocationGet(rxChannel[0], 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[0], out dev, out mod, out port);
if (rc == MAXT_SUCCESS)
Console.WriteLine("Transmitter Channel (TX) location={0}.{1}.{2}", dev, mod, port);
}
//Activate loopback before transmission and reception
#if LOOPBACK
if (rc == MAXT_SUCCESS)
rc = mxfAttributeUint64Set(rxChannel[0], KMXF_CSDB_TX_RX_TEST_LB, VMXF_ENABLE);
#endif
// Allocate RX acquisition buffer
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqBufferAlloc(rxChannel[0], BUFFER_SIZE, out rxBuffer, IntPtr.Zero);
// Allocate TX Periodic Update buffer (1MiB)
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], 0xA5, BUFFER_SIZE, out txBuffer[0], IntPtr.Zero);
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicUpdateMsgBufferAlloc(txChannel[0], TX_MSG_LABEL, BUFFER_SIZE, out txBuffer[1], IntPtr.Zero);
// Allocate host buffer
if (rc == MAXT_SUCCESS)
{
try
{
recPtr = Marshal.AllocHGlobal(BUFFER_SIZE);
}
catch (OutOfMemoryException)
{
rc = MAXT_ERROR_MEM;
}
}
// Start 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)
{
Console.WriteLine();
Console.WriteLine("Acquisition started");
}
}
// Start data transmission
if (rc == MAXT_SUCCESS)
{
rc = PeriodicScheduling(txChannel[0], txBuffer, recPtr);
if (rc == MAXT_SUCCESS)
{
// Read data in acquisition buffer
rc = ReadAcquisitionData(rxBuffer, recPtr);
}
}
// Stop and flush unread data
if (rc == MAXT_SUCCESS)
rc = mxfRxAcqStop(rxBuffer);
if (rc == MAXT_SUCCESS)
{
rc = mxfRxAcqClear(rxBuffer);
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Acquisition stopped");
}
}
// Catch any previous 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");
// Free all buffers and terminate
if (rxBuffer != 0)
mxfRxAcqBufferFree(rxBuffer);
for (indexBuffer = 0; indexBuffer < 2; indexBuffer++)
{
if (txBuffer[indexBuffer] != 0)
mxfTxPeriodicUpdateMsgBufferFree(txBuffer[indexBuffer]);
}
if (recPtr != IntPtr.Zero)
Marshal.FreeHGlobal(recPtr);
Console.WriteLine();
Console.WriteLine("Press enter to terminate");
Console.Read();
return;
}
/***************************************************************************************************************/
// ReadAcquisitionData
/***************************************************************************************************************/
private static UInt32 ReadAcquisitionData(UInt64 rxBuffer, IntPtr recCsdb)
{
IntPtr recPtr = recCsdb;
UInt64 status, msgsCount, bytesCount;
UInt64 i, j;
UInt32 rc;
{
data = new byte[12]
};
// Read and display records
rc = mxfCSDBRxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, recCsdb);
for (j = 0; (j < msgsCount) && (rc == MAXT_SUCCESS); j++)
{
rec = (MXF_CSDB_DATAREC)Marshal.PtrToStructure(recPtr, typeof(MXF_CSDB_DATAREC));
Console.Write("{0:D2}: Timetag={1:D16}: CSDB data=[ ", j, rec.timeTag);
for (i = 0; i < BLOCKCOUNT; i++)
Console.Write("0x{0:X2} ", rec.data[i]);
Console.Write("]\n\r");
rc = mxfCSDBNextDataRecordPtrGet(recPtr, out recPtr);
}
return rc;
}
/***************************************************************************************************************/
// PeriodicScheduling
/***************************************************************************************************************/
private static UInt32 PeriodicScheduling(UInt64 txChannel, UInt64[] txBuffer, IntPtr recPtr)
{
UInt64 schedule;
UInt64 msg = 0;
UInt32 rc;
UInt64 i;
{
data = new byte[12]
};
// Create the periodic scheduler
rc = mxfTxPeriodicScheduleNew(txChannel, out schedule);
// Set scheduling values: Rate=100 ms (.1sec), Phase=0 us
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleMsgAdd(schedule, 100000, 0, out msg);
// Define the number of buffer for the list and link to it
if (rc == MAXT_SUCCESS)
rc = mxfTxPeriodicScheduleBufferListAdd(msg, 2, 0, txBuffer);
// Set records for the buffers
// Set the sync block
if (rc == MAXT_SUCCESS)
{
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.reserved = 0;
for (i = 0; i < BLOCKCOUNT; i++)
recCsdb.data[i] = 0xa5;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[0], 1, recPtr);
}
// Set the message block
if (rc == MAXT_SUCCESS)
{
recCsdb.timeTag = 0;
recCsdb.control = 0;
recCsdb.repeatCount = 1;
recCsdb.data[0] = TX_MSG_LABEL;
recCsdb.data[1] = TX_MSG_SI;
for (i = 2; i < BLOCKCOUNT; i++)
recCsdb.data[i] = (byte)i;
recCsdb.reserved = 0;
Marshal.StructureToPtr(recCsdb, recPtr, false);
rc = mxfCSDBTxPeriodicUpdateMsgWrite(txBuffer[1], 1, recPtr);
}
// Run the scheduler, update records
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Running periodic transmission, please wait...");
// Run the schedule now
rc = mxfTxPeriodicScheduleRun(schedule);
}
// Wait 5 seconds
if (rc == MAXT_SUCCESS)
{
mxfSleep(5000);
rc = mxfTxPeriodicScheduleFree(schedule);
}
if (rc == MAXT_SUCCESS)
{
Console.WriteLine();
Console.WriteLine("Transmission stopped");
}
return rc;
}
//******************************************************************************
// Initialize CSDB protocol
//******************************************************************************
private static UInt32 InitCSDBProtocolHandler(UInt64 server, UInt64 deviceIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
{
// Set the device in CSDB mode
if (attrib == KMXF_CHANNEL_CLASS)
{
value = MXF_CLASS_CSDB;
return Convert.ToUInt32(true);
}
return Convert.ToUInt32(false);
}
}
}
Updated 10/23/2023