LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

asyncronous timers not working properly

i am using two asyncronous timers in my application one for sending data other for receiving data
when i press send button then 1st timer starts    
timer1ID = NewAsyncTimer (10/1000, -1, 1, SendData, 0);  // my firsty timer working properly
but when i press receive button then i start my other asyncronous timer    
timer2ID = NewAsyncTimer (10/1000, -1, 1, ReceiveData, 0);  
the functions SendData and ReceiveData are of the type

int CVICALLBACK FunctionName (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2);
but my second timer is not working properly
i mean to say when i place breakpoints in my 2nd asyncronous timer it calls after few seconds which is incorrect
although i configured it with 10/1000 which is 10 millisecond.
please help me best
regards
ahsan

0 Kudos
Message 1 of 6
(4,668 Views)

smartprogrammer ha scritto:

(...)

although i configured it with 10/1000 which is 10 millisecond.


This is definitely not true Smiley Surprised

Since 10 and 1000 are integers, the compiler operates an integer division, which results in 0!

With an interval of 0, the instrument automatically starts the timer with the maximum possible resolution which is given by GetAsyncTimerResolution (). On my system it is 1 msec, on your system it could be different and on the target system of your application it could be different again!

In every case, your asynchronous timers are very likely to run at a different interval from what you expect.

 

To force the compiler to treat values as double you must use decimals: with 10.0/1000 it considers the first one as double and promotes the second to this type; in your case is largely more convenient to directly pass 0.01 as the interval.

 

(For type promotion, see for example here, section 2.8.1 through 2.8.1.4)



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 6
(4,663 Views)

my question is still not answered , my first timer worked perfactly but when i used second timer the same way i created the first then it was not working properly , when i insert break in my second asyncronous timer callback function then it hots the break after very long time i mean 2 to 3 seconds , i dont understad why, i press 1st button and start one timer which is perfact but when after some time i press other button to aquire data it start new asyncronous timer the same way but it is not working like the 1st timer,

is there any issue due to which i am unable to use two asyncronous timer side by side or are there any limitions please share some example in which more than 1 asyncronous timer is used i read a document in which it was written that labwindows can use 16 side by side asyncronous timers.

0 Kudos
Message 3 of 6
(4,652 Views)

That's true: you can run 16 concurrent async timers, but since they share the same thread, they may interfere one with each other: read this thread that discusses strange behaviour of concurrent async timers.

I suppose your first timer works correctly but consumes all available time, so that the second timer hasn't any time to execute: that's why the first time it fires after long time since it's been created.

Supposing timer resolution is 1 msec, if your timer callback takes this exact time or more to complete the first timer fires itself immediately after it has finished executing, thus leaving no timer for the second one to run. Try setting the timer interval to 10 msec and see what happens.



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 6
(4,643 Views)

this issue is strange and very disturbing, i have checked with alot of cases in which i used two asyncronous timers with different intervals , ont timer sends data at serial every 20 milliseconds (that is 0.02 interval) while the other recieves data every 10 millisecond from other serial port, i also display send and receive data, now which ever timer i start first works fine but the other displays data after 5 to 8 seconds this is very poor performance where as labwindows claims the it provides maximum of 16 asyncronous timers which are very accurate as they run in there own thread. so i m really asyncronous timer claims are false.

now the approch i am using is using buttons to call thread i.e

one button calls

CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, DataAcqThreadFunction, NULL, &functionId);

second button calls

CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, DataSendThreadFunction, NULL, &functionId); 

now in  DataAcqThreadFunction and DataSendThreadFunction i use Delay(0.01) to wait for 10 milli second now my question is that is this approach good for multi threading and will these threads be independent of eachother and is this true multi threading approach please answer in detail sir i am waiting for your detail reply, i will be really thankful to you.

thanks alot in advance

ahsan

0 Kudos
Message 5 of 6
(4,617 Views)

Well, I cannot argue exactly on the design since I do no know the exact scenario you are in nor the devices you are using.

 

Just as a suggestion, I wonder whether you can substitute the receiving function with a callback installed on the serial port with InstallComCallback: if you device sends a fixed amount of data or if transmission has a termination code this may improve performance since it avoids having a running thread for that. Ultimately, you could install the callback to fire on every character received, accumulate characters in a queue and handle them when the message is full. Available option to define the com callback are described here; see also this example that shows how to install the com callback in a separate thread.

 

Second issue, using a fixed Delay can be less than optimal if thread execution time can be varying for some reaon; timing the thread can be done with SyncWait, this way:

int CVICALLBACK ThreadFunction (void *functionData)

{
	int		sleepPolicy;
	double	t, told = Timer ();

	// Change thread sleep policy
	sleepPolicy = GetSleepPolicy ();
	SetSleepPolicy (VAL_SLEEP_NONE);

	told = Timer ();

	//----------------------------------------------------------------
	// LOOP
	//----------------------------------------------------------------
Beginning:
	t = Timer ();		// Save time for timing
	tn = t - told;		// Time from last loop
	told = t;			// Save time for calculation

	// Your code here
	// ...

	// Timing at approx 10 msec
	SyncWait (t, 0.008);

	// Keep running until stopped
	if (threadActive) goto Beginning;

	// Restore Sleep Policy
	SetSleepPolicy (sleepPolicy);

	return 0;
}


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 6 of 6
(4,614 Views)