LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use UDP

Solved!
Go to solution

Hey,

   

     Very new to LabVIEW and having trouble getting the UDP VI's to work properly. I'm trying to use the UDP VI's to send data that is acquired in my main VI from a FieldPoint device to a Sub VI when a button is pressed. I can get it to work when I made a basic VI to run in parallel with my main VI just to try and understand the UDP setup, but when I copy it over to my Sub VI it will only send the first reading then doesn't update. Any ideas why one works and the other doesn't?

 

Thanks in advance for any help or suggestions,

dnorman

 

MainVI_UDP.jpgSubVI_UDP.jpgSubVI setup

 

Download All
0 Kudos
Message 1 of 22
(6,228 Views)

I still can't seem to find the error. I'm guessing it is just some small error that I'm overlooking, but then again I'm new to LabVIEW. Anyone with a bit more experience see a blaring error that I'm missing?

 

Thanks,

dnorman

0 Kudos
Message 2 of 22
(6,187 Views)

If you save your VI for an older version of LabVIEW (anything prior to 2012) I'm willing to take a look.  However, I really don't understand your question, nor your VI (based on the screenshots).  Wires should run left-to-right (this is a style, not function, issue) and it's odd that you'd put the UDP open and close outside a case structure but put the read inside it.  Normally you open the UDP socket once when the code starts and close it at the end, not open it and close it for every individual packet you send or receive.

0 Kudos
Message 3 of 22
(6,178 Views)

Hello dnorman,

 

There are some great UDP examples that are shipped with LabVIEW

 

From LabVIEW go to Help»Find Examples...

 

Then from the Example Finder choose Networking»TCP&UDP

 

There are two VIs in this folder, UDP Sender.vi and UDP Receiver.vi

 

I would highly recommend starting from these examples and modifying as needed.

 

Thank you,

 

Joel C

National Instruments

 

0 Kudos
Message 4 of 22
(6,172 Views)

Are you checking for errors at all on the sending side?

 

You have only attached the receiver, which also seems to have some weird coding constructs. It seems to use 100% cpu while doing nothing but spin the loop as fast as it can.

 

Where is the sender code shown in the image? Can you attach it too?

 

 

From the looks of your image, you seem to create and shutdown the connection with every iteration of the loop. Why? Is the local port important, the receiver does not seem to care about its value, so leave it disconnected. Why are you sending to the broadcast address?

 

Have a look at the shipping examples. I would recommend to prepend the data size so the receiver can determine how much to read.

 

In this particular case, I would recommend to use network shared variables instead. No need to roll your own.

0 Kudos
Message 5 of 22
(6,166 Views)

Thanks for the reply. This is my first time working with LabVIEW so I'm still getting used to everything. I inherited a project at work from a previous employee after IT managed to erase 75% of his VI from the computer it was ran on. 

 

I have already looked at the examples prior to posting here. The VI that I posted is the one that I made just to test the UDP setup. It works just like I want it to. The screenshots are from my main and sub VI's. The sub VI, to my knowledge, is setup the exact same way as my example VI, but doesn't work and I can't figure out why.

 

The open and close are outside because depending on the case there is different information sent.

 

Thanks,

dnorman

0 Kudos
Message 6 of 22
(6,164 Views)

If you keep your code this way, with a read in the case structure, you need to wire your refnum and error through the false case of the case structure. The way you have it set up now, it will "return default value" from the false case, so as soon as your program enters the false case, you lose your refnum to the open connection (because the default value is a null refnum). Also, with regards to the fact that each case has different data, don't put the read in the case structure, but do put the "string to number" in a case structure. If you have the read outside the case structure, you won't even have to deal with the previous mentioned issue about the refnum, because your refnum won't even have to be passed into the case.

0 Kudos
Message 7 of 22
(6,156 Views)

You again attached the same VI. Where is the code that sends?


@dnorman wrote:

The open and close are outside because depending on the case there is different information sent.



I am now talking a bout the picture on the right, where we are missing the real code: The open and close belong outside the while loop, they only need to bew done once per run. If there is different information being sent as a function of a case structure, all that needs to be in the case structure is the "different inforrmation". No need to duplicate UDP write.

Please show us your code!

0 Kudos
Message 8 of 22
(6,151 Views)

@for(imstuck) wrote:

You need to wire your refnum and error through the false case of the case structure. The way you have it set up now, it will "return default value" from the false case, so as soon as it enters the false case, you lose your refnum to the open connection.


Since he reads the connection with every iteration from the tunnel, the missing reference will only matter if the VI is stopped. However the while loop is hardwired, so "UDP close" is unreachable anyway. THe only way is to abort, which will close the connection anyway.

0 Kudos
Message 9 of 22
(6,147 Views)

To add to what has been stated already.

 

Wire from left to right.  It makes the code a lot easier to read.

 

Do not use the Abort button to stop a VI.  Close it out properly.  In this case, add a stop button instead of the false constant to stop the loop and close out the connection.

 

Wire the references through the case structures.  You do not want to lose those.  Bad things will happen if you do.  I know from experience.

 

Here's a cleaned up version of your VI.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Download All
0 Kudos
Message 10 of 22
(6,143 Views)