11-14-2023 10:22 AM - edited 11-14-2023 10:26 AM
Hello,
I want to realize UDP communication between a host computer (device A) and an instrument (device B), but I am confused about the configuration of the UDP module. I want to send parameters from device A to device B through UDP Write and use UDP Read to obtain the measurement on device B. I use two 'UDP open' functions, one for UDP write and another for UDP read. Since it is necessary to configure the port number for the 'UDP open' function, should I use the same port number for both 'UDP open'? Or should I use one port number for the 'UDP Open' used for sending data and a different port number for 'UDP Open' corresponding to receiving data?
11-14-2023 11:06 AM
Have you checked the UDP examples which comes with LabVIEW?
11-14-2023 11:10 AM
It depends on a few factors.
First, if the instrument is something that already has fixed functionality, then you should consult the documentation for the instrument. However, if the instrument is something that you control, you could set it up either way. UPD connections are bi-directional, so you could use only one, but if you choose to use 2, that is fine as well. It just depends on how you want to architect your application.
-Dave
11-14-2023 11:32 AM
UDP is a connectionless protocol and in your case you can use the same local port for sending and receiving. You could even use port 0 and LabVIEW will pick a free ephemeral port.
Device B should be configured with a fixed service port where you send the requests from A. Properly configured, B can be set to send the response back to whatever the source port of the request was.
It is actually often important that UDP responses come back just with the source/destination port reversed, especially if firewalls or NAT is somewhere in between. Incoming responses on ports that did not send requests could get blocked.
11-14-2023 02:29 PM
Yes, I have checked the simple UDP example. It seems like when I write to a port (say 5000), then when I configure the UDP Open function for reading, I need to use 5000 as well?
11-14-2023 02:33 PM
Hi dave,
Thanks for your reply. I haven't got the instrument yet but I think it has fixed functionality.
11-14-2023 02:39 PM
Hi altenbach,
Thanks for the clarification. I have checked the simple UDP example. It seems like when I write to a port (say 5000), then when I configure the UDP Open function for reading, I need to use 5000 as well?
I also tried to modify both UDP Open functions in the example 'Simple UDP'. When I set the ports of both UDP open as the same number, error 56 will occur, saying the port is already occupied. So is it correct that I need to specify different port number for both UDP open?
11-14-2023 02:41 PM
In the Simply UDP example, when open UDP port to read, the receive port is the same as the remote port in the Sender.vi
11-15-2023 01:36 AM
For reading you need to listen on whatever port the other sends the message. UDP open specified the local port (used as source port for outgoing messages and will receive any incoming message that has it as destination port..
Each UDP packet has a source port and a destination port. They don't need to match. Only one connection can listed on any specific port. As I already said, you only need one connection.