COM is a Dyalog cover class for Asynchronous Serial Communication using the .Net SerialPort 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 typically used for home automation, data acquisition and process control.
Instantiating the class:
com ← ⎕NEW COM
Opening a Port
To Open a port you need to specify the port number, baud rate, data bits, parity and stop bits. In the class you can specify the buffer size, the port time-out, the last byte time-out, the switching delay and the flow control as fields (see their individual comments in the class for more information).
r ← com.Open(PortNo BaudRate DataBits Parity StopBits) ⍝ PortNo = Communication Port Number (ex.: 1, 2, 3, 4, etc.) ⍝ BaudRate = Baud Rate (300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200) ⍝ DataBits = Number of Data Bits (4, 5, 6, 7, 8) ⍝ Parity = Number for Parity (0 = None, 1 = Odd, 2 = Even, 3 = Mark, 4 = Space) ⍝ StopBits = Number of Stop Bits (1, 1.5, 2) ⍝ r = 1 for Success, 0 for Failure ⍝ r = Literal Error if Failure
Sending the Data
The method SendReceive is used to send the data as numbers from 0 to 255 (bytes). By default, to detect the last byte received, the method will wait until the value of _LastCharacTimeOut as time-out. If a positive number (lastByte) is specified as left argument, the method will return as soon as this byte is received. Alternatively a negative number can be specified as left argument (qtyBytes) and the method will return as soon as this quantity of bytes as been received. Those 2 previous options will make the communication much faster when they are used.
r ← com.SendReceive bytes ⍝ bytes = numbers from 0 to 255 r ← lastByte com.SendReceive bytes ⍝ lastByte = positive number r ← qtyBytes com.SendReceive bytes ⍝ qtyBytes = negative number ⍝ r = 1 for Success, 0 for Failure ⍝ r = Response if Success, Literal Error if Failure
Closing the Port
The method Close is used to close the port. The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.
r ← com.Close ⍝ r = 1 for Success, 0 for Failure ⍝ r = Literal Error if Failure
How to install COM in your workspace
- Do a Select all (Ctrl+A) and a copy (Ctrl+C).
In your workspace execute )ed ○ COM
- Paste (Ctrl+V) the text into the Dyalog editor
- Press Escape and ')save' your workspace
Version 1.0 January 2016, Pierre Gilbert
<apgil AT SPAMFREE videotron DOT ca>