(Not recommended) Create serial port object
obj = serial('port')
obj = serial('port','
The serial port name.
A serial port property name.
A property value supported by
The serial port object.
obj = serial('port') creates a serial
port object associated with the serial port specified by
port does not exist, or if it is in use, you will not be able
to connect the serial port object to the instrument with the
obj = serial('port','
creates a serial port object with the specified property names and property values.
If an invalid property name or property value is specified, an error is returned and
the serial port object is not created.
This example creates the serial port object
s1 on a Windows® machine associated with the serial port COM1.
s1 = serial('COM1');
Port properties are automatically configured.
s1.Type ans = serial
s1.Name ans = Serial-COM1
s1.Port ans = COM
To specify properties during object creation,
s2 = serial('COM2','BaudRate',1200,'DataBits',7);
At any time, you can use the
instrhelp function to view a
complete listing of properties and functions associated with serial port
When you create a serial port object, these property values are automatically configured:
Type is given by
Name is given by concatenating
Serial with the port specified in the
Port is given by the port specified in the
You can specify the property names and property values using any format supported
set function. For example, you
can use property name/property value cell array pairs. Additionally, you can specify
property names without regard to case, and you can make use of property name
completion. For example, the following commands are all valid.
s = serial('COM1','BaudRate',4800); s = serial('COM1','baudrate',4800); s = serial('COM1','BAUD',4800);
Before you can communicate with the instrument, it must be connected to
obj with the
fopen function. A connected
serial port object has a
Status property value of
open. An error is returned if you attempt a read or write
obj is not connected to the instrument. You can
connect only one serial port object to a given serial port.
To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB® command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.
Not recommended starting in R2019b
serial and its object properties are not recommended. Use
serialport and its properties instead.
This example shows how to connect to a serial port device using the recommended functionality.
|Functionality||Use This Instead|
s = serial("COM1"); s.BaudRate = 115200; fopen(s)
s = serialport("COM1",115200);
See Transition Your Code to serialport Interface for more information about using the recommended functionality.