Repository navigation
Open Com
Home › File I/O Functions › Open Com
Opens a serial port on a file number, with its speed, parity and other settings.
Declare Function Open Com ( byref options As String, As filenumber As Long ) As Longoptionsfilenumber
This command opens a serial port of the PC, allowing to send and receive data by using the normal file commands as Print #, Input #, Get #, ...
The main parameter is a String that describes, at the very least, which communications port to open. It has the format:
"Comn: [ baudrate ][ , [ parity ][ , [ data_bits ][ , [ stop_bits ][ , [ extended_options ]]]]]"
where,
n
Com port to open. "1", "2", "3", "4", etc. Some platforms will support more serial ports depending on how the operating system is configured. Where n is not given, "COM:" will map to "COM1:", except on Linux where "COM:" maps to "/dev/modem"
baudrate
"300" (default), "1200", ..., etc.
parity
"N" (none), "E" (even, default), "O" (odd), "S" (space), "M" (mark), "PE" (QB-quirk: checked, even parity)
data_bits
"5", "6", "7" (default) or "8".
stop_bits
"1", "1.5" or "2". (default value depends on baud rate and data bits, see table below)
| Condition | Default number of stop bits |
|---|---|
| baud rate <= 110 and data bits = 5 | 1.5 |
| baud rate <= 110 and data bits >= 6 | 2 |
| baud rate > 110 | 1 |
extended_options
Miscellaneous options. (See table below)
| Option | Action |
|---|---|
| 'CSn' | Set the CTS duration (in ms) (n>=0), 0 = turn off, default = 1000 |
| 'DSn' | Set the DSR duration (in ms) (n>=0), 0 = turn off, default = 1000 |
| 'CDn' | Set the Carrier Detect duration (in ms) (n>=0), 0 = turn off |
| 'OPn' | Set the 'Open Timeout' (in ms) (n>=0), 0 = turn off |
| 'TBn' | Set the 'Transmit Buffer' size (n>=0), 0 = default, depends on platform |
| 'RBn' | Set the 'Receive Buffer' size (n>=0), 0 = default, depends on platform |
| 'RS' | Suppress RTS detection |
| 'LF' | Communicate in ASCII mode (add LF to every CR) - Win32 doesn't support this one |
| 'ASC' | same as 'LF' |
| 'BIN' | The opposite of LF and it'll always work |
| 'PE' | Enable 'Parity' check |
| 'DT' | Keep DTR enabled after CLOSE |
| 'FE' | Discard invalid character on error |
| 'ME' | Ignore all errors |
| 'IRn' | IRQ number for COM (only supported (?) on DOS) |
All items except for the COM port are optional. The order of baudrate, parity, data_bits, stop_bits is fixed. Any skipped fixed item ( baudrate, etc...) must be empty.
The error code returned by Open Com can be checked using Err in the next line. The function version of Open Com returns directly the error code as a 32 bit Long.
Open Com() returns a 32 bit Long: a zero (0) on success and a non-zero error code otherwise.
result = Open Com( options[,] As[#] filenumber )
- On the Windows platform
"COM:" maps to "COM1:"
baud rate must be an unsigned 32-bit integer, and is passed on directly to the operating system.
- On the Linux platform
"COM:" maps to "/dev/modem"
"COM1:" maps to "/dev/ttyS0"
"COM2:" maps to "/dev/ttyS1", etc
"/dev/xyz:" maps to "/dev/xyz", etc
baud rate must be one of 50, 150, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000
baud rate can also be one of 1500000, 2000000, 2500000, 3000000, 3500000, 4000000
the baud rates supported depend on the system where the compiler itself was compiled and built
- The DOS serial driver is experimental and can access COM ports 1 to 4
It uses the following base io and IRQ's as default:
COM1 - &h3f8 - IRQ4
COM2 - &h2f8 - IRQ3
COM3 - &h3e8 - IRQ4
COM4 - &h2e8 - IRQ3
An alternate IRQ can be specified using the the "IRn" protocol option where n is 3 through 7 (first PIC). n can also be 8 through 15 (second PIC).
Currently not supported: IRQ's on the slave PIC, alternate base I/O addresses, Timeouts and most errors as detected in QB, hardware flow control, FIFO's.
"COM:" maps to "COM1:"
baud rate must be between 50 and 115200
- In the -lang qb dialect the old syntax
OPEN "COMx:...is supported.
- In QB the syntax was
OPEN "COMx:[baudrate] [,parity, [data_bits, [stop_bits, [extended_options]]]]" FOR INPUT|OUTPUT|RANDOM AS [#] nIn QB, only"COM1:"and"COM2:"are supported. In FreeBASIC, any correctly configured serial port may be used.
Open Com "COM1:9600,N,,2" As 1
Opens COM1 with 9600 baud, no parity, 7 data bits and 2 stop bits.
Open Com "COM1:115200" As 1
Opens COM1 with 115200 baud, "even" parity, 7 data bits and 1 stop bits.
Category: File I/O Functions
Kind: function
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing