S
Scott
I have to send a TCP stream to a device. The device returns a response and
I cannot figure out how to catch the response. I know the device response
properly because if I connect to it using HyperTerminal I can send the
command and immediately the response is displayed back to me in
HyperTerminal. Now I need to do this programmatically. The send portion of
the code below works because the device response properly but for some
reason I'm unable to catch the response. I've looked through MANY examples
online and they all seem to look like this code block.
Please help, and thanks in advance.
Scott
([email protected])
System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient();
tcpClient.Connect(LED_IP, LED_Port);
System.Net.Sockets.NetworkStream networkStream =
tcpClient.GetStream();
// Does a simple write.
Byte[] sendBytes = Encoding.ASCII.GetBytes(LED_Stream);
Byte[] RecvBytes = new Byte[4096];
networkStream.Write(sendBytes, 0, sendBytes.Length);
String responseData = String.Empty;
Int32 bytes = networkStream.Read(RecvBytes,
RecvBytes.Length, 0);
responseData =
System.Text.Encoding.ASCII.GetString(RecvBytes, 0, bytes);
this.txtResult.Text = bytes.ToString() + ": " +
responseData;
networkStream.Flush();
tcpClient.NoDelay = true;
tcpClient.Close();
tcpClient = null;
networkStream = null;
I cannot figure out how to catch the response. I know the device response
properly because if I connect to it using HyperTerminal I can send the
command and immediately the response is displayed back to me in
HyperTerminal. Now I need to do this programmatically. The send portion of
the code below works because the device response properly but for some
reason I'm unable to catch the response. I've looked through MANY examples
online and they all seem to look like this code block.
Please help, and thanks in advance.
Scott
([email protected])
System.Net.Sockets.TcpClient tcpClient = new System.Net.Sockets.TcpClient();
tcpClient.Connect(LED_IP, LED_Port);
System.Net.Sockets.NetworkStream networkStream =
tcpClient.GetStream();
// Does a simple write.
Byte[] sendBytes = Encoding.ASCII.GetBytes(LED_Stream);
Byte[] RecvBytes = new Byte[4096];
networkStream.Write(sendBytes, 0, sendBytes.Length);
String responseData = String.Empty;
Int32 bytes = networkStream.Read(RecvBytes,
RecvBytes.Length, 0);
responseData =
System.Text.Encoding.ASCII.GetString(RecvBytes, 0, bytes);
this.txtResult.Text = bytes.ToString() + ": " +
responseData;
networkStream.Flush();
tcpClient.NoDelay = true;
tcpClient.Close();
tcpClient = null;
networkStream = null;