2.5 Basic UDP

2.5.1 UDP

NOTE: The UDP object has been deprecated and may not appear in newer versions of the instrument-control toolbox. Instead new code should use the udpport object.

A UDP connection can be opened using the udp function:

s = udp("127.0.0.1", 80) 

The first parameter is the IP address data will be to. The second parameter is the port number.

If and ip address and port is not provides, it will default to "127.0.0.1" and 23.

The address and port can be changed after creation using the remotehost and remoteport properties.

s = udp() 
s.remotehost = "127.0.0.1";
s.remoteport = 100;

After creating the interface object, other properties of the device can be set or retrieved using get or set functions or as property access.

s = udp("127.0.0.1", 80)
oldtimeout = get(s, "timeout") # get timeout

set(s, "timeout", 10) # set the timeout
s.timeout = oldtimeout # also sets the timeout

The device can be written and read from using fread, fwrite and udp_read and udp_write functions.

udp_write(s, "test")

val = udp_read(s, 5)

The device can be closed using fclose or udp_close.

fclose(s)

2.5.2 UDP Port

The recommended method of creating a udp socket is through the udpport object.

A udpport object can be created using the udpport function:

s = udpport()

Additional parameter/value pairs can be provided during creation of the object.

After creating the interface object, properties of the device can be set or retrieved using get or set functions or as property access.

s = udpport()
oldtimeout = get(s, "Timeout") # get timeout

set(s, "Timeout", 10) # set the timeout
s.Timeout = oldtimeout # also sets the timeout

The device can be written and read from using read and write functions.

The destination address and port to send data to must be specified at least on the first time write is used.

write(s, "test", "127.0.0.1", s.LocalPort)

val = read(s)

The device can be closed by clearing the object variable.

clear s