简体   繁体   English

通过Telnet连接到串行 - >以太网转换器盒,得到奇怪的响应(无论我发送什么)

[英]Connecting to Serial->Ethernet converter box via Telnet, getting strange response (no matter what I send)

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. 我正在尝试与具有串行COM端口的设备通信(使用RS232协议),我已经连接到转换器盒,将连接转换为以太网。

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. 我已成功连接到转换器盒(它有自己的IP),我已成功通过使用PuTTY通过telnet发送命令与设备通信,它已经响应了我已经能够使用的各种数据解析。

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 "??\" which I've researched and found that \\uhhhh is a Unicode escape protocol. 我通过创建一个新的TcpClient建立了一个连接,并且我能够将字符串发送到设备,但每次我得到一个响应,它总是“?? \\ u0003”我已经研究过并发现它uhhhh是一个Unicode转义协议。 This confuses me because I have used ASCII encoding for everything. 这让我感到困惑,因为我已经使用了ASCII编码。

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 ??\) 如果我传递“$ TEAA4B9 \\ r \\ n”作为消息,我会期待“$ TEA,086,040,031,000,3798”的内容,这与我所得到的无关(这是?? U0003)

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) 这就是我所做的:我在底部的“tcpStream.Read”行之前添加了一个“System.Threading.Thread.Sleep(”1000“),现在它输出了我需要的数据。设备在第一次输出垃圾line(也许是一次握手,不确定),这是在将它存储到“message”并返回之前所读取的所有内容(没有足够的时间用于读取它之前移动到下一行代码)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM