device

Connection to serial device on Arduino hardware

Description

This object represents a connection to the serial device connected to an Arduino® hardware. You can interact with the serial device using the functions listed in Object Functions.

Creation

Description

example

serialdevObj = device(arduinoObj,'SerialPort',SerialPort) creates an object that represents the connection between the serial device connected to the Arduino hardware at the serial port specified by SerialPort. The Arduino hardware is represented by an arduino object.

example

serialdevObj = device(arduinoObj,'SerialPort',SerialPort,Name,Value) creates an object that represents the connection with additional options specified by one or more Name,Value pair arguments between the serial device connected to the Arduino hardware.

Input Arguments

expand all

The ID of the serial port available on the Arduino hardware specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1)

Note

The Arduino Mega2560, and MegaADK boards have three serial ports available while the Arduino Leonardo, Micro, MKR1000, and MKR1010 have one serial port available.

Arduino hardware connection, specified as an arduino object.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200);

The rate at which bits are transmitted in bits/s specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200);

Note

If you are using more than one device, make sure the devices operate at same BaudRate for data consistency.

Number of data bits to transmit, specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8);

Number of bits to indicate the end of data transmission, specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1);

Data Types: double

Parity for the serial communication specified as a character vector.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1, 'Parity','even');

Maximum time in seconds to wait for serial data, specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1, 'Parity','even','Timeout',2);

Properties

expand all

This property is read-only.

Type of interface used for communication.

Example:

>> serialdevObj.Interface
ans = 
      Serial

Serial port available on the Arduino hardware, specified as a number.

Example:

>> serialdevObj.SerialPort
ans = 
      1 

Data Types: double

Transmission pin for serial communication.

Example:

>> serialdevObj.TxPin
ans = 
      'D18' 

Data Types: char

Receive pin for serial communication.

Example:

>> serialdevObj.RxPin
ans = 
      'D19' 

Data Types: char

The rate at which bits are transmitted in bits/s specified as a number.

Example:

>> serialdevObj.BaudRate
ans = 
      115200

Data Types: double

This property is read-only.

Number of bytes of data available to read from the serial device, specified as a number.

Example:

>> serialdevObj.NumBytesAvailable
ans = 
      0 

Data Types: double

Parity for the serial communication specified as a character vector.

Example:

>> serialdevObj.Parity
ans = 
      'even' 

Data Types: char

Number of bits to indicate the end of data transmission, specified as a number.

Example:

>> serialdevObj.StopBits

ans =
     1

Data Types: double

Number of data bits to transmit, specified as a number.

Example:

>> serialdevObj.DataBits

ans =
     8 

Data Types: double

Maximum time in seconds to wait for serial data, specified as a number.

Example:

>> serialdevObj.Timeout

ans =
     1

Data Types: double

Object Functions

readRead data from serial device
writeWrite data to serial device

Examples

collapse all

Create an arduino connection.

arduinoObj = arduino("COM16","Leonardo","Libraries",{'SPI','Serial','I2C'})
arduinoObj = 
  arduino with properties:

                  Port: 'COM16'
                 Board: 'Leonardo'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11', 'D13'}
   AvailableAnalogPins: {'A0-A5', 'D4', 'D6', 'D8-D10', 'D12'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'I2C', 'SPI', 'Serial'}

Create a connection to the serial device on the Arduino hardware.

serialdevObj = device(arduinoObj,'SerialPort',1)
serialdevObj = 
  device with properties:

             Interface: 'Serial'
            SerialPort: 1
                 TxPin: 'D1'
                 RxPin: 'D0'
              BaudRate: 9600 (bits/s) 
     NumBytesAvailable: 0 

Show all properties, functions

Create an arduino connection.

arduinoObj = arduino("COM16","Leonardo","Libraries",{'SPI','Serial','I2C'})
arduinoObj = 
  arduino with properties:

                  Port: 'COM16'
                 Board: 'Leonardo'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11', 'D13'}
   AvailableAnalogPins: {'A0-A5', 'D4', 'D6', 'D8-D10', 'D12'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'I2C', 'SPI', 'Serial'}

Create a connection to the serial device with the the BaudRate set to 115200 bits/s.

serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate', 115200)
serialdevObj = 
  device with properties:

             Interface: 'Serial'
            SerialPort: 1
                 TxPin: 'D1'
                 RxPin: 'D0'
              BaudRate: 115200 (bits/s) 
     NumBytesAvailable: 0 

Show all properties, functions

See Also

| |

Introduced in R2019b