Main Content

Create a UDP Object and View Properties

Create a UDP object with the udpport function. udpport does not require the name of the remote host as an input argument. Although UDP is a stateless connection, opening a UDP object with an invalid port number generates an error.

You can configure property values during object creation, such as the LocalPort property if you will use the object to read data from the instrument.

Create a Byte-Type UDP Object and View Properties

Create a UDP object associated with local port 3533. This creates a byte-type udpport object.

u = udpport("LocalPort",3533)
u = 

  UDPPort with properties:

     IPAddressVersion: "IPV4"
            LocalHost: "0.0.0.0"
            LocalPort: 3533
    NumBytesAvailable: 0

  Show all properties, functions

After you create a udpport object, you can view a full list of properties and their values. Click properties in the udpport output.

           IPAddressVersion: "IPV4"
                  LocalHost: "0.0.0.0"
                  LocalPort: 3533
          NumBytesAvailable: 0

                  ByteOrder: "little-endian"
                    Timeout: 10
                 Terminator: "LF"

          EnablePortSharing: 0
            EnableBroadcast: 0
            EnableMulticast: 0
    EnableMulticastLoopback: 1
             MulticastGroup: ""

      BytesAvailableFcnMode: "off"
     BytesAvailableFcnCount: 64
          BytesAvailableFcn: []
         OutputDatagramSize: 512
            NumBytesWritten: 0

           ErrorOccurredFcn: []
                   UserData: []

For more information about how to configure these properties, see Properties.

Create a Datagram-Type UDP Object and View Properties

You can also create a datagram-type udpport object by specifying the object type during object creation.

u = udpport("datagram")
u = 

  UDPPort with properties:

         IPAddressVersion: "IPV4"
                LocalHost: "0.0.0.0"
                LocalPort: 64655
    NumDatagramsAvailable: 0

  Show all properties, functions

Clicking the properties link for this object displays a different set of properties than the byte-type object.

  Show all properties, functions

              IPAddressVersion: "IPV4"
                     LocalHost: "0.0.0.0"
                     LocalPort: 64655
         NumDatagramsAvailable: 0

                     ByteOrder: "little-endian"
                       Timeout: 10

             EnablePortSharing: 0
               EnableBroadcast: 0
               EnableMulticast: 0
       EnableMulticastLoopback: 1
                MulticastGroup: ""

     DatagramsAvailableFcnMode: "off"
    DatagramsAvailableFcnCount: 1
         DatagramsAvailableFcn: []
            OutputDatagramSize: 512
           NumDatagramsWritten: 0

              ErrorOccurredFcn: []
                      UserData: []

Enable Port Sharing over UDP

UDP ports can be shared by other applications to allow for multiple applications to listen to the UDP datagrams on that port. You can bind a UDP object to a specific LocalPort number, and in another application bind a UDP socket to that same local port number so both can receive UDP broadcast or multicast data.

This allows for the ability to listen to UDP broadcasts or multicasts on the same local port number in both MATLAB® and other applications. You can enable and disable this capability with udpport object property EnablePortSharing.

The EnablePortSharing property allows you to control UDP port sharing, and the possible values are true and false. The default value is false. This property can be set only during object creation using a name-value pair argument.

u = udpport("LocalPort",3030,"EnablePortSharing",true);

You can now do read and write operations, and other applications can access the port since port sharing is enabled.

See Also

Related Topics