02-24-2021 07:03 AM
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,
Solved! Go to Solution.
02-24-2021 03:45 PM
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:
02-25-2021 06:42 AM
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);
02-25-2021 07:55 AM
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!
02-25-2021 08:03 AM
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.
02-25-2021 08:20 AM
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.
02-25-2021 08:38 AM
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
02-25-2021 09:28 AM
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.
02-25-2021 09:46 AM
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?
02-25-2021 10:31 AM
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?