admin管理员组文章数量:1410682
I’m working with a 3rd party device that I'm communicating with over a TCP/IP connection. According to the manual I was provided by the vendor, the tool is started when I send to it the RUN command (request message). Immediately upon sending the run command, I receive the response message RUN OK from the tool controller. The tool starts doing its thing as expected. The manual indicates that upon tool completion I should receive two additional responses to the RUN request message that I sent. (EOT - for last dot marked and ENQ - Indicating back to home position). The marking process can take several seconds to complete. Can someone with more experience than I have help me out hear. How do I continue to read the stream unit the EOT and ENQ or NAK responses are received? I can't just read a certain number of bytes. I'm struggling to come up with a way to continue to read the stream until I receive the ENQ or NAK response. Code example would be appreciated.
Marking tool fail response message received
Marking tool responses successfully received The tool manufacture communication protocol manual states that the following:
Please see the code below that demonstrates how I handled receiving the multiple response messages. I would be appreciative if anyone has recommendation on how to do things a better way. Would like to thank Marc. I read the linked articles that helped my grasp some of the basic things I was missing. Here's My code:
#region "wait for test results"
if ((int)cbCommand.SelectedIndex == (int)enum_commands.enum_Run)
{
string CMDResponse1 = string.Empty;
int MarkingResponseBytes = 0;
bool ACK_Received = false; //Acknowledge
bool DC4_Received = false; //Device Control 4
bool ENQ_Received = false; //Enquiry Transmission
bool EOT_Received = false; //End of Transmission
bool NAK_Received = false; //Negative Acknowledge
while (!NAK_Received && !ENQ_Received)
{
Byte[] MarkingResponse = new Byte[256];
MarkingResponseBytes = stream.Read(MarkingResponse, 0, MarkingResponse.Length);
Thread.Sleep(500);
var markresponse = Encoding.ASCII.GetString(MarkingResponse, 0, MarkingResponseBytes);
#region "RUN OK response received"
if (markresponse.Contains("RUN OK"))
{
ACK_Received = true;
rtbResponses.Text = Encoding.ASCII.GetString(MarkingResponse, 0, MarkingResponseBytes);
}
#endregion
#region "P response received"
if (Encoding.ASCII.GetString(MarkingResponse, 0, MarkingResponseBytes).ToLower() == "p")
{
rtbResponses.Text = rtbResponses.Text + "\n" + Encoding.ASCII.GetString(MarkingResponse, 0, MarkingResponseBytes);
}
#endregion
#region "EOT Repsonse Received"
if (markresponse.Contains("\u0004"))
{
EOT_Received = true;
rtbResponses.Text = rtbResponses.Text + "\n" + "End of Transmission (EOT) response received.";
}
#endregion
#region "ENQ Repsonse Received"
if (markresponse.Contains("\u0005"))
{
ENQ_Received = true;
rtbResponses.Text = rtbResponses.Text + "\n" + "Enquiry Transmission (ENQ) response received.";
}
#endregion
#region "DC4 Response Received"
if (markresponse.Contains("\u0014"))
{
DC4_Received = true;
rtbResponses.Text = rtbResponses.Text + "Device Control 4 (DC4) response received.";
}
#endregion
#region "NAK Response Recieved"
if (markresponse.Contains("\u0015"))
{
NAK_Received = true;
rtbResponses.Text = rtbResponses.Text + "\nNegative Acknowledgement (NAK) response received."
+ "\nMarking out of bounds";
}
#endregion
}
}
#endregion
本文标签: cHow do I receive multiple responses to a single TCPIP requestStack Overflow
版权声明:本文标题:c# - How do I receive multiple responses to a single TCPIP request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744910745a2631901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论