0

我正在尝试使用 TCP/IP 从 PC 连接机器人控制器。机器人控制器仅接受来自 PC(TCP 客户端)的 ASCII 字符串数据。根据机器人控制器的命令结构,我必须发送一个特定的字符串并从中获取 ACK 才能获得访问权限。我使用了以下代码

try
{
    System.Net.IPAddress IPADD = System.Net.IPAddress.Parse("192.168.255.2");
    int PortNo = 80;
    char[] ok = new char[33];
    byte[] Data = new byte[33];
    byte[] StartReq = new byte[21];
    String Start = "CONNECT Robot_access";


    // INITIALIZING TCP CLIENT
    TcpClient TCP = new TcpClient();
    Console.WriteLine("Connecting..........");
    TCP.Connect(IPADD, PortNo);
    Console.WriteLine("Connected");
    NetworkStream NS = TCP.GetStream();

    // START REQUEST

    StartReq = Encoding.ASCII.GetBytes(Start);
    NS.Write(StartReq, 0, StartReq.Length);
    Console.WriteLine("start request send..........");


    // RECEIVE ACK FOR ROBOT ACCESS
    Int32 RespData = NS.Read(Data, 0, Data.Length);
    string ACK = Encoding.ASCII.GetString(Data, 0, RespData);

    Console.WriteLine("The  ACK is {0} ", ACK);
    Console.WriteLine("ACK Received");


    Console.Read();
}
catch (Exception e)
{
    Console.WriteLine(e.StackTrace);
    Console.Read();
}

我也尝试过 Streamwriter 和 Bufferstream 来发送和接收字符串无法成功。我的程序毫无例外地完全运行(网络流读取延迟 30 秒除外)。

4

0 回答 0