| MATLAB® | ![]() |
| On this page… |
|---|
Overview of a Serial Port Object Configuring Properties During Object Creation |
The serial function requires the name of the serial port connected to your device as an input argument. Additionally, you can configure property values during object creation. For example, to create a serial port object associated with the serial port COM1, enter:
s = serial('COM1');The serial port object s now exists in the MATLAB workspace. You can display the class of s with the whos command.
whos s 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 descriptive information about the serial port object based on the object type and the serial port.
Descriptive General Purpose Properties
| Property Name | Description |
|---|---|
Specify a descriptive name for the serial port object | |
Indicate the platform-specific serial port name | |
Indicate the object type |
Display the values of these properties for s with the get function.
get(s,{'Name','Port','Type'})
ans =
'Serial-COM1' 'COM1' 'serial'You can configure serial port properties during object creation. serial accepts property names and property values in the same format as the set function. For example, you can specify property name/property value pairs.
s = serial('COM1','BaudRate',4800,'Parity','even');If you specify an invalid property name, the object is not created. However, if you specify an invalid value for some properties (for example, BaudRate is set to 50), the object might be created but you are not informed of the invalid value until you connect the object to the device with the fopen function.
The serial port object provides you with 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.
The display summary for the serial port object s is:
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
In MATLAB software, you can create an array from existing variables by concatenating those variables together. The same is true for serial port objects. For example, suppose you create the serial port objects s1 and s2.
s1 = serial('COM1');
s2 = serial('COM2');You can now create a serial port object array consisting of s1 and s2 using the usual MATLAB syntax. To create the row array x, enter:
x = [s1 s2] Instrument Object Array Index: Type: Status: Name: 1 serial closed Serial-COM1 2 serial closed Serial-COM2
To create the column array y, enter:
y = [s1;s2];
Note that you cannot create a matrix of serial port objects. For example, you cannot create the matrix:
z = [s1 s2;s1 s2]; ??? Error using ==> serial/vertcat Only a row or column vector of instrument objects can be created.
Depending on your application, you might want to pass an array of serial port objects to a function. For example, to configure the baud rate and parity for s1 and s2 using one call to set:
set(x,'BaudRate',19200,'Parity','even')
To see which functions accept a serial port object array as an input, see the Serial Port Devices functional reference.
![]() | Getting Started with Serial I/O | Connecting to the Device | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |