﻿# Accessing Serial Ports

Some A-BASIC applications access serial ports directly for output (e.g. OPEN #1, "TRM0:LASER1", OUTPUT), or even in some cases for input. The typical reason for this is to bypass the spooler to obtain better control over a particular printing operation. Another reason may be to communicate with some kind of non-terminal, non-printer device (such as a temperature sensor, mechanical counter, voice mail system, etc.) A-Shell does not emulate the "TRM" device, but it does allow you to open raw device files, provided you use the proper syntax and that the underlying operating system supports file I/O operations on ports. 

**(./images/hmtoggle_plus1.gif)	Windows Serial I/O**

Under Windows, it is possible (but not recommended) to open a serial port for output by using the following syntax:

open #1, "COM1\\", output

Note that you must replace the normal colon in COM1: with a backslash. Otherwise, A-Shell thinks COM1: is an AMOS-type logical disk.

Unfortunately, it is not possible to open serial ports as input files.

The recommended approaches to serial I/O under Windows are either to use AutoLog (see above), or [COMIO](comiosbr.md). 



**(./images/hmtoggle_plus1.gif)	Unix Serial I/O**

You can generally treat serial ports as sequential files under Unix, opening them for either INPUT or OUTPUT (or both) as shown below:

open #1, "/dev/tty1a", output

open #2, "/dev/tty1a", input

Once open, you can use the normal PRINT or INPUT statements, although they may not work exactly as you might expect due to the large number and complexity of configurable parameters which affect serial port operations under Unix. The typical method of setting these parameters (after opening the port) is to use HOSTEX[xs](keyword-types.md) to invoke the Unix stty (or equivalent) command. (Refer to the Unix "man" page or other documentation for further details on serial port settings, a subject which exceeds the scope of this discussion.)

Note that if you are going to use the xcall HOSTEX, "stty..." approach, you must do so **after** you open the port. Otherwise your changes may be lost when the port is opened.

To output to the port, you can use the PRINT command. Beware, however, that under the Unix default buffering scheme, the characters may not be "flushed" to the device until a line terminator is output. To input, you can use INPUT or INPUT LINE, but here the subtleties of the port parameters can cause considerable confusion. For character-oriented, rather than line-oriented input, use [GET](get.md)[xs](keyword-types.md).

You can turn off output buffering on Unix or Linux devices by opening the device as an output file and then executing xcall MIAMEX,MX\_NOBUF,87,CH where CH is the file channel. Similarly, you can use xcall MIAMEX,MX\_FLUSHBUF,8,CH to immediately flush any buffered characters on that channel.

You can do character-level input from a serial port using [GET](get.md):

**xcall GET, buf, chan, bytes'req, bytes'rcv, timeout**

For a more sophisticated approach for serial communications, contact Soft Machines ([www.softmach.com](http://www.softmach.com/#_blank)) for information on their AutoLog communications package, which, among other wonderful features, includes a subroutine interface from A-Shell. Under Windows, there is also the COMIO[xs](keyword-types.md) which offers capabilities somewhere in the middle between simple file-oriented I/O and the full feature set of AutoLog.

