Serial I/O

Serial ports are normally used to communicate with a host computer or an intelligent sensor. The GPL Controller's first RS-232 serial port is named "/dev/com1." If your controller contains additional RS-232 serial ports, they are named "/dev/com2" and "/dev/com3."  If your controller contains an RS-485 port that is available to application programs, it is named "/dev/com4".  If your system is connected to a Remote IO (RIO) board that provides additional remote serial ports, they are named "/dev/comrxy" where "x" is the number of the RIO board and "y" is the number of the RIO's serial port.

The first serial port is also used by the GPL serial console interface, so you cannot use the serial console if you are using "/dev/com1". When you open device "/dev/com1", the console interface is immediately disabled. You can disable or re-enable the serial console, by changing Parameter Database entry "Serial console enable" (DataID 125). When the serial port is being utilized for program input and output, you can still access the system console via the Telnet interface.  Note that system crash messages and certain fatal error messages may be output to /dev/com1 even when it is being used by a GPL procedure. Your remote system must be able to handle these unexpected messages.

Serial ports send and receive streams of byte data in a format and rate determined by their configuration. See instructions elsewhere for setting up the baud rate, character size, stop bits, parity, and hardware flow control settings. Unlike files, a single serial port can be opened for both input and output simultaneously. There is no way for a serial device to detect that a communications link has been closed. Normally the remote device sends a special byte sequence or message to indicate the end of communications.

Steps for Communicating Using a Serial Port

Follow this procedure:

Step Action

1.  

If you are planning to use the first serial port permanently for communications, set the system parameter "Serial console enable" (DataID 125) to 0.

2.  

Open the port for output by creating a StreamWriter object. The device name is a required argument to the StreamWriterNew method. For serial port 1, the device is "/dev/com1."

3.  

Open the port for input by creating a StreamReader object. The device name is a required argument to the StreamReader New method. Use the same device as specified in the previous step.

4.  

Decide if you want your data to be buffered during output. Generally serial communications is not buffered. If you want it buffered, change the AutoFlush property to False, from its default value of True. If you use buffered output you probably need to use the Flush method to make sure your output is transmitted when you expect.

5.  

If you are going to organize your output data into lines, decide if the default line terminator (CR-LF) is appropriate. If not, use the NewLine property to change it.

6.  

Use the Write or WriteLine methods on your StreamWriter object to write data.

7.  

If you do not want your input procedure to be blocked while waiting for data to be received, use the Peek method to check if data is present before using Read.

8.  

Use the Read or ReadLine methods on your StreamReader object to read data.

9.  

Use the Close method to release the serial ports for other use and free system resources.

 

Serial I/O Example

In this example, serial port 1 is used to communicate with an operator terminal connected to the port. The program prompts the operator to type a character and waits until they do. Then it outputs a message describing the character to the serial port. The device "/dev/com1" is opened for both output and input by creating both StreamWriter and StreamReader objects, "o" and "i", respectively. The output line terminator is set to CR by using the NewLine property. The procedure polls the input every 500 milliseconds for input. If no input is received, a series of dots is output. When an input character is received, it is converted to a readable string and a message is written back to the serial port. When an ASCII ESC character (decimal value 27) is received, the procedure closes the streams and exits.

Public Sub com1
' Open com1, echo info about any input.
Dim o As New StreamWriter("/dev/com1")
Dim i As New StreamReader("/dev/com1")
Dim c As Integer
Dim ss As String

o.NewLine = Chr(GPL_CR) ' Set CR as the line terminator
o.WriteLine("Type characters, hit ESC to quit.")

Do
o.Write("Waiting for input ")
While i.Peek() < 0
Thread.Sleep(500)
o.Write(".")
End While
o.WriteLine("")

c = i.Read()
If c >= &H20 Then
ss = Chr(c)
Else
ss = "^" & Chr(c+&H40)
End If

o.WriteLine("You typed " & """" & ss _
& """ = " & CStr(c))
Loop While c <> 27 ' Exit if ESC typed

i.Close()
o.Close()
End Sub