| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Overview of a Serial Port Object Configuring Properties During Object Creation |
The serial function requires the name of the serial port connected to your device as an input argument. Additionally, you can configure property values during object creation. To create a serial port object associated with the serial port enter:
s = serial('port');This creates a serial port object associated with the serial port specified by 'port'. If 'port' does not exist, or if it is in use, you will not be able to connect the serial port object to the device. 'port' object name will depend upon the platform that the serial port is on. The Instrument Control Toolbox function
instrhwinfo('serial')provides a list of available serial ports. This list is an example of serial constructors on different platforms:
Platform | Serial Constructor |
|---|---|
Linux 32 and 64-bit | serial('/dev/ttyS0'); |
Mac OS X and Mac OS X 64-bit | serial('/dev/tty.KeySerial1'); |
MicrosoftWindows 32 and 64-bit | serial('com1'); |
SunSolaris 64-bit | serial('/dev/term/a'); |
The serial port object s now exists in the MATLAB workspace. You can display the class of s with the whos command.
whos s Name Size Bytes Class s 1x1 512 serial object Grand total is 11 elements using 512 bytes
Note The first time you try to access a serial port in MATLAB using the s = serial('port') call, make sure that the port is free and is not already open in any other application. If the port is open in another application, MATLAB cannot access it. Once you have accessed in MATLAB, you can open the same port in other applications and MATLAB will continue to use it along with any other application that has it open as well. |
Once the serial port object is created, the following properties are automatically assigned values. These general-purpose properties provide descriptive information about the serial port object based on the object type and the serial port.
Descriptive General Purpose Properties
| Property Name | Description |
|---|---|
Specify a descriptive name for the serial port object | |
Indicate the platform-specific serial port name | |
Indicate the object type |
Display the values of these properties for s with the get function. On a Windows platform, it will look like this:
get(s,{'Name','Port','Type'})
ans =
'Serial-COM1' 'COM1' 'serial'You can configure serial port properties during object creation. serial accepts property names and property values in the same format as the set function. For example, you can specify property name/property value pairs.
s = serial('port','BaudRate',4800,'Parity','even');If you specify an invalid property name, the object is not created. However, if you specify an invalid value for some properties (for example, BaudRate is set to 50), the object might be created but you are not informed of the invalid value until you connect the object to the device with the fopen function.
The serial port object provides you with a convenient display that summarizes important configuration and state information. You can invoke the display summary these three ways:
Type the serial port object variable name at the command line.
Exclude the semicolon when creating a serial port object.
Exclude the semicolon when configuring properties using the dot notation.
You can also display summary information via the Workspace browser by right-clicking an instrument object and selecting Display Summary from the context menu.
The display summary for the serial port object s on a Windows platform is:
Serial Port Object : Serial-COM1 Communication Settings Port: COM1 BaudRate: 9600 Terminator: 'LF' Communication State Status: closed RecordStatus: off
Read/Write State TransferStatus: idle BytesAvailable: 0 ValuesReceived: 0 ValuesSent: 0
In MATLAB software, you can create an array from existing variables by concatenating those variables together. The same is true for serial port objects. For example, suppose you create the serial port objects s1 and s2 on a Windowsplatform.
s1 = serial('COM1');
s2 = serial('COM2');You can now create a serial port object array consisting of s1 and s2 using the usual MATLAB syntax. To create the row array x, enter:
x = [s1 s2] Instrument Object Array Index: Type: Status: Name: 1 serial closed Serial-COM1 2 serial closed Serial-COM2
To create the column array y, enter:
y = [s1;s2];
Note that you cannot create a matrix of serial port objects. For example, you cannot create the matrix:
z = [s1 s2;s1 s2]; ??? Error using ==> serial/vertcat Only a row or column vector of instrument objects can be created.
Depending on your application, you might want to pass an array of serial port objects to a function. For example, to configure the baud rate and parity for s1 and s2 using one call to set:
set(x,'BaudRate',19200,'Parity','even')
To see which functions accept a serial port object array as an input, see the Serial Port Devices functional reference.
![]() | Getting Started with Serial I/O | Connecting to the Device | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |