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');

Microsoft® Windows® 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

    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

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

s.Name
	ans = 
    'Serial-COM1'    
s.Port
	ans = 
    'COM1' 
s.Type
	ans = 
    'serial'

    Caution   The serial port is not locked by the MATLAB application, so other applications or other instances of the MATLAB Command Window can access the same serial port. This might result in a conflict, with unpredictable results.

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:

  • 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
Was this topic helpful?