Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Working on Net.Sockets , i have create win form to connect medical equipment, connection is working good also i am able to send data to machine, but i am facing problem in receive "ACK" from machine, i don't no how to wait until machine send Acknowledgement, so i can send other data

Can you please give me code to connect machine send data and received data

1 solution

You don't need to do anything special to wait.

Just read what is expected from the socket (or the network stream, if you use TcpClient, which I would recommend). The reading methods behave as blocking method calls. When the data is not yet sent by the other side, your calling thread will be blocked at this call until the data arrives; and the thread will be put in a special wait state where it does not waste any CPU time. The thread will be switched off and not scheduled back to execution until it is waken up. It will be waken up when the data arrives, or on some other events, notable timeout or thread abort.

More exactly, you should better explicitly define some application-layer protocol (perhaps define it based on some documentation on your medical equipment) and implement it. Many developers implement actually working network solution without realizing that this is a protocol, but it's better to realize it and define explicitly. Then your application simply writes and reads the data according to the protocol, and gets blocked if the other side is not yet ready. Of course, this applies to the application-layer protocol on top of a session-oriented transport protocol, such as TCP. See also:[^],[^],[^].

You may say that it would render UI unresponsive. Of course it would if you use just the UI thread. But that said, you should never use the UI thread for any activity related to communications with any external devices. You should always create some separate thread and dedicate them to communication. The the UI should work with your communications based on push technology: your communication thread(s) should notify the UI when it is required. It is done using the Invoke or BeginInvoke methods of Control or Dispatcher. (This Dispatcher is a WPF feature, but this is one of the rare WPF features which can be directly used in a System.Windows.Forms UI as well; this fact is not very well known.) Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

For further considerations related to socket-based communications and threads, please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^],
How Do I Get To Know If A A Tcp Connection Is Over[^].

See also my past answer related to push technology and threading: Application 'dashboard' for website accounts[^].

Share this answer
rajan K 20-Jun-15 2:38am    
Hi SA, Thanque for Solution,

Can you please give me some small code ,

This is my String Which i m passing to machine. like this type of sting i want to keep sending , but every string i need to wait until, machine send ACK,

Dim PatData As String
Dim FMENo As String = 1

Dim Header As String = "1H|\^&|||ASTM-Host|||||PSM||P||20000219111500" + CR
Dim Patient As String = "P|1|923501|4637463G66||Smith^John||19630101|M||||||||||Diabetes|||||20000218102000||||||||||Urology" + CR
Dim Order As String = "O|1|923502||^^^104\^^^105\^^^106\^^^107|||||||A||||||||||||||O" + CR
Dim EOL As String = "L|1|F" + CR
Dim input As String = Header + Patient + Order + EOL + ETX
Dim CheckSum As String = GetCheckSum1(input)

PatData = STX + Header + Patient + Order + EOL + ETX + CheckSum + CR + LF

rajan K 20-Jun-15 4:14am    
I am Working on Net.Sockets , i have create win form to connect medical equipment, connection is working good also i am able to send data to machine,

My Process is like this
1. i send Equerry (ENQ) to machien and wait for Acknowledgment (ACK) once machine send ACK i want to start my next process . now i am facing problem that once i send Equerry to machine that process come out from send event how i can keep wait until machine send ACK and then make my next process

Please help me

Can you please give me code to connect machine send data and received data
Sergey Alexandrovich Kryukov 20-Jun-15 8:40am    
You don't need code, you need to finally understand what to do. ACK is a part of your protocol. First, send what you need to send and then read 1 byte immediately and check if it is ACK or not. This thread will be put to wait state at the reading call, until this byte is ready.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900