COM-port (Serial Port)

1. Setting the communication condition
Use Device Manager on Windows Control Panel to set the communication condition of a COM port.
Make baud rate, data bits, parity, stop bits, flow control agree with the opposite side.
Note that flow control RTS/CTS agrees with flow control hardware on the PC side.

2. OPEN statement
To use a COM port, use such an OPEN statement as
OPEN #1: NAME "COM1"
Just after the execution of the OPEN statement, receiving starts and data are stored on the buffer even if no INPUT statement is executed.
Since data received before the execution of the OPEN statement are canceled, data transmission from the device must start after the execution of the OPEN statement.
[Supplement]
The communication conditions can be written in the form similar to the MODE command of MS-DOS just after the PORT name with a colon.
Example 1. OPEN #1:NAME "COM1: baud=9600 parity=N data=8 stop=1"
Example 2. OPEN #1:NAME "COM1: 9600,n,8,1,p"
In case of example 2,
first parameter baud rate
second parameter parity, either of n(none),e(even),or o(odd)
third parameter data bits (character size), ordinary either 7 or 8
forth parameter stop bits, either 1, 1.5, or 2
fifth parameter, ordinary either x(handshake with xon/xoff)or p(hardware handshake)

3. In case of line-based communication
Use SET ENDOFLINE, to make the end of line code agree with the opposite side.
Example. SET #1: ENDOFLINE CHR$(13)
If the EOL of the opposite side is CR+LF, this process is not required.

4. In case of character-based communication
Use a PRINT statement that has a semicolon at the tail to transmit a chaarcter, and use a CHARACTER INPUT statement to receive a character.

5 In case of binary data
Append a OPTION CHARACTER BYTE line in the program unit in order to manipulate a byte as a character.
Use ORD and CHR$ functions to convert a character to a byte and vice versa.


6. Receive Buffer
Data are received and accumulated in the buffer, unless any INPUT statement is executed.
The number of characters (in bytes) accumulated in the buffer can be known by exectuing an ASK statement with CHARACTER PENDING.
Example. ASK #1: CHARACTER PENDING n

7. CLOSE statement
Execute a CLOSE statement to terminate the COM port.
Example. CLOSE #1

There are some sample programs of using a COM port in COMM subfolder of the folder in which BASIC is installed.