01-13-2015 03:26 PM
Hi,
Am working ona application which receives data from four COM ports simentaniously,i have implemnted four threads to receive each of the channel.
i have a timing requirement for each channel, means i should read each channel at a diffrent interval 10,15,10,25 msec respectively.
what would be the best way to implement the timer for each of the tread independently to make it continuiosly receive in the above mentioned time frame until user stops the application.
Thanks in Advance
01-14-2015 07:23 AM - edited 01-14-2015 07:23 AM
Hello, IVI!
One way you could do this application, is to use CVI timers. You could program your timers individually to be triggered at specific intervals:
http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/uiref/cviprogramming_with_timer_controls/
You can also check some timer sample applications that ship with CVI, to get you started.
Best regards,
- Johannes
01-14-2015 09:45 AM
Hi,
Thanks For you reply,
i came through the function "NewAsyncTimer (1.0, -1, 1, MyTimerCallback, 0)"
so this function seems to start the function call (MyTimerCallback) in the secondary thread once the clock ticks.
so if i create four of above function and assign for four diffrent receive corresponds for each serial channel.
each of the receive should work in diffrent thread aftercorresponding clock ticks?
please correct me if my understanding is wrong.
Thanks In advance
01-15-2015 02:11 AM - edited 01-15-2015 02:12 AM
Async timers run in a separate thread, but all timers share the same thread!
This means that interference may happen between timers, in the worst case (depending on how long takes callback to execute) some timer may not run at all: see this thread for reference.
01-15-2015 06:54 AM
Yes IVI!
What you suggested and what RobertoBozzolo confirmed should be appropriate for your needs.
While normal Timers can leave out laps, if an iteration takes too much, overlapping the following one, if this happens with Asynchronous Timers, the subsequent iteration will not be lost, but will scheduled shortly after.
You can create a single common procedure for the timer data reading, and schedule each asynchronous timer with individual parameters, specific to each one.
Best regards!
- Johannes
01-15-2015 09:58 AM
Thanks,
so even if i create four Ansyc timer callback all four callback will be running in the same thread.
but i have a requirement for two of the callback runs at same time interval (50ms).i need to aquire data from two channels at a rate of 50ms
so i suppose async timer is not a good solution for me?
Thanks in advance
01-15-2015 10:18 AM
Hello IVI,
I think you might have misunderstood what RobertoBozzolo proposed.
An Asynchronous Timer will run in its own thread, independent from your thread pool, but normal Timers run all in the main CVI thread, as D_Biel explains in the post Roberto mentioned.
Here are some more resources, describing asynchronous timers:
Best regards!
- Johannes
01-15-2015 11:37 AM
Hi,
if i create four async timer as below in the aplication,to receive four of my serial channel
ID_async_timer1 = NewAsyncTimer (10.0, -1, 1, MyTimerCallback1, 0);
ID_async_timer 2= NewAsyncTimer (10.0, -1, 1, MyTimerCallback2, 0);
ID_async_timer3 = NewAsyncTimer (10.0, -1, 1, MyTimerCallback3, 0);
ID_async_timer4 = NewAsyncTimer (10.0, -1, 1, MyTimerCallback4, 0);
will all the four timers spawn 4 diffrent thread ?
because irrespective of other channel i have to recive in a channel for every 10 seconds?
Thanks in advance
01-16-2015 02:03 AM
As far as I can understand, all callbacks will be executed in the same thread. This implies that under some circumstances some timer callback can never be executed: callback lenght is crucial here (see D_Biel post and code here in the thread I posted earlier: experiment with the code as is and by running timer2 before timer1).
I can think of a more complex architecture that may be able to sustain your requirements: it won't be trivial to trim it at best but on modern multicore PCs I suppose is the best you can have:
01-16-2015 07:14 AM - edited 01-16-2015 07:14 AM
IVI, I should slightly correct my reply.
So Asynchronous Timers will all be scheduled to run in a common thread. This page states that Windows Asynchronous Timers all share one thread. It's only on RT, that each timer will own its dedicated thread.
So the best solution on Windows, would be to use dedicated threads.
In order to schedule these threads, you can either use the PostDeferredCallToThread method (or even events) described by RobertoBozzolo, or implement your own scheduling mechanism within the thread itself.
Best regards!
- Johannes