Serial Port Functions

Serial port functions enable communication with devices connected to the computer via an RS-232 interface. Such devices include modems, printers, and many scientific instruments. The operating system can also emulate RS-232 connections with other devices, such as built-in modems or USB (Universal Serial Bus) devices.

Functions described in this section include only those required for opening and configuring the connection. They correspond to fopen for files. Input, output, and control are done with the following generic functions:

FunctionDescription
fcloseclose the file
fflushflush I/O buffers
fgetlread a line
fgetsread a line
fprintfwrite formatted data
freadread data
fscanfread formatted data
fwritewrite data
redirectredirect output

Functions opendevice, devicename, closedevice, and flushdevice are obsolete and may be removed in the future. They are replaced with serialdevopen and serialdevset to specify configuration settings, serialdevname, fclose, and fflush.

serialdevname

Serial device name.

Syntax

name = serialdevname(n)
list = serialdevname

Description

serialdevname(n) returns the name of the n:th serial device which can be opened by serialdevopen. Argument n must be 1 or higher; with other values, such as those larger than the number of serial devices available on your computer, serialdevname returns the empty string.

Without input argument, serialdevname gives the list of serial device names.

Examples

On a Macintosh with internal modem:

serialdevname(1)
  Internal Modem

Under Windows:

serialdevname(1)
  COM1

See also

serialdevopen

serialdevopen

Open a serial port.

Syntax

fd = serialdevopen(portname, options)
fd = serialdevopen(portname)

Description

serialdevopen(portname) opens a connection to the serial port whose name is portname and returns a file descriptor fd. Names depend on the operating system and can be obtained with serialdevname.

Some platforms do not provide a complete list of all ports; serialdevopen may accept additional device names and pass them directly to the corresponding function of the operating system.

The second argument of serialdevopen(portname,options) is a structure which contains configuration settings. It is set with serialdevset.

Once a connection has been opened, the file descriptor fd can be used with functions such as fread, fwrite, fscanf, and fprintf. The connection is closed with fclose.

Example

fd = serialdevopen(serialdevname(1), ...
       serialdevset('BPS',19200,'TextMode',true,'Timeout',2));
fprintf(fd, 'L,%d,2\n', 1);
reply = fgetl(fd)
fclose(fd);

See also

fclose, serialdevname, serialdevset, fflush, fread, fwrite, fscanf, fgetl, fgets, fprintf

serialdevset

Configuration settings for serial port.

Syntax

options = serialdevset
options = serialdevset(name1, value1, ...)
options = serialdevset(options0, name1, value1, ...)

Description

serialdevset(name1,value1,...) creates the option argument used by serialdevopen. Options are specified with name/value pairs, where the name is a string which must match exactly the names in the table below. Case is significant. Options which are not specified have a default value. The result is a structure whose fields correspond to each option. Without any input argument, serialdevset creates a structure with all the default settings. Note that serialdevopen also interprets the lack of an option argument, or the empty array [], as a request to use the default values.

When its first input argument is a structure, serialdevset adds or changes fields which correspond to the name/value pairs which follow.

Here is the list of permissible options:

NameDefaultMeaning
BPS19200bit per seconds
Delay0delay after character output in seconds
Handshakefalsehardware handshake
StopBits2number of stop bits (1, 1.5, or 2)
TextModefalsetext mode
Timeout1timeout in seconds
Virtualfalsevirtual serial port

Output operations wait for the specified delay after each character; this can be useful with slow devices without handshake.

When text mode is set, input CR and CR/LF sequences are converted to LF. Output CR and LF are not converted.

Depending on the platform, operations which use the timeout value (such as input) can be interrupted with the platform-dependent abort key(s) (typically Escape or Control-C) or are limited to 10 seconds.

If Virtual is true, the connection speed, handshake and number of stop bits are not set. The intended use is for drivers which emulate a serial port, such as USB serial drivers.

Example

serialdevset
  BPS: 19200
  Handshake: false
  StopBits: 2
  TextMode: false
  Timeout: 1
  Virtual: false

See also

serialdevopen, serialdevname


Copyright 2000-2013, Calerga.
All rights reserved.