Documentation Center

  • Trial Software
  • Product Updates

serialdev

Create connection to serial device

Syntax

  • myserialdevice = serialdev(mypi,port)
  • myserialdevice = serialdev(mypi,port,baudRate,dataBits,parity,stopBits)

Description

myserialdevice = serialdev(mypi,port) connects to a serial device and returns a handle, myserialdevice. You can use myserialdevice to exchange data with the serial device using the read and write methods. You can read and write myserialdevice properties such as the baud rate.

On the Raspberry Pi™ hardware, the /boot/cmdline.txt file configures /dev/ttyAMA0 as the serial device and sets the default baud rate to 115200. The serial device connects to the Rx and Tx pins on the GPIO header.

By default, the serial console in the customized version of Raspian Wheezy on your Raspberry Pi hardware is disabled. To use serialdev, the serial console must be disabled.

myserialdevice = serialdev(mypi,port,baudRate,dataBits,parity,stopBits) connects to a serial device using optional arguments to override the default values for baud rate, data bits, parity, and stop bits. Specify values for each argument from left to right. To use default values, omit the right-most arguments.

Examples

expand all

Connect and Exchange Data with Serial Device

You can create a connection to a serial device, write data to the device, and read data from the device.

Create a connection to the Raspberry Pi board.

mypi = raspi;

Show the location of the Tx and Rx pins, GPIO 14 (UART0_TXD) and GPIO 15 (UART0_RXD), on the GPIO header.

showPins(mypi)

Raspberry Pi hardware uses +3.3V. Do not connect Raspberry Pi hardware directly to devices that use higher voltages.

Connect the Raspberry Pi serial port to a +3.3V serial device.

  • To receive data, connect the RXD pin (GPIO 15 UART0_RXD) on the Raspberry Pi board to the TXD pin on the serial device.

  • To transmit data, connect the TXD pin (GPIO 14 UART0_TXD) on the Raspberry Pi board to the RXD pin on the serial device.

  • Connect a ground pins (GND) on the Raspberry Pi board to the GND pin on the serial device.

  • Connect one of the +3.3V pins on the Raspberry Pi board to the VCC pin on the serial device.

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

Use serialdev to create a connection to the serial device and assign the connection to a handle.

myserialdevice = serialdev(mypi,'/dev/ttyAMA0',9600)
myserialdevice = 

  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(myserialdevice,[10 12],'uint16')

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

output = read(myserialdevice,100,'uint16');

Increase the timeout period of the serial port.

myserialdevice.Timeout = 20
myserialdevice = 

  Serialdev with Properties:

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

Input Arguments

expand all

mypi — Connection to specific boardhandle

Connection to a specific board, specified as a handle.

Use raspi to create this connection.

Example: mypi

port — Serial device path and file nameserial device

Serial device path and file name, specified as a string.

Example: '/dev/ttyAMA0'

Data Types: char

baudRate — Serial device baud rate115200 (default) | 50 | 75 | 110 | 134 | 150 | 200 | 300 | 600 | 1200 | 1800 | 2400 | 4800 | 9600 | 19200 | 38400 | 57600 | 230400

Serial device baud rate, specified as a scalar. Optional.

Example: 9600

Data Types: double

dataBits — Data bits per character8 (default) | 5 | 6 | 7 | 9

Data bits per character, specified as a scalar. Optional.

Example: 8

Data Types: double

parity — Parity bit'none' (default) | 'odd' | 'even' | 'mark' | 'space'

Parity bit, specified as a string. Optional.

This setting enables and selects the type of parity-bit error detection.

Example: 'none'

Data Types: char

stopBits — Stop bits1 (default) | 1.5 | 2

Stop bits, specified as a scalar. Optional.

Example: 1

Data Types: double

Output Arguments

expand all

myserialdevice — Connection to specific serial devicehandle

Connection to a specific serial device, specified as a handle.

You can use myserialdevice with the following methods:

You can use myserialdevice to read the following properties for the serial device:

  • BaudRate

  • DataBits

  • StopBits

  • Parity

You can use myserialdevice to read and write the TimeOut property for the serial device:

See Also

| | | |

Was this topic helpful?