LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get latest published values using UDP or TCP?

I'm currently using a cRIO to sample from some sensors and control the motors on a robot. An algorithm that uses sensor readings to form motor commands is being run on a different computer and I would like the cRIO and the remote computer to exchange information using UDP or TCP.

 

The cRIO samples data very quickly, whereas the algorithm on the computer only needs sensor readings every ms or so. Previously I was throwing all the sensor readings into a shared variable on the cRIO and then the remote computer would just grab the latest published value from the shared variable whenever it needed it. I overhauled the system to use UDP instead of shared variables so that I could interface with 3rd party applications. However, now all the values that the cRIO sends via UDP gets backed up in some sort of a buffer.

 

So my question is, is there any way to just get the latest published value when I read from a UDP socket? I get the impression that there is from the this whitepaper (http://www.ni.com/white-paper/12079/en) where they say, "Process-data communication is only interested in the latest value. Therefore, guaranteed delivery and transfer rates are not as important. [...] TCP/UDP should be used if the data will be monitored by 3rd party applications."

 

Thanks for your help!

Best,
Matt

0 Kudos
Message 1 of 5
(2,384 Views)

The only way you can get to the latest data on a UDP or TCP connection is to read all and discard the earlier data. The communication stacks will buffer the data until read. There is no way for them to act like a notifier where the new data pushes the old data out. I suggest you have a simple task that simply reads the data from the connection and posts it to a notifier. Where you use the data grab it from the notifier.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 2 of 5
(2,372 Views)

"I suggest you have a simple task that simply reads the data from the connection and posts it to a notifier."

 

But, this has to run on my laptop in a loop that's at least as fast as the loop on the cRIO. This just seems like a lot of resources dedicated to soaking up unnecessary data. And the algorithm itself is already pushing the limits of my computer. Is there a better way for getting "latest published data" over a network? For instance, if I sent a message to the cRIO saying "I want the data now" and then only then does the cRIO send out a command? Is this normal? Again, it just seems like unnecessary communication that would slow everything down.

 

Thanks again.

0 Kudos
Message 3 of 5
(2,366 Views)

You could do a command and response method to query for the latest data. If you want the cRIO to broadcast it's data than you have no other choice than to read all of the data. How much data are you streaming? Are there other ways you can optimize your code for performance. In general well written code can run very efficiently. Also a parallel task simply reading the data is not that much of a drain on your system.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 5
(2,362 Views)

@ggallin08 wrote:

But, this has to run on my laptop in a loop that's at least as fast as the loop on the cRIO.


Nope - on the laptop, you could read multiple pieces of data at once, and discard most of them.  Let's say the cRIO puts out a 10-byte packet every 10ms.  On your laptop, every 100ms you could read 100 bytes, so the laptop loop would run only 1/10th the speed of the cRIO loop.  You didn't specify how much data you're sending, but the Nagle algorithm says a lot of small packets will get combined into one larger one anyway, so doing one large read likely gets you the data at the same rate as several small reads.

 

A command-response system is also a reasonable way to approach this problem.

0 Kudos
Message 5 of 5
(2,354 Views)