LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Listen COM port indefinitely while app is running

Solved!
Go to solution


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

 

 

 

 

 

 

 

 

0 Kudos
Message 1 of 5
(3,970 Views)

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.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(3,967 Views)

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

0 Kudos
Message 3 of 5
(3,963 Views)
Solution
Accepted by topic author norak

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.

Message Edited by Roberto Bozzolo on 09-07-2009 01:26 PM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 5
(3,961 Views)

 

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.

 

0 Kudos
Message 5 of 5
(3,936 Views)