Main Content

serialdev

Connection to serial device on BeagleBone Black hardware

Add-On Required: This feature requires the MATLAB Support Package for BeagleBone Black Hardware add-on.

Description

This object represents a connection from the MATLAB® software to a serial device on the BeagleBone® Black hardware. To create this object, use the serialdev function. To exchange data with the serial device, use this object with the functions listed in Object Functions.

Creation

Description

serial = serialdev(bbb,port) creates a connection from the MATLAB software to the serial device on the BeagleBone Black hardware.

example

serial = serialdev(bbb,port,baudRate,dataBits,parity,stopBits) creates a connection from the MATLAB software to the serial device on the BeagleBone Black hardware using optional arguments to override the default values for baud, data bits, parity, and stop bits.

example

Input Arguments

expand all

BeagleBone Black connection created using beaglebone, specified as an object.

The name of the serial port that is connected to the BeagleBone Black hardware, specified as a character vector.

Example: '/dev/ttyO1'

Data Types: char

Properties

expand all

This property is read-only.

The rate at which the data is transferred over the serial line, specified as a scalar. The baud is measured in seconds. This property is set by the baudRate input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.

Example: 9600

Data Types: double

This property is read-only.

The number of bits per character, specified as a scalar. This property is set by the dataBits input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.

Example: 8

Data Types: double

This property is read-only.

The type of parity bit to be added to the data, specified as a character vector.

A parity bit is used to detect error in data transmission.

  • 'none' – No parity is used for error detection in the data transmission.

  • 'odd' – The data bits plus the parity bit produce an odd number of 1s.

  • 'even' – The data bits plus the parity bit produce an even number of 1s.

  • 'mark' – The parity bit is always 1.

  • 'space' – The parity bit is always 0.

This property is set by the parity input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.

Example: 'none'

Data Types: char

This property is read-only.

The number of bits used as the stop bit in the data transmission. A stop bit marks the end of a unit of transmission. This property is set by the stopBits input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.

Example: 1

Data Types: double

The waiting time in seconds to complete the object creation, specified as a positive value of type double.

Example: 10

Data Types: double

Object Functions

readRead data from serial device
writeWrite data to serial device

Examples

collapse all

You can connect to a serial device from the MATLAB software, write data to the device, and read data from the device.

Create a connection from the MATLAB software to the BeagleBone Black hardware.

bbb = beaglebone

Enable serial port 1.

enableSerialPort(bbb,1)
bbb.AvailableSerialPorts
ans = 

    '/dev/ttyO1'

In '/dev/ttyO1', the 'O' is the capital letter O, not the number zero.

Show the location of the port 1 TX and RX pins, P9_24 (UART1_TXD) and P9_26 (UART1_RXD), on the GPIO header.

showPins(bbb)

The BeagleBone Black board uses +3.3 V. Do not connect BeagleBone Black hardware directly to devices that use higher voltages.

Connect the BeagleBone Black serial port to a +3.3 V serial device.

  • To receive data, connect the P9_26 (UART1_RXD) pin on the BeagleBone Black hardware to the TxD pin on the serial device.

  • To transmit data, connect the P9_24 (UART1_TXD) pin on the BeagleBone Black hardware to the RxD pin on the serial device.

  • To provide power, connect one of the +3.3 V pins on the BeagleBone Black hardware to the VCC pin on the serial device.

  • To ground the serial device, connect a ground pin (GND) on the BeagleBone Black hardware to the GND pin on the serial device.

Research the values the serial device requires for baud, data bits, parity, and stop bit.

Create a connection, serial, from the MATLAB software to the serial device.

serial = serialdev(bbb,'/dev/ttyO1',9600)
serial = 

  Serialdev with Properties:

    BaudRate: 9600
    DataBits: 8
      Parity: 'none'
    StopBits: 1
     Timeout: 10

Write a pair of values to a serial device that requires a specific data type.

write(serial,[10 12],'uint16')

Read a 100-element array of numbers from the serial port.

output = read(serial,100,'uint16')

Increase the timeout period of the serial port.

serial.Timeout = 20
serial = 

  Serialdev with Properties:

    BaudRate: 115200
    DataBits: 8
      Parity: 'none'
    StopBits: 1
     Timeout: 20

Version History

Introduced in R2015a