Page 1 of 1

interfacing multiview 16 with c#

PostPosted: Mon Oct 14, 2019 9:45 am
by Ali123
Hello, how do I change the channels using c# and what commands options are available, please provide the code if possible

Re: interfacing multiview 16 with c#

PostPosted: Mon Oct 14, 2019 11:14 pm
by Ian Morrish
It uses the same api as the VideoHub.
Easier to just use the Videohub tcp text based protocol to set labels and map inputs to window position.

Sent from my SM-G960F using Tapatalk

Re: interfacing multiview 16 with c#

PostPosted: Sat Oct 19, 2019 4:43 pm
by Ali123
This is what I am using, it sends the preamble(Version) but I am unable to send any commands, it does not respond:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;


namespace Client01
{
class Program
{
static void Main(string[] args)
{

int port = 9990;
string IpAddress = "192.168.50.150";
Socket ClientSocket= new Socket(AddressFamily
.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep= new IPEndPoint(IPAddress.Parse(IpAddress), port);
ClientSocket.Connect(ep);
Console.WriteLine("connected!");
while(true)
{

string messageFromClient= null;
Console.WriteLine("Enter the message");
messageFromClient= Console.ReadLine();
ClientSocket.Send(System.Text.Encoding.ASCII.GetBytes(messageFromClient),0,messageFromClient.Length,SocketFlags.None);

byte[] MsgFromServer= new byte[1024];
int size=ClientSocket.Receive(MsgFromServer);
Console.WriteLine("Server "+System.Text.Encoding.ASCII.GetString(MsgFromServer,0,size));
}
}
}
}

Re: interfacing multiview 16 with c#

PostPosted: Tue Oct 22, 2019 11:18 pm
by Brendan Dower
Shehryar Ali,

Thanks for providing your code. It's necessary to consume the preamble before you start sending commands to the device. I have re-written your code for you so that it consumes this preamble.

Also, it's highly recommended to use the "try", "catch" blocks when working with sockets to ensure that Exceptions are handled correctly. I have separated the program into 2 classes, a 'TCPConnection' class to handle the telnet connection and the Program class.

I have included the code below. It is commented throughout for your convenience.
It has been tested with the Smart Videohub 12G using Visual Studio Code.

All the best,

Brendan
Blackmagic Design Developer Support

Code: Select all
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;


namespace Client01
{

    //Separates the Socket functions into class/object
    class TCPConnection
    {

        //Connect the Socket to use TCP/IP commands - returns the true or false
        public bool Connect(string IpAddress, int port)
        {
            ClientSocket.ReceiveTimeout = 5000;

            try
            {
                IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IpAddress), port);
                ClientSocket.Connect(ep);
                Console.WriteLine("connected!");
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Connecting to Socket: " + e);
                return false;
            }
        }

        //Send any Command to the Device. Returns true or false
        public bool send(string messageFromClient)
        {
            try
            {
                ClientSocket.Send(Encoding.ASCII.GetBytes(messageFromClient),
                            0, messageFromClient.Length, SocketFlags.None);
                ClientSocket.Send(Encoding.ASCII.GetBytes("\n\n"));
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error sending command on Socket:\n" + e);
                return false;
            }
        }

        //Receive bytes until a terminating string (such as "\n\n").
        //Returns the string or "ERROR"
        public string receiveUntil(string terminator)
        {
            string response = null;
            Byte[] buffer = new byte[terminator.Length - 1];

            while (true)
            {
                Array.Clear(buffer, 0, buffer.Length);
                try
                {
                    ClientSocket.Receive(buffer);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error receiving data from socket:\n" + e);
                    response = "ERROR";
                    break;
                }
                response += Encoding.ASCII.GetString(buffer);
                if (response.Contains(terminator))
                    break;
            }

            return response.TrimEnd('\n','\0');
        }

        //New socket Object
        public Socket ClientSocket = new Socket(AddressFamily.InterNetwork,
                                        SocketType.Stream, ProtocolType.Tcp);

    }

    class Program
    {

        //Get the Preamble from the Device - Returns the preamble or "ERROR"
        static string getPreamble(TCPConnection telnet)
        {
            string preamble = telnet.receiveUntil("END PRELUDE:\n\n");
            return preamble;
        }

        //Get the response from your command - Returns the response or "ERROR"
        static string getResponse(TCPConnection telnet)
        {
            string response = telnet.receiveUntil("\n\n");
            return response;
        }

        //Main function - Entry Point to program
        static void Main(string[] args)
        {
            //Create a new Telnet Object
            var telnet = new TCPConnection();

            if (telnet.Connect("192.168.23.125", 9990))
            {
                //Get and print the preamble which is sent on successful connection
                string preamble = getPreamble(telnet);
                Console.WriteLine(preamble + '\n');

                while (true)
                {
                    //User input to send to the device
                    Console.WriteLine("Enter the command");
                    telnet.send(Console.ReadLine());           

                    //Get the ACK or NAK response
                    string ack = getResponse(telnet);
                    Console.WriteLine("Server " + ack + '\n');

                    //If ACK then get the Response from the command
                    if (ack.Contains("ACK"))
                    {
                        string response = getResponse(telnet);
                        Console.WriteLine(response + '\n');
                    }
                }
            }
        }
    }
}