Reading and writing data

To complement the new COMM OPEN statement, PowerBASIC introduces four new COMM statements to help you write serial communications programs:

COMM PRINT #hComm, expr [;]

COMM SEND #hComm, expr

COMM RECV #hComm, count, expr

COMM LINE [INPUT] #hComm, expr

COMM PRINT and COMM SEND are used to send data out of the communications port (via the transmit buffer). COMM PRINT sends the data specified by expr followed by a CR/LF byte pair {$CRLF or CHR$(13,10)}. By adding a trailing semicolon to the COMM PRINT statement, PowerBASIC suppresses these CR/LF bytes. COMM SEND is identical to COMM PRINT with a trailing semicolon.

COMM RECV and COMM LINE [INPUT] are used to receive data from a communications port (via the receive buffer). The COMM(#hComm, RXQUE) function can be used to identify the number of bytes that can be retrieved with COMM RECV. COMM LINE is used to return a CR/LF delimited "line" of data from the receive buffer.

If your communications application is primarily dealing with binary data transmission and reception, COMM SEND and COMM RECV will suit this purpose exactly. COMM PRINT and COMM INPUT are very useful for sending "AT" commands to a modem and receiving the modem response. For example:

COMM PRINT #hComm, "AT"

SLEEP 1000 ' Give modem time to respond

WHILE COMM(#hComm, RXQUE)

COMM LINE #hComm, a$

' Display "AT" (the modem echo),

' followed by "OK" (the modem response)

#IF %DEF(%PB_CC32)

PRINT a$

#ELSE

MSGBOX a$

#ENDIF

WEND

The COMM RESET statement allows you to switch off all flow control during a serial communications session.

COMM RESET #hComm, FLOW

See Also

Serial Communications

Communications Basics

Communication Buffers

Parity and general error checking

Start and stop bits

Opening a communications port

A simple communications program