• Keine Ergebnisse gefunden

10 WHILE NOT EOF(1)

Im Dokument By by (Seite 148-172)

COMMUNICATION 1/0 FUNCTIONS

10 WHILE NOT EOF(1)

20 AS=INPUTS(LOC(1),1#1]

30

40 . . . Process data returned in AS 50 ...

60 WEND

The above statements return the characters in the buffer into A$ and process them, provided there are characters in the buffer. Ifthere are more than 255 characters, only 255 will be returned at a time to prevent String Overflow.

Ifthis is the case, EOF(l) is false and input continues until the input buffer is empty. The sequence of events is therefore simple, concise, and fast.

6-6

AN EXERCISE INCOMMUNICATION 1/0 The following program enables your Personal Computer to be used as a conventional terminal.

Besides Full Duplex communication with a host, the TTY program allows data to be downloaded to a file. Conversely, a file may be uploaded (transmitted) to another machine.

In addition to demonstrating the elements of Asynchronous Communication, this program should be useful in transferring GWBASIC programs and data to and from your system.

10 SCREEN O,O:WIDTH 80 15 KEY OFF:CLS:CLOSE 20 DEFINT A-Z 25 LOCATE 25,1

30 PRINT STRINGS(60," ") 40 FALSE = O:TRUE NOT FALSE 50 MENU = 5 'Value of MENU key (ctrl-E) 60 XOFFS = CHRS(19):XONS = CHRS(17) 100 LOCATE 25,1:PRINT "Async TTY Program"i 110 LOCATE 1,1:L1NE INPUT "Speed? "iSPEEDS 120 COMFILS="COM1:"+SPEEDS+",E,7"

130 OPEN COMFILS AS #1

140 OPEN "SCRN:" FOR OUTPUT AS #3 200 PAUSE = FALSE

210 AS = INKEYS: IF AS =" " THEN 230

220 IF ASC(AS) = MENU THEN 300 ELSE PRINT #1,ASi 230 IF EOF(1) THEN 210

240 IF LOC(1»128 THEN PAUSE=TRUE: PRINT #1,XOFFSj 250 AS = INPUTS(LOC(1),#1)

253 L1NEFEED = 0

255 L1NEFEED = INSTR (L1NEFEED + 1, AS,CHRS(10))

257 IF L1NEFEED = 0 THEN MIDS(AS, L1NEFEED,1) = CHRS(O):GOTO 255 260 PRINT #3,ASi:IF LOC(1)> 0 THEN 240

270 IF PAUSE THEN PAUSE=FALSE:PRINT #1,XONSj 280 GOTO 210

300 LOCATE 1,1:PRINT STRINGS(30," "):LOCATE 1,1 310 LINE INPUT"FILE? "jDSKFILS

400 LOCATE 1,1:PRINT STRINGS(30," "):LOCATE 1,1 410 LINE INPUT"(T)RANSMIT OR (R)ECEIVE? "jTXRXS

420 IF TXRXS = "T" THEN OPEN DSKFILS FOR INPUT AS #2:GOTO 1000

430 OPEN DSKFILS FOR OUTPUT AS #2 440 PRINT #1,CHRS(13)j

500 IF EOF(1) THEN GOSUB 600

510 IF LOC(1)>128 THEN PAUSE=TRUE: PRINT #1,XOFFSi 520 AS = INPUTS(LOC(1),#1)

530 PRINT #2,ASj:IF LOC(1»0 THEN 510

540 IF PAUSE THEN PAUSE=FALSE:PRINT #1,XOFFSj 550 GOTO 500

600 FOR 1=1 TO 5000

610 IF NOT EOF(1) THEN 1=9999 620 NEXT I

630 IF 1=9999 THEN RETURN

640 CLOSE #2:CLS:LOCATE 25,10:PRINTIS.Download complete ·"i 650 RETURN 200

1000 WHILE NOT EOF(2) 1010 AS = INPUTS(1,#2) 1020 PRINT #1,ASj 1030 WEND

1040 PRINT #1,CHRS(26)j'CTRL-Z to make close file.

1050 CLOSE #2:CLS:LOCATE 25,10:PRINT " •• pload complete • ·"i 1060 GOTO 200

9999 CLOSE:KEY ON

6-8

Line Comments

10 Sets the screen to Black and White Text Mode and sets the Width to 80.

15 Turns off the Soft Key Display, clears the screen, and makes sure that all files are closed.

Asynchronous implies character I/O as opposed to line or Block I/O. Therefore, all PRINT's (either to the COM file or screen) are terminated with a semicolon (;). This cancels the CR LF normally issued at the end of a PRINT statement.

20 Defines all numeric variables as INTEGER.

Primarily for the benefit of the subroutine at 600-620. Any program looking for speed optimization should use integer counters in loops where possible.

25-30 Clears the 25th line starting at column 1.

40 Defines Boolean TRUE and FALSE.

50 Defines the ASCII (ASC) value of the MENU key.

60 Defines the ASCII XON, XOFF characters.

100-130 Prints program-id and asks for baud rate

(speed). Opens Communications to file number 1, Even parity, 7 data bits.

This section can be modified to check for valid baud rates before continuing.

200-280 This section performs Full Duplex110 between the Video Screen and the device connected to the RS232 connector as follows:

• Read a character from the keyboard into A$.

Note that INKEY$ returns a null stringif no character is waiting.

• Ifno character is waiting then check to see if any characters are being received. If a character is waiting at the keyboard then:

• Ifthe character was the MENU Key, then the user is ready to download a file, so retrieve the file name.

• Ifcharacter (A$) is not the MENU key then send it by writing to the communication file (PRINT #1 ... ).

• At 230, check if any characters are waiting in COM buffer. Ifnot, then go back and check keyboard.

• At 240, if more than 128 characters are waiting, then set the PAUSE flag, thereby suspending input andsend XOFF to the host, thus stopping further transmission.

• At 253-257, strip out linefeed characters before sending buffer contents to the screen. Otherwise the PC executes a LF with each CR, resulting in double spacing.

6-10

• At 250-260, read and display the contents of COM buffer on screen until empty. Continue to monitor (in 240). Suspend transmission in the event of an interface delay.

• Finally, resume host transmission by sending XON onlyifsuspended by previous XOFF.

Repeat process until MENU Key struck.

300-310 Retrieves the name of the Disk File from which the information is to be downloaded. Opens the file to file number 2.

400-420 Asks if file named is to be transmitted (Uploaded) or received (Downloaded).

430-440 Sends a CR to the host to begin the download.

This program assumes that the last command sent to the host to begin such a transfer was missing only the terminating CR.

500 When no more characters are being received (LOC(x) returns 0), then performs a time-out routine (explained later).

510 Again, if more than 128 characters are waiting, this line signals a pause, and in the meantime sends XOFF to the host.

520-530 Reads all characters in the COM buffer (LOC(x)) and writes them to disk (PRINT #2.. ).

540-550 If a pause was issued, restart host by sending XON and clear the pause flag. Continue process until no characters are received for a

pre-determined time.

600-650 This is the time-out subroutine. The FOR loop count was determined by experimentation. In short, ifno character is received from the host for 17-20 seconds, then transmission is assumed complete. If any character is received during this time (line 610) then set I well above FOR loop range to exit loop and then return to caller.

Ifhost transmission is complete, close the disk file.

6·12

1000-1060 Transmit routine. Until end of disk file do:

Read one character into A$ with INPUT$

statement. Send character to COM device in 1020. Send aCTRL Z at end of file in 1040 if receiving device needs to close its file. Finally, in lines 1050 and 1060, close the disk file, print completion message, and go back to conversation mode in line 200.

9999 Presently not executed. As an exercise, add some lines to the routine 400-420 to optionally exit the program via line 9999. This line closes the COM file which is left open and restores the Soft Key Display.

7 References

• Introduction

• Commands, Statements, and

Functions with Examples

The following GWBASIC commands, statements, and functions are described in this chapter.

ABS

Returns the absolute value of a numeric expression.

Returns the ASCII decimal code for the first character of a given string.

Returns the arctangent of the argument.

Generates a line number after every carriage return. AUTO is used only for entering programs.

Activates the bell.

Loads a memory image file into memory.

Saves sections of the main memory on the specified file.

Transfers control to a machine language subroutine. Passes unsegmented addresses.

Transfers control to a machine language subroutine. Passes segmented addresses.

Converts a given numeric expression to a double precision number.

Transfers control and passes variables to another program.

Changes the current directory.

CHR$

Returns a one-character string whose ASCII decimal code is the value of the argument passed to this function.

Converts any numeric argument to an integer by rounding the fractional portion.

Draws a circle or an ellipse with the specified center and radius. (Graphics Mode.)

Clears all numeric variables to zero, all string variables to null, and closes all open files.

Options set the highest memory location available for use by GWBASIC and set the amount of stack space.

Terminates I/O to a file or device.

Erases all or part of the screen.

In the Text Mode, sets the foreground and background colors. In Graphics Mode, defines the background and foreground palette colors.

Enables or disables event trapping of

communications activity on the specified channel.

Defines the common area that is not erased by a CHAINed program, and allows you to pass variables from one program to another.

Resumes program execution after a

CTRL-BREAK has been typed or a STOP or END statement has been executed.

cos

Returns the cosine of the argument.

Converts any numeric argument to a single

pr~cisionnumber.

Returns the current line (row) position of the cursor.

Converts an eight-byte string to a double precision number.

Converts a two-byte string to an integer.

Converts a four-byte string to a single precision number.

Creates an "internal file" of data items that can be assigned to program variables using the READ statement.

The DATE$ statement sets the current date. The DATE$ function retrieves the current date.

Defines and names user-written functions.

Assigns the current segment of memory.

Enables access to a machine language subroutine by specifying the starting address.

Declares the variable type in accordance with the letter(s) specified.

Erases program lines.

DIM

Specifies the array name, the number of dimen-sions, and the subscript upper bound for each dimension. May specify one or more arrays.

Draws an object as specified by the contents of a string expression. (Graphics Mode.)

Lets you change a program line.

Terminates program execution, closes all open data files, and returns to the command level.

Allows a modification of parameters in GWBASIC's Environment String Table.

Retrieves the specified Environment String from GWBASIC's Environment String Table.

Indicates that the end of file has been reached.

Releases space and variable names previously reserved for arrays.

An integer function that contains the error code returned by the last device to declare an error.

A string function that contains the name of the device driver that generated the error.

Returns the number of the line that contains the error.

Returns an error code.

ERROR Simulates the occurrence of a GWBASIC error, or generates a user-defined error.

EXP Returns "e" (base of natural logarithms) to the power of the argument.

FIELD Allocates space for variables in a random file buffer.

FILES Displays the names of the files in the specified directory.

FIX Returns the truncated integer part of the argument.

FOR...NEXT Allows a series of statements to be performed in a loop a specified number of times.

FRE Returns the number of bytes in memory not being used by GWBASIC.

GET(COM) Reads a specified number of bytes into the communications buffer.

GET(Files) Reads a record from a random disk file into a random buffer.

GET(Graphics) Reads graphics images from the screen.

GOSUB ...

RETURN

GOTO GWBASIC

7-6

GOSUB transfers control to a GWBASIC subroutine by branching to the specified line.

RETURN transfers control to the statement following the most recent GOSUB (or

ON...GOSUB) executed, or to a specified line.

Transfers control to a specified program line.

Initializes GWBASIC and the operating environment. (MS-DOS command.)

HEX$ Returns a string that represents the hexadecimal value of the decimal argument.

IF GOTO ELSE IF THEN ELSE

Makes a decision regarding program flow based on the result of a specified condition.

INKEY$

Returns either a one- or two-character string read from the keyboard.

Returns the byte read from a port.

Allows input from the keyboard during program execution.

Reads data items from a sequential disk file and assigns them to program variables.

Returns a string of characters read from the standard input device, the keyboard, or from a file.

Searches for the first occurrence of a given substring in a string, and returns the position at which the match is found.

Returns the largest integer that is less than or equal to the argument.

Sends a "Control Data" string to a character device driver once the device has been OPENed.

Returns a "Control Data" string from a character device driver that is OPEN.

Defines and/or displays the function key

KEY(n) Enables, disables, or terminates interrupts caused by a specific key.

KILL Deletes a disk file.

LCOPY Dumps the screen text to the printer.

LEFT$ Returns a substring extracting the leftmost number of characters from a specified string as specified by the "length" parameter.

LEN Returns the number of characters in a given string.

LET Assigns a value to a variable.

LINE Draws either a line, a rectangle, or a filled rectangle. (Graphics Mode.)

LINE INPUT Inputs an entire line (up to 254 characters) to a string variable, without delimiters.

LINE INPUT# Reads an entire line (up to 254 characters) without delimiters, from a sequential disk data file to a string variable.

LIST

LLIST LOAD LaC

7-8

Lists the current program to the screen or to a specified file or device.

Lists the current program on the printer.

Loads a program into memory from a file.

Returns the current position of the file.

LOCATE

In Graphics Mode, moves the graphics cursor to the specified position. In Text Mode, LOCATE moves the cursor to the specified position on the active page. In both modes, LOCATE may also turn the cursor on and off and define the size of either the overwrite or the user cursor.

Returns the length of the named file in bytes.

Returns the natural logarithm of a positive argument.

Returns the current position of the printhead within the printer buffer.

Prints data on the printer.

Prints data on the printer using a specified format.

Stores a string value in a random buffer field left justified, or left justifies a string value in a string variable.

Merges the current program with another program previously saved in ASCII format.

As a function, MID$ returns a substring from a specified string. As a statement, replaces a portion of one string with another string.

Makes a new directory on a specified disk.

Converts a double-precision number to an eight-byte string.

MKI$

Converts an integer to a two-byte string.

Converts a single-precision number to a four-byte string.

Changes the name of a disk file.

Deletes the current program and clears all variables, so that you can enter a new program.

A function that returns a string that is the octal value of the decimal argument.

Specifies the first line number of a trap routine to be activated as soon as characters arrive in the communications buffer.

Enables error trapping and specifies the first line number of a subroutine to be executed if an error occurs.

ON... GOSUB calls one of several specified subroutines, depending on the value of the specified expression. ON...GOTO branches like ON... GOSUB but does not return from the branch.

Specifies the first line number of a subroutine to be executed when a specified key is pressed.

Specifies the first line number of a subroutine to be executed when the music buffer contains fewer than "n" notes. This permits continuous

background music during program execution.

Specifies the first line number of a subroutine to be executed when one of the joystick buttons (triggers) is pressed.

ON TIMER(n) Causes an event trap every "n" seconds.

GOSUB

OPEN Allows I/O to a file or device.

OPEN COM Opens a communications file.

OPTION Defines the minimum value for array subscripts.

BASE

OUT Transmits a byte to an output port.

PAINT Paints an enclosed area on the screen with a specified color. (Graphics Mode.)

PEEK Returns the byte read from the specified memory location.

PLAY Plays music in accordance with a string that specifies the notes to be played and the way in which the notes are to be played.

PLAY(n) Returns the number of notes remaining in the music background buffer.

PLAY ON/ Enables, disables, or suspends PLAY(n) trapping.

OFF/STOP

PMAP Converts physical coordinates to world coordinates or vice versa. (Graphics Mode.) POINT With two arguments (x,y), returns the color

number of a pixel on the screen. Ifone argument(n) is given, returns the current graphics coordinate. (Graphics Mode.) POKE Writes a byte into a memory location.

POS

Returns the current horizontal (column) position of the cursor.

Draws a point at the specified position on the screen. (Graphics Mode.)

Outputs data on the screen.

Outputs data to the screen using a specified format.

Writes data sequentially to a disk file.

Writes data sequentially to a disk file using a specified format.

Illuminates a pixel at a specified position on the screen. (Graphics Mode.)

Writes a specified number of bytes to a communications file.

Writes a record from a random buffer to a random file.

PUT(Graphics) Transfers the graphics image stored in an array to the screen.

RANDOMIZE Reseeds the random number generator.

READ

REM

RENUM

RESET

7-12

Reads values from one or more data statements and assigns them to variables.

Allows explanatory remarks to be inserted in a program.

Changes the line numbers of the current program.

Closes all open data files on all drives.

RESTORE

Permits DATA statements to be re-read either from the beginning of the internal data or from a specified file.

Continues program execution after an error trap·

ping routine has been performed.

Returns a substring from a specified string, extracting the rightmost characters as specified by the "length" parameter.

Removes an existing directory.

Returns a random number between 0 and 1.

Stores a string value in a random buffer field right justified, or right justifies a string value in a string variable.

Runs the current program or loads a program from disk and runs it.

Saves the current program on disk.

The SCREEN function returns the ASCII code (0-255) or the color number for the character at the specified row and column. The SCREEN statement sets the screen attributes that will be used by subsequent statements.

Returns 1 if the argument is positive, 0 if the argument is zero, and -1 if the argument is negative.

Calculates the sine of the argument.

Produces a sound on the speaker.

Returns a string of a specified number of spaces.

SPC

Skips "n" spaces in a PRINT, LPRINT, or PRINT# statement.

Returns the square root of a positive expression.

Returns the x and y coordinates of two joysticks.

Terminates program execution and returns.

Returns the status of the joystick buttons (triggers).

Enables and disables trapping of the joystick buttons.

Returns the string representation of the value of a specified numeric expression.

Returns a string of specified length whose

characters all have the same ASCII code or equal the first character of a given string.

Exchanges the values of two variables.

Closes all open data files and returns to MS-DOS.

Tabs the cursor or the printhead to a specified position in PRINT, LPRINT or PRINT#

statements.

Returns the tangent of the argument.

The TIME$ statement sets the current time. The TIME$ function retrieves the current time.

Returns a single precision number indicating the seconds that have elapsed since midnight or system reset.

TIMER ON/

Enables, disables, or suspends event trapping.

(Trace Off) Stops the line number listing initiated by TRON.

(Trace On) Causes the line number of each state-ment executed to be listed.

Calls a machine language subroutine.

Converts the string expression of a number to its numeric value.

Returns the memory address of a variable or file control block.

Returns the starting address of the file control block for a specified file.

Defines subsets of the screen called "viewports."

VIEW PRINT Sets the boundary of the text window.

WAIT

Suspends program execution while monitoring the status of a machine input port.

Loops through a series of statements as long as a given condition remains true.

Sets the line width in characters.

Permits the redefinition of the screen coordinates.

(Graphics Mode.)

Writes data to the screen.

Syntax Remarks

Example

7-16

Returns the absolute value of a numeric expression.

ABS (numexp)

Th~returned value will always be positive or zero.

Ok

PRINT ABS(8*(-6])

Im Dokument By by (Seite 148-172)