LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use asychronous timer Interval based on CVI ?

Hi !

Thank you very much for your reply.

I have been thinking about the problem these two days.But I still have no idea about error checking for communications errors. The situation that the operator possibly stop the running test may exist.The operator may want to terminate the test in advance.

Could you give me some guidance about error checking for communications errors ?

 

Besides,I have to use menu bars because I have several user interfaces to test different parts of the system. I have to simulate several different roles, and I will use Preset Multiple Registers (function code:0x10) and read holding register (function code:0x03) .So I need several flags to send different requests when I switch to different user interface.

 synchronous timer is not accurate.If I set the flags in synchronous timer callback,I'm afraid it will affect the accuracy of the test. I do not want to use several asynchronous timers ,because it will increase my programming difficulty. I want to know how to set the flags using asynchronous timer.If it could not set the flags using asynchronous timer or it would use several asynchronous timer,does the synchronous timer inaccuracy has a lot of impact in my test ?

 

I'm sorry for  troubling you. Thank you very much !

Best regards.

xiepei 

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 11 of 17
(2,373 Views)

Error checking is conceptually simple: you ave to test some error indicators and in case thay are set operate consequently. All functions in RS232 library return a negative value in case of error, so you can test the return value from every time you call a function in that library: what to do in case of errors depends on your application, but usually one wants to stop the running test, recod the event in some log on file or on screen and wanr the operator.

 

The code I attached in my last message shows you how to terminate a test prematurely: have you looked at it?

 

On the other hand, I don't understand why you aren't calculating elapsed time directly while looping inside the threaded function: this will save you the timer function and give you a more direct control over the running process, especially in case you want to have more processes running in parallel.



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?
Message 12 of 17
(2,369 Views)

Hi !

Thank you very much for your reply.

You said all functions in RS232 library return a negative value in case of error,so I can test the return value  every time I call a function.Is the following right?

int status;
status=ComRd (comport, in_data, inqlen);
if(status!=inqlen)
{
   MessagePopup("Error","Read data failed!")
}

 I have looked at the code you attached in the last message . It shows me how to terminate a test prematurely and it also showed me how to terminate the test when it reached to the testing time. When it reaches to the testing time,dcount will reduce to zero.

// New test stop condition
if (!dCount)
{
   ThreadFlag = 0; 
   CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, threadID, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);

   SetAsyncTimerAttribute (TimerID, ASYNC_ATTR_ENABLED, 0); 

  per = (float)errorcount / (float)(rightcount+errorcount);
  SetCtrlVal (panelHandle, PANEL_PER, per);
}

 I do not know how to use

int CVICALLBACK AsyncTimerEventFunction(int reserved,int TimerID,int event,void*callbackData,int eventData1,int evenetData2)

 if I calculate elapsed time directly while looping inside the threaded function.I am also worried about confusing current time and starting time of the test.Current time code and starting time code could not be inside the same function.

What I think is above.I expect you could give me some guidance when you have time.

Thank you very much.

Best regards.

xiepei

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 13 of 17
(2,363 Views)

Hi

I personally would test specifically for status < 0.

 

With reference to calculating elapsed time, I was thinking to something like this:

double  tini;

tini = Timer ();
while (!endThread) {

// Your code here

if (Timer () - tini >= tmax) break; // Time elapsed
}

 

The poor level of precision in this approach (depending on how long is the loop lasting before testing the timer) is negligible in a test that is intended to perform for several days. This way you don't need the timer anymore.



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?
Message 14 of 17
(2,359 Views)

Hi !

Thank you very much for your guidance !

According to your advice about error checking for communications errors,is the following modified  right ?

status=ComRd (comport, in_data, inqlen);
if(status<0)
{
  MessagePopup("Error","Read data failed!")
}

 

 

 

I will add the calculating elapsed time code to my looping inside the threaded function.I will use the following function to show current time and close the asynchronous timer..

int CVICALLBACK AsyncTimerEventFunction(int reserved,int TimerID,int event,void*callbackData,int eventData1,int evenetData2)

 As I need to simulate several different roles,I have three user interfaces to test different parts of the system.I want to test them at the same time.The following is my idea.

I will use three “int ThreadFunction(void *functionData);"  functions and set ThreadFlag1,ThreadFlag2 ,ThreadFlag3 to write my receiving functions.

I will use three synchronous timer callback function to send datas.

I'm not sure whether this will work.

 

Thank you very much.

Best regards.

xiepei 

 

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 15 of 17
(2,343 Views)

Are you using a single Com port or three different ports to connect to the system(s)? If you are using a single port you need to discriminate the unit to communicate to and route received messages to the correct section of code not to mix answers from different units.



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 16 of 17
(2,340 Views)

Hi !

I'm using a single Com port to connect to the systems.I really need to discriminate the unit to communicate to and route received messages to the correct section of code not to mix answera from different units.So I intend to use three synchronous timer to mark different signs to send datas. However,this is only my idea.

I have written a code that reading holding register (function code:0x03) .I use a synchronous timer to send datas and receiving datas in "int ThreadFunction(void1 *functionData)" function.

I wrote a code that Preset Multiple Registers (function code:0x10) in "SStartCallback" function and receiving datas in "int ThreadFunction2(void *functionData)"

funtion.

The attachment 1 is my code.I know there's a lot of improving to do.

In your last message.you mentioned a way that I could not need timer anymore.

double  tini;

tini = Timer ();
while (!endThread) {   // Your code here   if (Timer () - tini >= tmax) break;    // Time elapsed}

 So I removed the asynchronous timer and only ues multithreading because I want to do the tests at the same time .The attachment 2 is the code without asynchronous timer.

Would you have any good idea according to my code ? As I want to do the tests  for several days at the same time .

You could help me when you have time.

Thank you very much.

Best regards.

 

xiepei

  

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
Download All
0 Kudos
Message 17 of 17
(2,327 Views)