This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Create serial port object


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


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 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:

PlatformSerial Port Constructor
Linux® 64serial('/dev/ttyS0');
Mac OS X 64serial('/dev/tty.KeySerial1');
Windows® 64serial('com1');

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.


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

s1 = serial('COM1');

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

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

To specify properties during object creation

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


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

  • The Type property is given by serial.

  • The Name property is given by concatenating Serial with the port specified in the serial function.

  • The Port property is given by the port specified in the serial function.

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 on a Windows platform.

s = serial('COM1','BaudRate',4800);
s = serial('COM1','baudrate',4800);
s = serial('COM1','BAUD',4800);

Refer to Property Reference for a list of serial port object properties that you can use with serial.

Before you can communicate with the device, 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 the object is not connected to the device. You can connect only one serial port object to a given serial port.

See Also

| | | | | |

Introduced before R2006a

Was this topic helpful?