0

I'm trying to communicate with a device that has a Serial COM port that goes out (using RS232 Protocol), which I've hooked up to a converter box that converts the connection to Ethernet.

I've successfully connected to the converter box (which has its own IP) and I've successfully communicated with the device by sending it commands over telnet by using PuTTY, to which it has responded with various data that I've been able to parse.

I've established a connection by creating a new TcpClient, and I'm able to send strings to the device, but every time I get a response back, it's always "??\u0003" which I've researched and found that \uhhhh is a Unicode escape protocol. This confuses me because I have used ASCII encoding for everything.

public string TcpConnect(string cmd)
{
        var client = new TcpClient();
        //cmd = "ping";

        Console.WriteLine("Connecting to server");

        client.Connect("169.254.20.40", 23);   //22 = ssh, 23 = telnet, 80 = http

        Console.WriteLine("CONNECTED SUCCESSFULLY");

        Stream tcpStream = client.GetStream();

        ASCIIEncoding A = new ASCIIEncoding();
        byte[] enable = A.GetBytes("enable" + Environment.NewLine);
        byte[] connect = A.GetBytes("connect line 1" + Environment.NewLine);
        byte[] ba = A.GetBytes(cmd);

        Console.WriteLine("Transmitting.....");

        tcpStream.Write(enable, 0, enable.Length);
        tcpStream.Write(connect, 0, connect.Length);
        tcpStream.Write(ba, 0, ba.Length);

        byte[] responseBytes = new byte[4096];

        int numBytesRead = tcpStream.Read(responseBytes, 0, responseBytes.Length);
        var message = A.GetString(responseBytes, 0, numBytesRead);


        Console.WriteLine("\nServer Closed.");

        return message;
}

If I were to pass in "$TEAA4B9\r\n" as the message, I would expect something along the lines of "$TEA,086,040,031,000,3798" which is nowhere close to what I'm getting (which is ??\u0003)

trotunno
  • 1
  • 1
  • Where are you expecting to see this "$TEA,086,040,031,000,3798"? In the device or in the Converter or in the response back from the device to the client? – shobhonk Jun 07 '19 at 01:16
  • Figured it out! I added a "System.Threading.Thread.Sleep("1000") before the "tcpStream.Read" line, and now it outputs the data I need. The device was outputting garbage on the first line, and that's all that was being read before it was being returned (not enough time was spent reading before it moved on to the next line of code) – trotunno Jun 07 '19 at 15:09

1 Answers1

0

Nevermind figured it out

Just kidding! Here's what I did: I added a "System.Threading.Thread.Sleep("1000") before the "tcpStream.Read" line at the bottom, and now it outputs the data I need. The device was outputting garbage on the first line (perhaps a handshake, not sure) and that's all that was being read before it was being stored into "message" and returned (not enough time was spent reading before it moved on to the next line of code)

trotunno
  • 1
  • 1