Regarding read write threads of a serial port in vc++

Regarding read write threads of a serial port in vc++

Post by cdeepa1 » Sat, 14 Oct 2006 15:00:53


Hai,
I am new to vc++.I am developing an application.In that I have to
write a command to the serial port and I will be getting
acknowledgments for all the commands and in addition to ack I will be
getting som responses from the other side of the port in which case I
have to write back the ack.

For this need I have two threads running.One thread-write only will be
writing the command to the port,other thread-read-writewill be reading
the port and processing and if needed writes back the ack.

In had a sleep of 1 in the second thread where both read-write is
combined.Actually when the other side sends back the response,it is not
getting the ack from my side within 250 ms with 9600 baudrate and it is
timing out and starts retransmitting for 3 times.But in the mean time
my read operation is going on and i am sending back the ack.In this
case where my retransmission from the other side happens,when the write
thread sends next command followed by the read-write thread expecting
the ack from the other side and is reading the previous retransmitted
data and not the present ack.
My Question is
1)Why my read-write thread is taking so much time to send back ack
2)Do I need to implement any semaphore in synchronization of the
threads.

I tried with removing the sleep in the read-write thread.But it is
sending the ack back before the 250ms.I know this will create problem
since my both threads will be running and create problem.But somehow
there isno problem.But how?
Can anyone clarify me and give the proper solution
 
 
 

Regarding read write threads of a serial port in vc++

Post by Rajan » Thu, 19 Oct 2006 15:36:20

Hi ,
From your problem statement i understood the following:
"u have created 2 threads - One for Writing into seriel port and
another for Read the Ack from SPort and Write the Ack again to SP"

It seems the 2nd Thread u r invoking is taking time due to sleep.
That's why it doesn't act smartly to read the Ack.
-> Here you have to estimate the time value used in Sleep(). Remember
the value should be in miliseconds.

it is better to use WaitForComEvent() instead of Sleep(). This call
will invoke ReadWrite thread immediately, when it got Ack from SPort.
In the invoke procedure(function) you could Read the Ack and write back
the Ack.
For more info: MSDN - Serial communication API's

Please, be clear in your problem statement. And write me back if u need
any help

regards,
Rajan