COMM OPEN statement

Purpose

Open a serial port.

Syntax

COMM OPEN "COMn" AS [#] hComm

Remarks

Opens a serial port to begin communications, creating a relationship between a file number and a specific serial port device.

COMn

Identifies the serial port number, for example, COM1, COM4, etc.  A colon must not follow the port specification.  See Restrictions below.

hComm

A valid integer-class variable containing a free PowerBASIC file number, typically provided by the FREEFILE function.  Although not recommended, hComm can be specified as a numeric literal; however, using hard-coded values can be more difficult to manage successfully in large programs, and such code is unlikely to be thread-safe.

The Number symbol (#) prefix is optional, but recommended for the purposes of clarity.

If the port was not opened successfully, the ERR system variable will contain the error code.  Before actual communications through the port can commence, you must configure the communication parameters by using a COMM SET statement for each parameter.

Restrictions

COMM OPEN cannot use an operating system file handle, nor open a port that is already in use.  When opening ports above COM9, Windows requires the port name to be specified using the following syntax:

COMM OPEN "\\.\COM15" AS #hComm

See also

Serial CommunicationsCOMM CLOSE, COMM functionCOMM LINE, COMM PRINT, COMM RECV, COMM RESET, COMM SEND, COMM SET, FREEFILE, OPEN

Example

DIM hComm AS LONG

hComm = FREEFILE

COMM OPEN "COM1" AS #hComm

COMM OPEN "COM2" AS #5