Documentation Center

  • Trial Software
  • Product Updates

read

Read data from serial device

Syntax

  • serialData = read(myserialdevice,count)
  • serialData = read(myserialdevice,count,precision)

Description

serialData = read(myserialdevice,count) returns data from the serial device.

The read method stops when it finishes reading the specified data from the device, or when the timeout period elapses.

serialData = read(myserialdevice,count,precision) overrides the default value for precision.

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

myserialdevice — Connection to serial devicehandle

Connection to a serial device, specified as a handle.

Use serialdev to create this connection.

Example: myserialdevice

count — Data sizedata size

Data size to read from the serial device, specified as a scalar.

Example: 2

Data Types: double

precision — Data precision'uint8' (default) | 'char' | 'int8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'single' | 'double'

Data precision, specified as a string. Optional.

Example: 'uint8'

Data Types: char

Output Arguments

expand all

serialData — Data from serial deviceserial data

Data from the serial device, returned in the device- or user-defined format.

See Also

| | |

Was this topic helpful?