LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

While loop with toogle button

Solved!
Go to solution

Hi,

 

I am trying to programm a while loop having as a condition the state of a toggle button.

This is what i have done:

 

int CVICALLBACK ReadComPort (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{

 

      switch (event)
      {
         case EVENT_COMMIT:

                  GetCtrlVal(apipanel,ApiPanel_ReadPortButton,&ReadButtonState);
                  while (ReadButtonState)
                  {

                       bytesRead = ComRdTerm (OpenedComPort, smRespuesta, 99, 10);

                  }

          break;
        }
       return 0;
}

 

The problem is that the program once it enters the while does not respond to the button, I can not disable it.

 

I hope you understand me,

 

Thank you,

0 Kudos
Message 1 of 15
(1,923 Views)

Yes, all loops (while, for, do) are blocking on that thread, which means that program flow will stop until the loop breaks.  A way to 

 

What kind of control is bound to your callback function ReadComPort?

 

What kind of control is your ApiPanel_ReadPortButton?  I'm assuming this is a Command (momentary) button?

 

How is this program supposed to flow?  If you click the Command button, an instrument starts sending serial data?

 

A better, non-blocking approach is to use InstallComCallback on your serial port.  Then, it will fire when you get data.  There are really excellent example projects in the Help file:

 

ElectroLund_0-1614203131147.png

 

 

0 Kudos
Message 2 of 15
(1,890 Views)

Hi, first of all, thank you for answering.

 

My ApiPanel_ReadPortButton is a toggle button. Clicking this button displays the information written by an instrument in that port. 

I tried using InstallComCallback but using it delays the program and I don't know why.

I have done this:

 

void ComCallback(int portNumber, int eventMask, void *callbackdata)
{
   char smRespuesta[1000], tboxReadData[1000];
   int bytesRead;

   if (LWRS_RXFLAG)
   {
       bytesRead = ComRd (OpenedComPort, smRespuesta, 50);
       CopyString (tboxReadData, 0, smRespuesta, 0, bytesRead);
       SetCtrlVal (apipanel, ApiPanel_ReadComPort, tboxReadData);
   }
}

 

InstallComCallback(OpenedComPort, LWRS_RXFLAG, 0, 13, ComCallback, NULL);

0 Kudos
Message 3 of 15
(1,881 Views)

You say that the installed callback "delays the program".  What does that mean exactly?  Have you debugged this program?  Set a breakpoint in the com callback.  Does your program ever enter here?  If so, what's the value of bytesRead?  If negative, you have a ComRd error.  Do you get any data in smRespuesta?  Is it what you expect?

 

There is a lot of info you could be answering yourself with the debugger.  Use it and report back!

0 Kudos
Message 4 of 15
(1,876 Views)

Yes, I debugged the program and smRespuesta takes the value I want. But the problem is that it takes a few seconds for the program to print what it receives through the port. I sent a command and the response instead of appearing immediately takes several seconds.

 

I hope you got it.

0 Kudos
Message 5 of 15
(1,876 Views)

Well perhaps that's an issue with your instrument?  Have you worked with the instrument with a terminal emulator instead to verify?  That is always helpful too, to verify packet  formatting.

0 Kudos
Message 6 of 15
(1,871 Views)

I don't know how but I fixed the problem. Now it works fine but I still have a problem. The program only display part of the response if this one is quite long.

 

Imagine that the result is:

sm
Current time: 02/25/21 15:26:43 ASN: 594468
Elapsed time: 0 days, 01:39:15
MAC MoteID Age Jn UpTime Fr Nbrs Links State
00-17-0D-00-00-21-BB-92 ap 1 1 01:39:04 5 0 25 Oper

 

Well, it only displays:

sm
Current time: 02/25/21 15:26:43 ASN: 594468

 

 And if I click again then:

Elapsed time: 0 days, 01:39:15
MAC MoteID Age Jn UpTime Fr Nbrs Links State
00-17-0D-00-00-21-BB-92 ap 1 1 01:39:04 5 0 25 Oper

0 Kudos
Message 7 of 15
(1,867 Views)

That's because your received packet...

 

sm
Current time: 02/25/21 15:26:43 ASN: 594468

 

Is precisely 49 characters, including \r and \n line terminations.  Since your character buffer must include the terminating nul, that brings you to 50, which is exactly what you told ComRd to do.

0 Kudos
Message 8 of 15
(1,857 Views)

It's true. I switched to 500 and the delay has reappeared. It seems that if you get the whole answer, it doesn't show it right away. But if you get only a portion of it, as in the previous case, it works well. Why is this happening?

0 Kudos
Message 9 of 15
(1,854 Views)

Just a note here: I see that in ReadComPort () function you use 10 as the terminator, while in the com callback you have set it to 13. Could this explain why you have a delay, possibly due to a timeout in ComRd where you have the wrong terminator?



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 10 of 15
(1,847 Views)