Citation :
OPEN "COMn:baud,parity,data stop{,options}" for random as #handle
Description:
The OPEN "COMn:" statement opens a serial communications port for reading and writing. This feature uses Microsoft Windows' own built-in communications API, so if you have a multiport communications card and a WIndows driver to support that card, you should be able to use any port on the card.
The simplest form for this command is:
OPEN "COMn:baud,parity,data,stop" for random as #handle
Allowable choices for baud are:
75 110 150 300 600 1200 2400 1800 2400 4800 9600 19200 38400 57600 115200
Allowable choices are parity are:
N No parity
E Even parity
O Odd parity
S Space parity
M Mark parity
Allowable choices for data are:
5 bits long
6 bits long
7 bits long
8 bits long
Allowable choices for stop are:
1 stop bit
2 stop bits
Additional optional parameters can be included after the baud, parity, data and stop information:
CSn Set CTS timeout in milliseconds (default 1000 milliseconds)
DSn Set DSR timeout in milliseconds (default 1000 milliseconds)
PE Enable parity checking
RS Disable detection of RTS (request to send)
Other defaults:
DTR detection is disabled
XON/XOFF is disabled
binary mode is the default
To set the in and out communications buffers (each port has its own), set the variable Com (notice the uppercase C) to the desired size before opening the port. Changing the variable after opening a port does not affect the size of the buffers for that port while it is open.
'set the size of the communications buffers '(in and out) to 16K each
Com = 16384
Usage Notes:
To open com port 2 at 9600 baud, 8 data bits, 1 stop bit, and no parity, use this line of code:
open "com2:9600,n,8,1" for random as #commHandle
It is recommended that you set certain handshaking switches so that your program doesn't just freeze when waiting for data to come in or trying to send data. To do this, we add ds0, cs0, and rs switches as below. You needs may differ, but these work well for most applications. Notice we use a different communications speed in this example.
open "com2:19200,n,8,1,ds0,cs0,rs" for random as #commHandle
Remember that when a modem dials and connects to another modem, it negotiates a cpnnectopm speed. In the case of 14400 speed modems, you need to specify 19200 as the connection speed and let the modems work it out between themselves during the connect. This is because 14400 is not a baud rate supported by Windows (and you'll find that QBASIC doesn't directly support 14400 baud either).
Once the port is open, sending data is accomplished by printing to the port (ATZ resets modems that understand the Hayes command set):
print #commHandle, "ATZ"
To read from the port you should first check to see if there is anything to read. This is accomplished in this fashion:
numBytes = lof(#commHandle)
Then read the data using the input$() function.
dataRead$ = input$(#commHandle, numBytes)
Putting the lof( ) and input$( ) functions together on one line, it looks like this:
dataRead$ = input$(#commHandle, lof(#commHandle))
When you're all done, close the port:
close #commHandle
Just BASIC has the ability to disable DSR checking by specifying a zero or non value using the DS switch:
open "com1:9600,n,8,1,ds0" for random as #com
or
open "com1:9600,n,8,1,ds" for random as #com
Use the txcount(#handle) function to get a count of bytes in a serial communications transmit queue.
count = txcount(#com)
|