Products & Services Solutions Academia Support User Community Company

Learn more about Instrument Control Toolbox   

Serial Port Object

Creating a Serial Port Object

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.

instrhwinfo ('serial')

provides a list of available serial ports. This list is an example of serial constructors on different platforms:

Platform

Serial Constructor

Linux 32 and 64-bit

serial('/dev/ttyS0');

Mac OS X

and Mac OS X 64-bit
serial('/dev/tty.KeySerial1');

MicrosoftWindows 32 and 64-bit

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 information about the serial port object based on the object type and the serial port.

Serial Port Descriptive Properties

Property Name

Description

Name

Specify a descriptive name for the serial port object.

Port

Indicate the platform-specific serial port name.

Type

Indicate the object type.

You can display the values of these properties for s with the get function.

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

Serial Port Object Display

The serial port object provides a convenient display that summarizes important configuration and state information. You can invoke the display summary these three ways:

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.

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
  


Recommended Products

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

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