我正在尝试使用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来发送和接收字符串无法成功。我的程序完全没有任何异常(除了Networkstream读取延迟30秒)。