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 may also emulate RS-232 connection 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, configuring and closing the connection. They correspond to fopen and fclose for files. For receiving and sending data, use the same functions as for reading from and writing to files, such as fread and fwrite.

closedevice

Close a serial port opened with opendevice.

Syntax

closedevice(fd)

Description

closedevice(fd) closes the connection established with opendevice. Its argument is the file descriptor obtained with opendevice. Once it is closed, a serial port can be opened again by opendevice or by another application. All ports are closed anyway when the application quits.

See also

opendevice

devicename

Serial device name.

Syntax

name = devicename(n)

Description

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

Examples

On a Macintosh with internal modem:

devicename(1)
  Internal Modem

Under Windows:

devicename(1)
  COM1

See also

opendevice

flushdevice

Flush the input buffer of a serial port opened with opendevice.

Syntax

flushdevice(fd)

Description

flushdevice(fd) discards all the data in the input buffer of a serial port opened with opendevice. The input buffer is always flushed when the connection is opened. flushdevice is useful to recover from errors.

See also

opendevice

opendevice

Open a serial port.

Syntax

fd = opendevice(portname, options)
fd = opendevice(portname)

Description

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

The second argument of opendevice(portname,options) is a string of comma-separated options from the following list (n is a place holder for a positive decimal number):

OptionDefaultMeaning
bn19200bits per second
Tn1000timeout in milliseconds
tofftext mode (input CR and CRLF are converted to LF)
hoffhardware handshake control
sn2number of stop bits (1, 1.5 or 2)

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

Example

fd = opendevice(devicename(1), 'b19200,t,T2000');
fprintf(fd, 'L,%d,2\n', 1);
reply = fgetl(fd)
closedevice(fd);

See also

closedevice, devicename, fread, fwrite, fscanf, fgetl, fgets, fprintf


Copyright 2000-2001, Calerga.

All rights reserved.