UDP is a Dyalog cover class for Asynchronous UDP/IP Communication using the .Net UdpClient object. It is using the methods WriteAsync and ReadAsync available with .Net 4.5 for asynchronous communication that will not block the calling thread during the communication process. It is used to requests data from an Internet resource using UDP (User Datagram Protocol) over IP (Internet Protocol) like for home automation, data acquisition and process control.
An application using UDP prepares a packet and sends it to the receiver's address without first checking to see if the receiver is ready to receive a packet. If the receiving end is not ready to receive a packet, the packet is lost. In practice, UDP is quite reliable as long as the receiving socket is active and is processing data as quickly as it arrives. This is particularly the case when used on a private network. The application that is using UDP must be prepare to resend the data in case that it did not arrive to the receiving end.
Since UDP is a connectionless protocol there is no need to connect before sending the bytes.
Instantiating the class
udp ← ⎕NEW UDP
After instantiating the class the Fields _PortTimeOut and _Retries are accessible. _PortTimeOut is the Time in milliseconds to Wait for a UdpClient that is not Active (Receiving Nothing), if exceeded Windows Stop Waiting and returns the Control to the Calling Application. The default value is 250 milliseconds. _Retries is the number of times that the UdpClient will try to send the data if nothing is received. The default value is 3.
Sending the Data
The method SendReceive is used to send the data as numbers from 0 to 255 (bytes). The response contains the data received. To convert characters to bytes use ⎕UCS to do the conversion.
r ← udp.SendReceive(bytes ip port) ⍝ bytes = Bytes (as Numbers 0 to 255) to be Written to the Communication Port ⍝ ip = IP Address where to send the bytes in characters ('192.168.1.1') or numbers (192 168 1 1) ⍝ port = Port Number to send the bytes (0 - 65535) ⍝ r = 1 for Success, 0 for Failure ⍝ r = Response if Success, Literal Error if Failure
There is no need to close the connection since there is no connection that is made before sending the data.
How to install UDP in your workspace
- Do a Select all (Ctrl+A) and a copy (Ctrl+C).
In your workspace execute )ed ○ UDP
- Paste (Ctrl+V) the text into the Dyalog editor
- Press Escape and ')save' your workspace
Version 1.1 January 2016, Pierre Gilbert
<apgil AT SPAMFREE videotron DOT ca>