read

Read data from serial port

Description

example

data = read(device,count,precision) reads the number of values specified by count, with the data type specified by precision, from the device connected to the serial port device. For all numeric precision types, data is a row vector of double values. For the text type precision values of 'char' or 'string', data is of the specified type.

The function blocks MATLAB and waits until the specified number of values are read from the serial port, or a timeout occurs.

Examples

collapse all

Read five values of uint32 data from the COM3 serial port. These five values at 4 bytes each are a total of 20 bytes.

s = serialport("COM3",9600);
data = read(s,5,"uint32");

Input Arguments

collapse all

Serial port, specified as a serialport object.

Example: serialport()

Number of items to read, specified as a scalar positive integer numeric value. You cannot specify a count of inf or NaN. If count is greater than the NumBytesAvailable property of device, the function blocks MATLAB and waits until the specified amount of data is read or a timeout occurs.

Example: 20

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size and format of each value, specified as a character vector or string. precision determines the number of bits to read for each value and the interpretation of those bits as a MATLAB data type.

Example: 'int16'

Data Types: char | string

Output Arguments

collapse all

Numeric or ASCII data read, returned as a 1-by-N vector of doubles or text. For all numeric precision types, data is a row vector of double values. For the text type precision values of 'char' or 'string', data is of the specified type.

See Also

Functions

Introduced in R2019b