Skip to Main Content Skip to Search
Product Documentation

serial - Create serial port object

Syntax

obj = serial('port')
obj = serial('port','PropertyName',PropertyValue,...)

Arguments

'port'

The serial port name.

'PropertyName'

A serial port property name.

PropertyValue

A property value supported by PropertyName.

obj

The serial port object.

Description

obj = serial('port') 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 instrument with the fopen function.

obj = serial('port','PropertyName',PropertyValue,...) 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.

Tips

At any time, you can use the instrhelp function to view a complete listing of properties and functions associated with serial port objects.

instrhelp serial

When you create a serial port object, these property values are automatically configured:

You can specify the property names and property values using any format supported by the 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 operation while obj is not connected to the instrument. You can connect only one serial port object to a given serial port.

Examples

This example creates the serial port object s1 on a Windows machine associated with the serial port COM1.

s1 = serial('COM1');

The Type, Name, and Port properties are automatically configured.

get(s1,{'Type','Name','Port'})
ans = 
    'serial'    'Serial-COM1'    'COM1'

To specify properties during object creation,

s2 = serial('COM2','BaudRate',1200,'DataBits',7);

See Also

fclose | fopen | Name | Port | propinfo | Status | Type

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS