02-08-2021 10:07 AM
Hi! I'm trying to communicate with a device through the COM port, but is experiencing some odd behaviour. I'm using InstallComCallback to catch data from the buffer, but if I use the LWRS_RXFLAG option I get no calls to the callback at all.
My COM port spews data constantly (GPS module).
Minimal example, basically ripped from the "commcallback" example shipped with CVI:
#include <userint.h>
#include <utility.h>
#include <ansi_c.h>
#include <rs232.h>
void CVICALLBACK Event_Char_Detect_Func(int portNo, int eventMask, void *callbackData);
int index2 = 0;
int main()
{
int COM_PORT=6;
int err = 0;
err = OpenComConfig(COM_PORT, "", 9600, 0, 8, 1, 32766, 32766);
fprintf(stdout, "Status of COM port: %d", err);
/* Turn off Hardware handshaking (loopback test will not function with it on) */
SetCTSMode (COM_PORT, LWRS_HWHANDSHAKE_OFF);
/* Make sure Serial buffers are empty */
FlushInQ (COM_PORT);
FlushOutQ (COM_PORT);
/* Install a callback such that if the event character appears at the
receive buffer, our function will be notified. */
InstallComCallback(COM_PORT, LWRS_RXFLAG, 0, 13 , Event_Char_Detect_Func, 0);
RunUserInterface();
return 0;
}
/********************************************************************************************/
/* Event_Char_Detect_Func (): Fuction called when the event character is detected. */
/********************************************************************************************/
void CVICALLBACK Event_Char_Detect_Func(int portNo, int eventMask, void *callbackData)
{
fprintf(stdout, "got message\n");
char readBuf[40000] = {0};
int strLen;
strLen = GetInQLen (portNo);
ComRdTerm(portNo, readBuf, strLen, 13); // 13 = Carriage Return (CR). ComRdTerm automagically removes following Line Feeds (LF).
fprintf(stdout, "%d, In buffer: %d\n%s\n", index2, strLen, readBuf);
index2++;
return;
}
If I exchange LWRS_RXFLAG for LWRS_RXCHAR then the program behaves as expected. I have tried using different eventCharacters, such as '$' and <CR> that I know exist in my data (NMEA string), I have tried giving it the decimal number directly, I have tried doing explicit conversions (e.g. (int)'$'). I have tried giving it them as chars.
Also, since when using LWRS_RXCHAR the line "ComRdTerm(portNo, readBuf, strLen, 13);" successfully identifies 13 as <CR> then I find it odd that InstallComCallBack should not.
So, when using LWRS_RXFLAG I get no calls at all, when I use LWRS_RXCHAR it works perfectly. What am I missing?
Thank you for any insight!
02-12-2021 07:04 AM
Hello,
In your callback fonction, you should use :
if (eventMask & LWRS_RXFLAG)
{
your code...
}
Without this test, the callback is called everytime a character is received.
Regards,
Alain
02-12-2021 07:24 AM
Hi, Alain!
Thank you for your answer, but my problem is that I do not get any calls to the callback at all with the LWRS_RXFLAG. I have a breakpoint at "fprintf(stdout, "got message\n");", but it never gets there unless I change the eventMask I use.
Adding the if-clause did not help since I do not enter the callback function at all...