Q(uick)BASIC Statements: COM

Quick View

COM

Event trapping statements that enables, disables, or inhibits event trapping of communications activity on a specified port

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • COM(n%) ON
  • COM(n%) OFF
  • COM(n%) STOP
  • ON COM(n%) GOSUB line
Description/Parameter(s)
n% The number of a COM (serial) port (1 or 2).
COM(n%) ON Enables trapping of a communications event.
COM(n%) OFF Disables communications event trapping.
COM(n%) STOP Suspends communications event trapping. Events are processed once event trapping is enabled by COM ON.
line The label or number of the first line of the event-trapping subroutine.
Example
COM(1) ON 'Enable event trapping on port 1. ON COM(1) GOSUB ComHandler DO : LOOP WHILE INKEY$ = "" COM(1) OFF END ComHandler: PRINT "Something was typed at the terminal attached to COM1." RETURN

See also:

Syntax
  • COM(n) ON
  • COM(n) OFF
  • COM(n) STOP
Description/Parameter(s)
n the number of the communications port, either 1 or 2

The COM ON statement enables communications event trapping. If a character arrives at a communications port after a COM ON statement, then the subroutine specified in the ON COM statement is executed.

COM OFF disables communications event trapping. No communications trapping takes place until another COM ON statement is executed. Events occurring while trapping is off are ignored.

COM STOP inhibits communications event trapping so no trapping takes place until a COM ON statement is executed. Events occurring while trapping is inhibited are remembered and processed when the next COM ON statement is executed.

See also:

Syntax
  • COM(n%) ON
  • COM(n%) OFF
  • COM(n%) STOP
Description/Parameter(s)
n% The number of the COM (serial) port: 1 = COM port 1; 2 = COM port 2.
COM(n%) ON Enables trapping of a communications event by an ON COM statement.
COM(n%) OFF Disables the event trap. Even if an event takes place, it is not remembered.
COM(n%) STOP Suspends the event trap. Any events that occur are remembered and are trapped as soon as a COM ON statement is executed.

Usage Notes

  • COM(n%) ON enables communications-event trapping at COM port n%. If a character arrives at a communications port after a COM(n%) ON statement, the routine specified in the ON COM statement is executed.
  • COM(n%) OFF disables communications-event trapping at COM port n%. No trapping takes place until another COM(n%) ON statement is executed. Events occurring while trapping is off are ignored.
  • COM(n%) STOP suspends communications-event trapping at COM port n%. No trapping takes place until a COM(n%) ON statement is executed. Events occurring while trapping is suspended are remembered and processed when the next COM(n%) ON statement is executed. However, remembered events are lost if a COM(n%) OFF statement is executed.
  • When a signal-event trap occurs (that is, the GOSUB is performed), an automatic COM(n%) STOP is executed so that recursive traps cannot take place. The RETURN from the trapping routine automatically executes a COM(n%) ON statement unless an explicit COM(n%) OFF was performed inside the routine.
  • If your program contains event-handling statements and you are compiling from the BC command line, use the BC /W or /V option. (The /W option checks for events at every label or line number; the /V option checks at every statement.) If you do not use these options and your program contains event traps, BASIC generates the error message, "ON event without /V or /W on command line."
  • For more information, see Chapter 9, "Event Handling" in the Programmer's Guide.
Example

This example uses the OPEN COM statement to open the COM1 port for input and the COM statement to enable event trapping. The ON COM statement passes control to a subroutine when there is activity on a terminal.
Note: To run this program, you must have a terminal connected to the COM1 port.

CLS 'Set up error handling in case COM1 doesn't exist. ON ERROR GOTO ErrHandler 'Open the COM port. OPEN "COM1:9600,N,8,1,BIN" FOR INPUT AS #1 'Turn on COM event processing. COM(1) ON 'Set up COM event handling. ON COM(1) GOSUB Com1Handler 'Wait for a COM event to occur or a key to be pressed. DO LOOP WHILE INKEY$ = "" 'Turn off COM event handling. COM(1) OFF CLS END Com1Handler: PRINT A$; "Something was typed on the terminal attached to COM1." RETURN ErrHandler: SELECT CASE ERR CASE 68: PRINT "COM1 is unavailable on this computer.": END CASE ELSE: END END SELECT