Microsoft KB Archive/20549

= Microsoft Knowledge Base =

How to Use "COM1:" Serial Communications in Macintosh BASIC
Last reviewed: January 12, 1995

Article ID: Q20549

Question:

How can I use the Macintosh serial communications port ("COM1:") in Microsoft BASIC Interpreter, Microsoft BASIC Compiler, or QuickBASIC for the Apple Macintosh? Are there any examples or books to which I can refer for additional information?

Response:

To use the Macintosh serial port, you must first open it with the OPEN statement. The "COM1:" device can be OPENed with the parameters (baud-rate, parity, data-bits, and stop-bits) as shown in the following manuals:


 * 1) Page 124 of the "Microsoft QuickBASIC for Apple Macintosh: User's Guide" for Versions 1.00, 1.00a, 1.00b.
 * 2) Page 42 of the "Microsoft BASIC Interpreter for Apple Macintosh" manual for Versions 2.00, 2.10, or 3.00. (QuickBASIC succeeds these earlier interpreter versions.)
 * 3) "COM1:" is not documented in Microsoft BASIC Compiler Version 1.00 for Macintosh, a product which was succeeded by QuickBASIC.

The following is an example OPEN statement

OPEN "COM1: 1200, N, 8, 1" AS #1 where 1200 is the baud rate, the speed of communication. (Default baud is 300 if you don't specify a baud rate. The baud rate can be 110, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 19,200, or 57,600. However, transmission errors are more likely at the higher speeds.)

Most programs input from "COM1:" with the INPUT$ function in a loop using the LOC function to determine the number of characters currently waiting in the communications input buffer. Output to "COM1:" is usually done with the PRINT# statement. Communications programs can be tricky to write.

For an example of how to use the Macintosh serial port from BASIC, please refer to the Terminal program that comes on the release disk for QuickBASIC Versions 1.00, 1.00a, 1.00b, BASIC interpreter Versions 2.00, 2.10, and 3.00, and BASIC compiler Version 1.00.

The Terminal program is a dumb-terminal emulator, the simplest kind of communications program. The Terminal program allows you to transmit characters to and from another computer that is running a similar dumb-terminal-emulator program.

More advanced programs that upload and download files can be designed with some creativity. For more information about using and expanding the Terminal program, please refer to the following book:

"Creative Programming in Microsoft BASIC for Optimal Macintosh  Performance," by Steve Lambert (Microsoft Press, 1985); Section III, Pages 145-260. This book shows how to extend the Terminal program to add the ability to upload and download files (with a simplistic XON/XOFF handshaking protocol) and store and edit a directory of telephone numbers. (XON is a CTRL+Q, a character with ASCII value 17. XOFF is a CTRL+S, a character with ASCII value 19.)

Commercial communications applications are often quite expensive, but some communications programs written in Macintosh BASIC, such as Red Ryder, are available in the public domain. Macintosh user's groups are a good source of public domain communications programs.

For communications on the "COM1:" serial port, BASIC uses the ROM serial driver that is built into the Macintosh ROM. Apple sells a program called MacTerminal that circumvents the ROM serial driver and uses its own serial driver. If you notice a difference between Apple's MacTerminal program and BASIC's Terminal program, it could be due to a problem in the ROM serial driver built into the Macintosh ROM.

Note: You cannot change the communications parameters when you OPEN the printer port (with the "LPT1:" device name, or the LPRINT statement) in Macintosh BASIC. You can change only baud rate, parity, data bits, or stop bits when you OPEN the "COM1:" device name. You can OPEN "COM1:" as #n and send output with the PRINT#n statement.