Main Content

Create Serial Port Object

Create a Serial Port Object

You create a serial port object with the serialport function. serialport requires the name of the serial port connected to your device and the baud rate as input arguments. You can also configure property values during object creation using name-value pair arguments.

Each serial port object is associated with one serial port. For example, connect to a device that is on serial port COM1 and configured for a baud rate of 4800.

s = serialport("COM1",4800);

If the specified port does not exist, or if it is in use, you cannot connect the serial port object to the device. The port name depends on the platform that the serial port is on.

You can also use the serialportlist function to return a list of all serial ports on a system, including virtual serial ports provided by USB-to-serial devices and Bluetooth® Serial Port Profile devices. The list shows all serial ports that you have access to on your computer and can use for serial port communication.

ans = 

  1×3 string array

    "COM1"    "COM3"    "COM4"

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


Serial Constructor

Linux® 64-bit

s = serialport("/dev/ttyS0",9600);
macOS 64-bit
s = serialport("/dev/tty.KeySerial1",9600);

Microsoft® Windows® 64-bit

s = serialport("COM1",9600);


The first time you try to access a serial port in MATLAB® using the s = serialport("COM1",9600) 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. After you access the serial port in MATLAB, you can open the same port in other applications, and MATLAB continues to use it along with any other application that has it open as well.

Serial Port Object Display

The serial port object provides a convenient display that summarizes important configuration and state information. You can invoke the display summary in 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 dot notation.

You can also display summary information using 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 machine is given here.

s = serialport("COM4",9600)
s = 

  Serialport with properties:

                 Port: "COM4"
             BaudRate: 9600
    NumBytesAvailable: 0

  Show all properties, all methods

                      Port: "COM4"
                  BaudRate: 9600
         NumBytesAvailable: 0

                 ByteOrder: "little-endian"
                  DataBits: 8
                  StopBits: 1
                    Parity: "none"
               FlowControl: "none"
                   Timeout: 10
                Terminator: "LF"

     BytesAvailableFcnMode: "off"
    BytesAvailableFcnCount: 64
         BytesAvailableFcn: []
           NumBytesWritten: 0

          ErrorOccurredFcn: []
                  UserData: []

Use dot notation to configure and display property values.

s.BaudRate = 4800;
ans =

For more information about configuring these properties, see serialport.