Input from Devices

Devices may be read in the same manner as a STRING file, (see the STRING verb for more information), with the exception of not being able to position to a point in the file. Devices are treated as continuous character streams and must be read serially (exceptions may occur if your system supports block devices).

The data read from a device is only echoed back to the device by PRO/5 if the device is a terminal, and only when the terminal is OPENed on channel 0 (this is performed by PRO/5 when it is started). If you want a terminal to echo data on a channel other than channel 0, you must send the 'BE' (begin echo) mnemonic on the desired channel.

Examples of Device Input

Description

Example

Read characters from channel 1 until one of the input terminating characters is encountered (defined in the CTL variable).

READ (1)A$

Specify the maximum number of characters to read.

The example reads only one character from the device. .A terminator character would terminate the input, resulting in A$ containing no characters.

>READ (1,SIZ=1)A$

Use the RECORD modifier to read from a device without PRO/5 scanning for terminators.

The example retrieves one character from channel 1 and places it into A$ without modification.

>READ RECORD(1,SIZ=1)A$

Modify input characters, using the TBL= option which performs translation character-by-character. This translation occurs on each character prior to PRO/5 scanning for field terminators

In the example, the TABLE in line 0010 would map all characters into the range 0..127 and replace most of the control characters with a space (leaving the input terminators alone).

0010 TABLE 7F 20 20 20 20 20 20 20 20 20 20 0A 20 20
0010:0D 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1C
0010:1D 1E 1F
>READ (1,TBL=0010)A$

Specify the device timeout, using the TIM= option to set the maximum number of seconds to wait for the first character and between characters. If a character is not received in the specified number of seconds, PRO/5 will generate an !ERROR=0 (busy device) and return without having modified your input variable in any manner

The example establishes a "connection" between the current terminal and the device "/dev/tty01". This is done by attempting to retrieve a character from the device, and if one is available, printing it to the current terminal and trying to get another character. If the device does not have a character available, an !ERROR=0 (busy or not ready) is generated, and the ERR= branch is taken.

Line 30 retrieves a character from the current terminal (opened on channel 1 to avoid echoing), and sends it to the device. If there is not a character available from the terminal keyboard, the program continues execution at line 20. This program would loop until you interrupted it.

0010 BEGIN; OPEN(1)FID(0); OPEN(2)"/dev/tty01"
0020 READ RECORD(2,TIM=0,SIZ=1,ERR=0030); PRINT A$,;
0020:GOTO 20
0030 READ RECORD(1,TIM=0,SIZ=1,ERR=0020)A$;PRINT(2)A$,;
0030:GOTO 0020

The length of the input field may be limited without terminating the input using the "LEN=" option. This will allow the input to continue until the length specified is reached. At that point, further input is ignored (the terminal bell rings if the device is a terminal) until a terminator is read.