Two serial ports are available for asynchronous serial communications. They are labeled COM1: and COM2: and are opened in a manner similar to opening a file. After being opened they will have an associated file number (like an opened disk file) and you can use any commands that operate with a file number to read and write to/from the serial port. A serial port is also closed using the CLOSE command.
The following is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
‘ open the first serial port with a speed of 4800 baud OPEN “COM1:4800” AS #5 ‘ send the string “Hello” out of the serial port PRINT #5, “Hello” ‘ get up to 20 characters from the serial port Data$ = INPUT$(20, #5) ‘ close the serial port CLOSE #5 |
The Serial OPEN Command
A serial port is opened using the command:
OPEN comspec$ AS #fnbr
The transmission format is fixed at 8 data bits, no parity and one stop bit.
‘fnbr’ is the file number to be used. It must be in the range of 1 to 10. The # is optional.
‘comspec$’ is the communication specification and is a string (it can be a string variable) specifying the serial port to be opened and optional parameters.
It has the form “COMn: baud, buf, int, intlevel, FC, OC” where:
COMn: baud, buf, int, intlevel, FC, OC, S2
where:
‘n’ is the serial port number for either COM1: or COM2:
‘baud’ is the baud rate, either 19200, 9600, 4800, 2400, 1200, 600, 300 bits per second. Default is 9600.
‘buf’ is the buffer sizes in bytes. Two of these buffers will be allocated from memory, one for transmit and one for receive. The default size is 256 bytes. Buffer allocation is done in 256 byte blocks. If you set the buffer size to 800 bytes (for example) then 1024 bytes of memory will actually be used.
‘int’ is the line number or label of an interrupt routine to be invoked when the serial port has received some data. The default is no interrupt.
‘intlevel’ is the number of characters that must be waiting in the receive queue before the receive interrupt routine is invoked. The default is 1 character.
All parameters except the serial port name (COMn:) are optional. If any one parameter is left out then all the following parameters must also be left out and the defaults will be used.
Four options can be optionally added, these are FC, DE, and OC.
‘FC’ will enable hardware flow control. Flow control can only be specified on COM1: and it enables two extra signals, Request To Send (receive flow control) and Clear To Send (transmit flow control). Default is no flow control.
‘DE’ will enable the Data output Enable (DE) signal for RS485. The DE signal will go high just before a byte is transmitted and will go low when the last byte in the transmit buffer has been sent.
‘OC’ will force the output pins (Tx and optionally RTS) to be open collector and can be used on both COM1: and COM2:. The default is normal (0 to 3.3V) output. Open collector is like having a transistor that when turned on connects the output to ground, but when off leaves the output floating.
‘S2’ specifies that two stop bits will be sent following each character transmitted.
These options must be at the end of ‘comspec’ and, if specified, OC must be last. FC and DE apply only to COM1 and are mutually exclusive (you cannot specify both).
Opening a serial port using all the defaults:
OPEN “COM2:” AS #2
Opening a serial port specifying only the baud rate (4800 bits per second):
OPEN “COM2:4800” AS #1
Opening a serial port specifying the baud rate (9600 bits per second) and buffer size (1KB) but no flow control:
OPEN “COM1:9600, 1024” AS #8
The same as above but with receive flow control (RTS) and transmit flow control (CTS) enabled:
OPEN “COM1:9600, 1024, FC” AS #8
An example specifying everything including an interrupt, an interrupt level, flow control and open collector:
OPEN “COM1:19200, 1024, ComIntLabel, 256, FC, OC” AS #5
Serial Input/Output Pin Allocation
COM1: uses pin 15 for receive data (data in) and pin 16 for transmit data (data out). If flow control is specified pin 17 will be used for RTS (receive flow control – it is an output) and pin 18 will be CTS (transmit flow control – it is an input). If the Data output Enable (DE) signal for RS485 is enabled it will be on pin 17.
COM2: uses pin 19 for receive data (data in) and pin 20 for transmit data (data out) in the monochrome Maximite and D0 for receive data and pin D1 for transmit data on the Color Maximite.
When a serial port is opened the pins used by the port are automatically set to input or output as required and the SETPIN and PIN commands are disabled for the pins. When the port is closed (using the CLOSE command) all pins used by the serial port will be set to a not-configured state and the SETPIN command can then be used to reconfigure them.
The signal polarity is standard for devices running at TTL voltages (not RS232). Idle is voltage high, the start bit is voltage low, data uses a high voltage for logic 1 and the stop bit is voltage high. The flow control pins (RTS and CTS) use a low voltage to signal stop sending data and high as OK to send. These signal levels allow you to directly connect to devices like the EM-408 GPS module (which uses TTL voltage levels).
Reading and Writing the Serial Port
Once a serial port has been opened you can use any commands or functions that use a file number to write and read from the port. Generally the PRINT command is the best method for transmitting data and the INPUT$() function is the most convenient way of getting data that has been received. When using the INPUT$() function the number of characters specified will be the maximum number of characters returned but it could be less if there are less characters in the receive buffer. In fact the INPUT$() function will immediately return an empty string if there are no characters available in the receive buffer.
The LOC() function is also handy; it will return the number of characters waiting in the receive buffer (ie, the number characters that can be retrieved by the INPUT$() function). The EOF() function will return true if there are no characters waiting. The LOF() function will return the space (in characters) remaining in the transmit buffer.
When outputting to a serial port (ie, using PRINT #n, data) the command will pause if the output buffer is full and wait until there is sufficient space to place the new data in the buffer before returning. If the receive buffer overflows with incoming data the serial port will automatically discard the oldest data to make room for the new data.
Serial ports can be closed with the CLOSE command. This will discard any characters waiting in the buffers, return the buffer memory to the memory pool and set all pins used by the port to the not configured state. A serial port is also automatically closed when commands such as RUN and NEW are issued.
Opening a Serial Port as the Console
A serial port can be opened as the console for MMBasic. The command is:
OPEN comspec AS CONSOLE
In this case any characters received from the serial port will be treated the same as keystrokes received from the keyboard and any characters sent to the video output will also be transmitted via the serial port. This enables a user with a terminal at the end of the serial link to exercise remote control of MMBasic. For example, via a modem.
Note that only one serial port can be opened “AS CONSOLE” at a time and it will remain open until explicitly closed using the CLOSE CONSOLE command. It will not be closed by commands such as NEW and RUN.
Information from Geoff Graham http://geoffg.net
Leave a Reply