Creating a Serial Port Object

Overview of a Serial Port Object

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

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 NameDescription

Name

Specify a descriptive name for the serial port object

Port

Indicate the platform-specific serial port name

Type

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'

Configuring Properties During Object Creation

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 Display

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:

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

Creating an Array of Serial Port Objects

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.

  


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