Getting Started with Serial Port Communication

Query a Serial Port Device

This example illustrates some basic serial port commands.

Note

This example is shown on a Windows® platform.

If you have a device connected to the serial port COM1 and configured for a baud rate of 4800, execute the following example.

s = serialport("COM1",4800);
writeline(s,"*IDN?")
out = readline(s);
clear s

The *IDN? command queries the device for identification information, which is returned to out. If your device does not support this command, or if it is connected to a different serial port, modify the previous example accordingly.

Note

*IDN? is one of the commands supported by the Standard Commands for Programmable Instruments (SCPI) language, which is used by many modern devices. Refer to your device documentation to see if it supports the SCPI language.

The Serial Port Session

This example describes the steps you use to perform any serial port task from beginning to end.

The serial port session comprises all the steps you are likely to take when communicating with a device connected to a serial port. These steps are:

  1. Find your serial ports — Display a list of serial ports on your system using the serialportlist function.

  2. Connect to a serial port device — Connect to a device for a specific serial port using the serialport creation function.

    Configure properties during object creation if necessary. In particular, you might want to configure properties associated with serial port communications such as the baud rate, the number of data bits, and so on. Alter the necessary device settings by configuring property values, read data, and write data.

  3. Configure properties — To establish the desired serial port object behavior, assign values to properties using dot notation.

    In practice, you can configure many of the properties at any time including during, or just after, object creation. Conversely, depending on your device settings and the requirements of your serial port application, you might be able to accept the default property values and skip this step.

  4. Write and read data — Write data to the device using the writeline or write function, and read data from the device using the readline or read function.

    The serial port object behaves according to the previously configured or default property values.

  5. Disconnect and clean up — When you no longer need the serial port object, remove it from the MATLAB® workspace using the clear command.

The serial port session is reinforced in many of the serial port documentation examples. To see a basic example that uses the steps shown above, see Query a Serial Port Device.

Configure and Return Properties

This example describes how to display serial port property names and property values, and how to assign values to properties.

Establish the desired serial port object behavior by configuring property values. You can display or configure property values using dot notation.

Display Property Names and Property Values

After you create the serial port object, click all properties to display all properties and their values to the command line.

s = serialport("COM1",4800);
s = 

Serialport with properties

                 Port: "COM1"
             BaudRate: 4800
    NumBytesAvailable: 0
      NumBytesWritten: 0

Show all properties

                      Port: "COM1"
                  BaudRate: 4800
         NumBytesAvailable: 0
           NumBytesWritten: 0

                 ByteOrder: "little-endian"
                  DataBits: 8
                  StopBits: 1
                    Parity: "none"
               FlowControl: "none"
                   Timeout: 10
                Terminator: "LF"

     BytesAvailableFcnMode: "off"
    BytesAvailableFcnCount: 64
         BytesAvailableFcn: []

          ErrorOccurredFcn: []
                  UserData: []

To display the current value for one property, supply the property name using dot notation.

s.BytesAvailableFcnCount
ans =

    64

Configure Property Values

You can configure property values using dot notation as well.

s.BaudRate = 9600
s = 

Serialport with properties

                 Port: "COM1"
             BaudRate: 9600
    NumBytesAvailable: 0
      NumBytesWritten: 6

Show all properties

You can configure only one property value at a time using dot notation.

In practice, you can configure many of the properties at any time while the serial port object exists — including during object creation. However, some properties are not configurable while the object is connected to the device or when recording information to disk. For information about which properties are configurable, see serialport.

Specify Property Names

Serial port property names are presented using mixed case. While this makes property names easier to read, use any case you want when specifying property names. For example, to configure the BaudRate property:

s.BaudRate = 9600;
s.baudrate = 9600;

Default Property Values

Whenever you do not explicitly define a value for a property, the default value is used. All configurable properties have default values.

Note

Your operating system provides default values for all serial port settings such as the baud rate. However, these settings are overridden by your MATLAB code and have no effect on your serial port application.

You can find the default value for any property in serialport.