MX Foundation 4
relaybox.cs
/*******************************************************************************
//
// File:
// relaybox.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 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
{
static void Main(string[] args)
{
UInt32 rc;
UInt64 server = 0;
UInt64 device = 0;
UInt64 rotarySw = 0, db9 = 0, relay = 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("\nStarting\n");
// Get the device handle
rc = mxfSystemDeviceGet(server, 0, out device);
// Get RelayBox current relays configuration
if (rc == MAXT_SUCCESS)
rc = mxfRelayBoxStatusGet(device, out rotarySw, out db9, IntPtr.Zero, out relay);
// Connect A0-B0, A1-C1, A2-D2, A3-B3-C3, A4-B4-D4, A5-C5-D5, A6-B6-C6-D6
if (rc == MAXT_SUCCESS)
{
Console.WriteLine("Relays currently set to 0x{0:x16}", relay);
if ((rotarySw != 0) || (db9 != 0))
Console.WriteLine("Rotary switch and DB9 configuration will override software configuration");
rc = mxfRelayBoxConfigSet(device, MXF_RELAYBOX_CONFIG_OPT_MASK, 0x0074006A0059007F);
}
// Get RelayBox current relays configuration
if (rc == MAXT_SUCCESS)
rc = mxfRelayBoxStatusGet(device, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out relay);
if (rc == MAXT_SUCCESS)
{
UInt16 i, A, B, C, D;
UInt16 mask;
Console.WriteLine("Relays now set to 0x{0:x16}", relay);
A = (UInt16)(relay & 0xFFFF);
B = (UInt16)((relay >> 16) & 0xFFFF);
C = (UInt16)((relay >> 32) & 0xFFFF);
D = (UInt16)((relay >> 48) & 0xFFFF);
for (i = 0; i < 16; i++)
{
Console.Write("{0:d2}: ", i);
mask = (UInt16)(1 << i);
if ((A & mask) == mask)
Console.Write("A");
if ((B & mask) == mask)
Console.Write("B");
if ((C & mask) == mask)
Console.Write("C");
if ((D & mask) == mask)
Console.Write("D");
if (((A | B | C | D) & mask) == mask)
Console.WriteLine();
else
Console.WriteLine("-");
}
}
// 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;
}
}
}
Updated 10/23/2023