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:
|fclose||close the file|
|fflush||flush I/O buffers|
|fgetl||read a line|
|fgets||read a line|
|fprintf||write formatted data|
|fscanf||read formatted data|
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.
Serial device name.
name = serialdevname(n) list = serialdevname
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.
On a Macintosh with internal modem:
serialdevname(1) Internal Modem
Open a serial port.
fd = serialdevopen(portname, options) fd = serialdevopen(portname)
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.
fd = serialdevopen(serialdevname(1), ... serialdevset('BPS',19200,'TextMode',true,'Timeout',2)); fprintf(fd, 'L,%d,2\n', 1); reply = fgetl(fd) fclose(fd);
Configuration settings for serial port.
options = serialdevset options = serialdevset(name1, value1, ...) options = serialdevset(options0, name1, value1, ...)
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:
|BPS||19200||bit per seconds|
|Delay||0||delay after character output in seconds|
|StopBits||2||number of stop bits (1, 1.5, or 2)|
|Timeout||1||timeout in seconds|
|Virtual||false||virtual 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.
serialdevset BPS: 19200 Handshake: false StopBits: 2 TextMode: false Timeout: 1 Virtual: false