MX Foundation 4
discrete_pulse_clock.cs
/*****************************************************************************
//
// File:
// discrete_pulse_clock.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 pulse mode of discrete
// for transmission.
//
// Hardware requirements:
// - MAXT Flex.
//
*****************************************************************************/
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace discrete_example
{
class discrete_pulse_clock
{
const int MAX_TX_RECORDS_TO_TRANSMIT = 1;
static void Main(string[] args)
{
UInt32 rc;
UInt64 server;
UInt64 device = 0;
var module = new UInt64[1];
var txChannel = new UInt64[1];
UInt64 txBuffer = 0;
var rec = new MXF_DISCRETE_DATAREC();
IntPtr recPtr = IntPtr.Zero;
UInt64 moduleCount = 0;
UInt64 channelCount = 0;
UInt64 txBufferSize = 0;
IntPtr txHostBuffer = IntPtr.Zero;
UInt64 data;
// Connect to services and initialize environment
#if LOCAL
rc = mxfServerConnect("0.0.0.0", "", "", 0, out server);
#else
rc = mxfServerConnect("192.168.0.1", "admin", "admin", 0, out server);
#endif
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:X8}", rc);
Console.ReadKey();
return;
}
// Initializes MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n");
rc = mxfSystemInit(server);
}
// Gets the device handle
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceGet(server, 0, out device);
// Gets handle of first DIO_eh module
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device, MXF_MODULE_DIOFIFO_EH, 1, out moduleCount, module);
// Gets handle of first pulse TX channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, txChannel);
// If channel not found, return an error
if (rc == MAXT_SUCCESS && channelCount == 0)
rc = MAXT_ERROR_NOT_FOUND;
// Allocate buffer for tx data
if (rc == MAXT_SUCCESS)
{
txBufferSize = (UInt64)(MAX_TX_RECORDS_TO_TRANSMIT * Marshal.SizeOf(typeof(MXF_DISCRETE_DATAREC)));
// 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;
}
}
}
// Sets timebase to RTC nsec
if (rc == MAXT_SUCCESS)
rc = mxfSystemTimeBaseSet(server, MXF_TIMEBASE_DEVICE_NSEC);
if (rc == MAXT_SUCCESS)
{
// Prepares records
recPtr = txHostBuffer;
for (data = 0; data < MAX_TX_RECORDS_TO_TRANSMIT; data++)
{
rec.timeTag = 0;
rec.control = MXF_DISCRETE_TX_REC_CTRL_PULSE_START;
rec.repeatCount = 1;
rec.data = 0x0000;
rec.edge = 0x0003;
rec.highDuration = (UInt32)(200000000); //LEDs 0 and 1 stay on for 2 seconds
rec.lowDuration = (UInt32)(100000000); //LEDs 0 and 1 stay off for 1 second
Marshal.StructureToPtr(rec, recPtr, false);
rc = mxfDiscreteNextDataRecordPtrGet(recPtr, out recPtr);
}
}
if (rc == MAXT_SUCCESS)
{
Console.Write("Transmitting ...\n");
rc = mxfDiscreteTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, MAX_TX_RECORDS_TO_TRANSMIT, txHostBuffer);
}
if (rc == MAXT_SUCCESS)
{
mxfSleep(10000);
}
// Stop pulse
if (rc == MAXT_SUCCESS)
{
rec = new MXF_DISCRETE_DATAREC();
// Prepares record
recPtr = txHostBuffer;
rec.timeTag = 0;
rec.control = 0;
rec.repeatCount = 1;
rec.data = 0x0000;
rec.edge = 0x0003;
rec.highDuration = 0;
rec.lowDuration = 0;
Marshal.StructureToPtr(rec, recPtr, true);
rc = mxfDiscreteTxAperiodicWrite(txBuffer, MXF_TXAPERIODIC_FLAG_DEFAULT, 0, 1, txHostBuffer);
}
// Frees device and host buffers
if (txBuffer > 0)
if (txHostBuffer != IntPtr.Zero)
Marshal.FreeHGlobal(txHostBuffer);
if (rc != MAXT_SUCCESS)
{
StringBuilder errorString = new StringBuilder(256);
rc = mxfSystemErrorStringGet(server, rc, (UInt32)errorString.Length, errorString);
Console.WriteLine(errorString + "\n");
}
Console.Write("Terminating ...\n");
// Unloads MX Foundation library
// Disconnects from MX Foundation library
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
}
}
Updated 10/23/2023