0

I'm trying to write a simple UDP transfer program in Labwindows/CVI. The idea is it creates 2 UDP channels, uses one to write data to a port, and the other to receive the data and print it out.

Here's the receiving end:

//Called whenever data arrives on port

int CVICALLBACK udpCallback(unsigned channel, int eventType, int errCode, void *callbackData)
{
    printf("Callback called\n");  

    //Gets the data from port
    readChannel();

    return 0;
}

void createReadChannel()
{
    //Channel for given port, receiving from any IP address 
    CreateUDPChannelConfig(port, UDP_ANY_ADDRESS, 0, NULL, NULL, &readerChannel);  

    //Attach callback to channel (above)
    SetUDPAttribute(readerChannel, ATTR_UDP_CALLBACK, udpCallback);

    printf("Read channel created\n");
}

My main problem is just that when I run it in debug mode, the shown callback function is never called, i.e. "Callback called" is not printed, not is any data stored or printed in the resulting readChannel() call.

However, when compiled and executed as an .exe, it works as intended. Every time data is received on that port the callback executes.

What difference could there be between the debug and 'release' version that would cause this to happen?

EDIT: After much testing I believe it has to do with waiting for messages using functions like getchar() which caused the main thread to hang up. Why it worked in release mode I don't know, but it probably has something to do with the difference in output window(?). My solution was to remove the callbacks and run the receiving channel on it's own thread. This way the thread is always waiting for a message, using:

UDPRead(readerChannel, 0, 0, UDP_WAIT_FOREVER, NULL, NULL)) < 0)

And then my main thread can pick up messages as needed. If anyone has any additional info let me know.

FreshWaterTaffy
  • 270
  • 1
  • 2
  • 18
  • How do you run the "debug mode"? Step-by step or just free-run? – Eugene Sh. Mar 15 '16 at 17:05
  • Free run I guess, if by that you mean no breakpoints. – FreshWaterTaffy Mar 15 '16 at 17:14
  • 2
    As you pointed out, using `getchar()` is problematic as it is a blocking call, and will stop execution flow until it sees activity on stdin. Aside from this, the differences you are seeing between debug mode and release mode executions will in part be due to the 10x reduction in execution speed for debug mode. It can be even slower if you have resource tracking turned on, or an excessive number of watch variables selected. Reductions in execution speed, such as these, can affect, or completely render useless, time sensitive operations, such as communication routines, – ryyker Aug 09 '16 at 13:14
  • @ryyker Bit of an old thread, but yes I think you're right. What I did was just separate the operations onto different threads so they didn't interfere. Thanks! – FreshWaterTaffy Aug 09 '16 at 14:04
  • LOL - yes, it is an old thread. I look around the CVI area once in awhile just to see what's happening. ...Separate threads is a good approach. – ryyker Aug 09 '16 at 16:07

0 Answers0