You can use the NI-XNET IP stack to establish message-oriented communication with UDP.

This procedure requires you to create a JSON configuration string to define your IP stack.

UDP is a message-oriented protocol—it involves an interface that functions as a sender and another interface that functions as a receiver. The sender and receiver do not establish a connection. The sender sends and the receiver receives only when it is configured to, which may or may not be at a time when the sender is prepared to send to a receiver or when the receiver is prepared to receive from a sender.

Once you have generated the JSON configuration, setting up UDP communication involves the following high-level operations:

  1. Instantiating the IP stack.
  2. Creating a socket and, depending on whether you are programming a sender or receiver, iteratively writing or reading data on the connection.
  3. Closing the socket and the IP stack.

Use senders together with receivers to establish communication.

Complete the following steps to create UDP senders and receivers with the NI-XNET IP stack:

  1. Call IP Stack Create and pass in the JSON configuration string you created.
    Tip You can name the stack to help identify it. The IP Stack Info Tool and IP Stack Get Info can return this name.
  2. Call IP Stack Wait to ensure the interface is ready for communication.
  3. Call UDP Socket Open and specify a local port for the socket.
  4. Depending on the role you are programming, create a loop to write data (sender) or read data (receiver) over time:
    RoleDescription
    Sender Call a UDP Socket Write function:
    1. Provide either the text or the binary to write.
    2. Provide the remote address to send data to.

      This address should match the address for the receiver that you set up in the JSON configuration.

    3. Provide the remote port to send data to.

      This value should match the local port you specified for the receiver in the receiver code.

    Receiver
    1. Call the corresponding UDP Socket Read function to the data type the sender is writing.

      You can optionally set a maximum size for messages and a timeout value.

    2. To prevent timeout errors from stopping the receiver, clear error -13860.

    You can add a wait time for iterations of the loop. You can also add a manual stop to give the end user the ability to halt the writing or reading of data.

  5. To close the connection, call UDP Socket Close.
  6. To clear (close) the IP stack, call IP Stack Clear.
  7. Incorporate the sender and receiver code in your application to establish UDP communication.
    Note The sender and receiver must be configured with opposing states. Use the PHY State setting in NI configuration software to control the state interactively, or use the PHY State Configured property to control the state programmatically.

LabVIEW Programming Examples for UDP Communication

For example code for establishing UDP communication, refer to NI-XNET IP Stack Simple UDP.lvproj.