Create a Serial Port Object

Overview of a Serial Port Object

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 seriallist function returns a list of all serial ports on a system. The list includes virtual serial ports provided by USB-to-serial devices and Bluetooth Serial Port Profile devices. This provides a list of the serial ports that you have access to on your computer and could use for serial port communication. For example:


ans = 

  1×2 string array

  "COM1"    "COM3"

This table shows an example of serial constructors on different platforms:


Serial Constructor

Linux® 64-bit


macOS 64-bit


Microsoft® Windows® 64-bit


The serial port object s now exists in the MATLAB® workspace. To display the class of s, use the whos command.

whos s
  Name      Size         Bytes  Class

  s         1x1            512  serial object

Grand total is 11 elements using 512 bytes


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 NameDescription


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:

ans = 
    'Serial-COM1'    'COM1'    'serial'

Configuring Properties During Object Creation

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 Display

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.

To display summary information using the Workspace browser, right-click an instrument object and select 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

Creating an Array of Serial Port Objects

In MATLAB, you 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 Windows platform.

s1 = serial('COM1');
s2 = serial('COM2');

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: