|On this page…|
You create a serial port object with the serial function. serial requires the name of the serial port connected to your device as an input argument. As described in Configuring Properties During Object Creation, you can also configure property values during object creation.
Each serial port object is associated with one serial port. For example, to create a serial port object associated with a 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.
provides a list of available serial ports. This list is an example of serial constructors on different platforms:
Linux® 32 and 64-bit
Mac OS Xand Mac OS X 64-bit
Microsoft® Windows® 32 and 64-bit
The serial port object s now exists in the MATLAB® workspace. You can display the class of s with the whos command.
Name Size Bytes Class s 1x1 512 serial object Grand total is 11 elements using 512 bytes
Note: The first time you try to access a serial port in MATLAB using the s = serial('com1') 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 information about the serial port object based on the object type and the serial port.
Serial Port Descriptive Properties
Specify a descriptive name for the serial port object.
Indicate the platform-specific serial port name.
Indicate the object type.
You can display the values of these properties for s
ans = 'Serial-COM1'
ans = 'COM1'
ans = 'serial'
The serial port object provides 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.
You can also display summary information via 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 = serial('COM1')
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