Read data from serial port
reads the number of values specified by
data = read(
count in the form specified by
datatype from the serial port connection
For all numeric
data is a row
vector of double values. For the text type
datatype values of
data is of
the specified type. The function suspends MATLAB® execution until the specified number of values are read or a timeout
Write and Read Data with Serial Port Device
Create a connection to a serial port device. In this example, the serial port at
COM3 is connected to a loopback device.
device = serialport("COM3",9600)
device = Serialport with properties: Port: "COM3" BaudRate: 9600 NumBytesAvailable: 0 Show all properties, functions
Write the values
[1,2,3,4,5] in uint8 format.
Since the port is connected to a loopback device, the data you write to the device is returned to MATLAB. Read all the data.
ans = 1×5 1 2 3 4 5
device — Serial port connection
Serial port connection, specified as a
read(device,20,"uint32") reads data from the serial port
count — Number of values to read
Number of values to read, specified as a positive integer value. If
count is greater than the
device, the function suspends MATLAB execution and waits until the specified amount of data is read or a
read(device,5,"uint32") reads five values of uint32 data.
Each uint32 value is four bytes, for a total of 20 bytes read.
datatype — Size and format of each value
Size and format of each value, specified as a character vector or string.
datatype determines the number of bytes to read for each value
and the interpretation of those bytes as a MATLAB data type.
read(device,5,"uint16") reads five values of uint16 data.
Each uint16 value is two bytes, for a total of 10 bytes read.
Introduced in R2019b