2

I have an optoma projector and I want to communicate to it using Telnet session from inside the C# program. Using various wrappers and Nuget packages, I am able to start the telnet session but I am not abe to communicate commands or receive response in my C# program.

In a normal windows cmd, if I write Telnet 192.168.222.127, telnet session begins. The projector responds to commands in ASCII format(https://www.optoma.de/uploads/RS232/DS309-RS232-en.pdf) and it returns 'F' or 'P' based on if command Failed or passed. To change brightness to 10, I would do something like this:

  1. open a Telnet Connection : telnet 192.168.222.127
  2. Send a command to change brightness to 10 : ~0021 10
  3. Session will respond with a 'P' as the command passed in changing brightness. Telnet Terminal Commands

I want to do the same with C# program but I am stuck. Most of the answers point to outdated packages and link. I am new to Telnet and communication protocols and need help regarding this. Thank you.

  • 1
    just use a [Telnet library](https://stackoverflow.com/questions/390188/c-sharp-telnet-library). – Andy Aug 16 '21 at 22:18
  • Or if you are feeling adventurous, just use a vanilla every day *Tim Shaw Demtel* socket – TheGeneral Aug 17 '21 at 01:46
  • https://stackoverflow.com/questions/390188/c-sharp-telnet-library - has some lib suggestions (off topic for SO) and at least one answer with some rough code for poking a telnet server – Caius Jard Aug 17 '21 at 05:56
  • I tried different telnet libraries and with all of them I was able to connect to the projector. The problem was I was sending the ascii command as "~0000 1\n". As soon as I changed it to "~0000 1\r\n", commands were understood by the projector. It might be an obvious thing but I didn't know it until I tried that. – Sarvesh Thakur Aug 18 '21 at 00:33

1 Answers1

2

If you simply want to connect to the device, send in the command and read the response, simple C# TcpClient is enough. It provides client connections for TCP network services which was just right for my case.

For establishing connection :

using system.net.sockets;

    public void EstablishConnection(string ip_address, int port_number=23){
        try
        {
            client = new TcpClient(ip_address, port_number);
            if (DEBUG) Console.WriteLine("[Communication] : [EstablishConnection] : Success connecting to : {0}, port: {1}", ip_address, port_number);
            stream = client.GetStream();
        }
        catch
        {
            Console.WriteLine("[Communication] : [EstablishConnection] : Failed while connecting to : {0}, port: {1}", ip_address, port_number);
            System.Environment.Exit(1);
        }
    }

For sending and receiving response :

    public string SendMessage(string command)
    {
        // Send command
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(command);
        stream.Write(data, 0, data.Length);
        if (DEBUG) Console.Write("Sent : {0}", command);

        WaitShort();

        // Receive response
        string response = ReadMessage();
        if (DEBUG) Console.WriteLine("Received : {0}", response);

        return response;
    }

    public string ReadMessage()
    {
        // Receive response
        Byte[] responseData = new byte[256];
        Int32 numberOfBytesRead = stream.Read(responseData, 0, responseData.Length);
        string response = System.Text.Encoding.ASCII.GetString(responseData, 0, numberOfBytesRead);
        response = ParseData(response);
        if (response == "SEND_COMMAND_AGAIN")
        {
            if (DEBUG) Console.WriteLine("[ReadMessage] : Error Retreiving data. Send command again.");
        }
        return response;
    }

You may parse the response from the server based on your requirements.