COM1-COM4 May Be Accessed from Basic Through DOS Service (76742)



The information in this article applies to:

  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1

This article was previously published under Q76742

SUMMARY

No version of Microsoft Basic, QuickBasic, or Basic Professional Development System (PDS) supports access to the COM3 or COM4 device through the Basic OPEN "COMx:" statement. However, COM3 and COM4 may be accessed by opening the device as a sequential file from Basic, which will access the device through DOS.

To access COM3 or COM4 in this manner, you must be using DOS version 3.3 or later. Note that using Basic sequential file I/O to access the serial port involves polling the port for data. This polling system is not guaranteed to be completely reliable, especially at baud rates above 1200. For higher baud rates and greater reliability, it is recommended that you use a third party library for interrupt-driven communications.

This information applies to Microsoft QuickBasic version 4.5 for MS-DOS and to Microsoft Basic PDS versions 7.0 and 7.1 for MS-DOS and MS OS/2.

MORE INFORMATION

For an explanation of why Basic does not directly support COM3 or COM4, and for a list of third-party products that may provide COM3 and COM4 support for Basic, query on the following words:

QuickBasic and com3 and third-party

You can perform both input and output to COM3 or COM4 by opening a sequential file of that name from Basic. Because opening a device this way will go through the DOS open services, which are general file and device I/O routines, any modifications needed to the baud rate or parity must be made by using the DOS MODE command.

For example, to set the baud and parity to 2400 bps and no parity with 8 data and 1 stop bits, use the following command at the DOS prompt:

mode com3:2400,N,8,1

The MODE command can be accessed from inside a Basic program via the SHELL statement. To avoid possible conflicts with shelling to MODE, which is a terminate-and-stay-resident (TSR) program, you should run MODE at least once before starting a program that will shell to it.

The following program will open COM3 as a sequential file and write to the port:
SHELL "MODE COM3:9600,N,8,1"
OPEN "COM3" FOR OUTPUT AS #1
PRINT #1, "HELLO COM3";
CLOSE #1
				
The following program will open COM3 as a sequential device, accept input from COM3, and allow the user to terminate the program by entering a Q:
SHELL "MODE COM3:9600,N,8,1"
OPEN "COM3" FOR INPUT AS #1
DO WHILE INKEY$ <> "q"
<WWBLOCKQUOTE>
        A$ = INPUT$(1, 1)          'Get 1 character from COM3
        IF A$ <> "" THEN
                PRINT A$;
                A$ = ""
        END IF

LOOP
CLOSE #1
				
Note that because you are accessing the serial port as a sequential file, all the rules of sequential file I/O apply. Data will always be transmitted as ASCII characters, and a carriage return plus linefeed pair will be appended to the end of each block of data written by a Basic PRINT statement (unless you place a semicolon [;] at the end of the PRINT statement). Errors during communication will not be noted in Basic, but may result in a Basic file I/O error instead.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB76742