The following pages describe how to read and write data from or to serial ports and files using GPL streams. These pages provide a summary of the classes and methods that may be used, as well as some simple examples. For additional details on individual methods, see the GPL Dictionary.
Table 19-33 summarizes many of the concepts related to file and serial I/O operations that are mentioned in this section.
| Concept | Description |
|---|---|
|
ASCII |
American Standard Code for Information Interchange. A code that represents the English alphabet, numbers, symbols, and control characters as 7-bit binary numbers. Used by GPL to represent text strings. |
|
Buffer |
An internal data area that groups bytes into larger blocks so that they can be read or written more efficiently. |
|
Byte |
An 8-bit data item that can hold a number from 0 to 255 or an ASCII character. Streams are composed of bytes. |
|
CR |
Carriage Return. The ASCII control character with decimal value 13. Often used as a line terminator. |
|
Directory |
A named grouping of files. Also known as a "folder". Directory names have the same properties as normal file names. Directories may be contained inside other directories. |
|
File |
A named collection of bytes that may be stored in permanent flash memory (on device /flash) or in temporary system memory (on device /ROMDISK). |
|
File name |
The name of a file. File names may be from 1 to 43 characters and contain any printable ASCII character other than "/" or a leading ".". Upper and lower case letters are considered to be different. It is recommended, but not required, that only valid GPL symbol names are used. |
|
Flush |
For efficiency, write operations often just add data to an internal buffer and do not access the associated file or serial port. This allows small strings to be accumulated. The system then automatically writes entire buffers to the output device when the buffer is full. The downside of this process is that if the controller is turned off, the contents of the internal buffers are lost. "Flushing" buffers forces their contents to be written to the file or serial port. Closing a stream automatically flushes any associated buffer. |
|
LF |
Line Feed. The ASCII control character with decimal value 10. Often used as a line terminator. |
|
Line terminator |
A sequence of 1 or 2 ASCII characters that marks the end of a line. Normally LF, CR, or CR-LF. |
|
Path |
The file name, preceded by a list of folders that determine the location of the file. For example: A GPL program file may be found in "/flash/projects/My_project/Main.gpl". |
|
Serial port |
An I/O device that transmits and receives byte data using a standard serial protocol. The first RS-232 serial port is named "/dev/com1". Depending on your controller model, you may have additional RS-232 serial ports named "/dev/com2" and "/dev/com3" and an optional RS-485 serial port named "/dev/com4". Remote serial ports are named "/dev/comrxy" where "x" is the number of the remote device and "y" is the serial port on the remote device. |
The StreamWriter and StreamReader classes treat data from serial ports or files as a continuous stream of 8-bit bytes. These bytes may be ASCII characters or they may be arbitrary binary data. Many of the methods transfer data to and from GPL string variables. Each byte of a string may be thought of as either an 8-bit binary value or an ASCII character. GPL includes methods and functions to convert between integer data and ASCII characters in strings, for example the Chr and Asc functions.
Some methods interpret the data stream as a series of lines, terminated by a special "line-terminator" character sequence. The NewLine property allows some flexibility in determining the line-terminator used when writing lines.
The File class contains methods for managing entire files or directories, such as creating directories or deleting files. All the File methods are shared, so there are no File objects.