MX Foundation 4
relaybox_demo.cs
/*******************************************************************************
//
// File:
// relaybox_demo.cs
//
// Copyright (c) MAX Technologies Inc. 1988-2022, All Rights Reserved.
// CONFIDENTIAL AND PROPRIETARY INFORMATION WHICH IS THE
// PROPERTY OF MAX TECHNOLOGIES INC.
//
// This example shows how to control interactively a RelayBox using Ethernet.
//
// Hardware Requirements:
// - MAXT RelayBox
//
*******************************************************************************/
using System;
using System.Text;
using System.Runtime.InteropServices;
using static MAXT.MXFoundation.mxf;
namespace RelayBox_example
{
public class relaybox_demo
{
public static readonly string[] posName = { "OFF", "A-B", "A-C", "A-D", "B-C", "B-D", "C-D", "OFF" };
static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
UInt64 sw = 0, db9 = 0, soft = 0, relay = 0;
double temperature = 0;
Boolean quit = false;
UInt64 mask = 0;
// Connect to RelayBox
rc = mxfServerConnect("192.168.0.1", "admin", "admin", Convert.ToUInt64(true), out server);
if (rc != MAXT_SUCCESS)
{
Console.Write("Failed to connect; rc=0x{0:x8}", rc);
Console.WriteLine("\n\rPress a key to terminate");
Console.ReadKey();
return;
}
Console.Write("\n\rStarting\n\r");
// Get the device handle
rc = mxfSystemDeviceGet(server, 0, out device);
if (rc == MAXT_SUCCESS)
rc = mxfRelayBoxReset(device);
while ((rc == MAXT_SUCCESS) && !quit)
{
Console.Clear();
rc = mxfRelayBoxStatusGet(device, out sw, out db9, out soft, out relay);
if (rc != MAXT_SUCCESS) break;
rc = mxfRelayBoxTemperatureSensorRead(device, out temperature);
if (rc != MAXT_SUCCESS) break;
Console.WriteLine();
Console.WriteLine("---------");
Console.WriteLine("Temperature : {0,4:F2} C", temperature);
Console.WriteLine("---------");
Console.WriteLine("Rotary Switch : {0} {1}", sw, posName[Math.Min(7, sw)]);
Console.WriteLine("DB9 Input : 0x{0:x2} {1}", db9, posName[Math.Min(7, db9)]);
Console.WriteLine("Software Mask : 0x{0:x16}", soft);
Console.WriteLine();
Console.WriteLine("Relay Mask : 0x{0:x16}", relay);
Console.WriteLine("---------");
Console.WriteLine(" 0 : OFF ");
Console.WriteLine(" 1 : A-B ");
Console.WriteLine(" 2 : A-C ");
Console.WriteLine(" 3 : A-D ");
Console.WriteLine(" 4 : B-C ");
Console.WriteLine(" 5 : B-D ");
Console.WriteLine(" 6 : C-D ");
Console.WriteLine(" m : MASK ");
Console.WriteLine(" q : EXIT ");
Console.WriteLine("---------");
Console.WriteLine(" SELECTION ? ");
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.Key)
{
case ConsoleKey.Q:
quit = true;
break;
case ConsoleKey.M:
mask = getSoftMask("Mask ", 0, MXF_RELAYBOX_CONFIG_MASK, mask);
rc = mxfRelayBoxConfigSet(device, MXF_RELAYBOX_CONFIG_OPT_MASK, mask);
break;
case ConsoleKey.D0:
case ConsoleKey.D1:
case ConsoleKey.D2:
case ConsoleKey.D3:
case ConsoleKey.D4:
case ConsoleKey.D5:
case ConsoleKey.D6:
case ConsoleKey.NumPad0:
case ConsoleKey.NumPad1:
case ConsoleKey.NumPad2:
case ConsoleKey.NumPad3:
case ConsoleKey.NumPad4:
case ConsoleKey.NumPad5:
case ConsoleKey.NumPad6:
rc = mxfRelayBoxConfigSet(device, MXF_RELAYBOX_CONFIG_OPT_POS, UInt64.Parse(key.KeyChar.ToString()));
break;
default:
Console.WriteLine("INVALID SELECTION !!!");
mxfSleep(900);
break;
}
}
mxfSleep(333);
}
// Catch any previous error
if (rc != MAXT_SUCCESS)
{
var errorString = new StringBuilder(200);
if (mxfSystemErrorStringGet(server, rc, (UInt32)errorString.Capacity, errorString) != MAXT_SUCCESS)
{
errorString.Clear();
errorString.Append(string.Format("ERROR # 0x{0:x8}", rc));
}
Console.WriteLine(errorString);
}
Console.WriteLine("\n\rTerminating");
Console.WriteLine("\n\rPress enter to terminate");
Console.ReadKey();
return;
}
private static UInt64 getSoftMask(string text, UInt64 limitLow, UInt64 limitHigh, UInt64 defaultValue)
{
string buffer;
Boolean valid = false;
UInt64 value = 0;
while (!valid)
{
buffer = String.Format("({0:x16}..{1:x16}, [Enter]={2:x16}) in HEX ? ", limitLow, limitHigh, defaultValue);
Console.WriteLine();
Console.Write("{0}{1}", text, buffer);
buffer = Console.ReadLine();
if (buffer.Length > 1)
{
if (!UInt64.TryParse(buffer, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.CurrentCulture, out value))
continue;
}
else
value = defaultValue; /* nothing typed in, use defaultValue */
valid = ((value >= limitLow) && (value <= limitHigh));
}
return value;
}
}
}
Updated 10/23/2023