MX Foundation 4
discrete.cs
/******************************************************************************
//
// File:
// discrete.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 read discrete input and to write to
// discrete output using direct access.
//
// Hardware requirements:
// - MAXT Flex.
//
*******************************************************************************/
#define LOOPBACK
//#define LOCAL
using System;
using static MAXT.MXFoundation.mxf;
using System.Text;
namespace discrete_example
{
class discrete
{
static void Main(string[] args)
{
UInt32 rc;
UInt64 deviceCount;
UInt64 moduleCount;
UInt64 channelCount;
UInt64 server;
var device = new UInt64[1];
var module = new UInt64[1];
var rx = new UInt64[1];
var tx = new UInt64[1];
UInt64 i;
UInt64 state;
StringBuilder errorString = new StringBuilder();
// 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
// Initialize MX Foundation library
if (rc == MAXT_SUCCESS)
{
Console.Write("Starting ...\n");
rc = mxfSystemInit(server);
}
// Get handle of discrete input channel
if (rc == MAXT_SUCCESS)
rc = mxfSystemDeviceAllGet(server, MXF_DEVICE_ALL, 1, out deviceCount, device);
if (rc == MAXT_SUCCESS)
rc = mxfDeviceModuleAllGet(device[0], MXF_MODULE_DIOFIFO_EH, 1, out moduleCount, module);
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_RX_CHANNEL, 1, out channelCount, rx);
// Get handle of discrete output channel
if (rc == MAXT_SUCCESS)
rc = mxfModuleChannelAllGet(module[0], MXF_CLASS_DISCRETE, MXF_SCLASS_TX_CHANNEL, 1, out channelCount, tx);
// Read value of input pin #1 and set it to output pin #2 for about 10 seconds
for (i = 0; i < 10000 && rc == MAXT_SUCCESS; i++)
{
rc = mxfDiscreteChannelRead(rx[0], 0x02, out state);
state <<= 1; // to be aligned with mask 0x04
if (rc == MAXT_SUCCESS)
rc = mxfDiscreteChannelWrite(tx[0], 0x04, state);
}
if (rc != MAXT_SUCCESS)
{
if (mxfSystemErrorStringGet(server, rc, (UInt32)errorString.Length, errorString) > 0)
Console.Write(" {0} ERROR # 0x{1:X8}", errorString, rc);
Console.Write("{0}\n\r", errorString);
}
// Unload MX Foundation library
Console.Write("\nPress a key to terminate\n");
Console.ReadKey();
return;
}
}
}
Updated 10/23/2023