09-07-2009 05:42 AM
Hi,
My app supports multiple threads,
One COM port must have a listen thread, that has to listen indefinitely
while app runs and until app ends.
The program uses others COM Ports (so timeout is set to 10s to avoid
communications problems with this other ports)
the pseudocode is as follow:
int main()
{
CmtNewThreadPool(1);
CmtScheduleThreadPoolFunction(Listen);
// continue executing code (new threads) while listening to the COM port
// end of app
// finish to Listen to the COM port
CmtDiscardThreadPool();
}
int Listen()
{
ComRd()
}
The problem is that if Timeout is not set to infinite
ComRd() times out after 10s so the program finish to Listen the Com Port
before the app ends.
Perhaps it runs if The ComRd function is inside a do while() loop
until main program ends (I think is not the best solution because
it is run a ComRd function every 10s ...)
Any ideas?
thanks a lot
Solved! Go to Solution.
09-07-2009 05:48 AM
I suggest you to use GetInQLen (port) to test wether there are characters to read or not: only in this case you can read from the port immediately avoiding timeout problems. You could consider to extend this method to all other threads / sections of code where you are communicatinog over the serial line so that the code is more responsive as it has no timeout periods pending.
09-07-2009 06:10 AM
ok,
I see the idea,
this suggestion will improve my code, but i am in the same previous question.
Do you think the method I mentioned above is a good way (the only way?) to listen indefinitely a serial port while app is running?
that is, remain in Listen function while app is running?
thanks
09-07-2009 06:23 AM - edited 09-07-2009 06:26 AM
Well, having spawned a separate thread for this yes, you must loop indefinitely in that thread listening for port activity. The system is in charge of letting some processor time to the other threads but you can consider to slow down this thread execution since you possibly do not need such a fast port scanning.
You could consider for example to use an asyncronous timer instead of a thread: asyncronous timers are built on top of multimedia timers and run in a separate thread (a single thread for all async timers); this is usually the easiest way to develop an application that must periodically execute some function: in your case the timer callback could call GetInQLen and in case read the port, handling the message read in a proper way. This task could run 5 to 10 times per second (or possiby even at lower frequency), thus leaving the majority of execution time and resources to your other threads. If you run this way, please keep in mind that you are still running a multi-threaded application, so you must use all the available methods to protect your data from concurrent acces from more threads the same way you are surely doing it actually.
09-08-2009 04:27 AM
Thanks Roberto,
Your way is easier and it looks to be enough for my app.
Pd, thanks for your advise about management data with multithreading.