read
Read data from serial device
Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.
Description
returns number of bytes of data read from serial device.out
= read(serialdevObj
,numBytes
)
also specifies the data precision.out
= read(serialdevObj
,numBytes
,precision
)
Examples
Read Data from Serial Device on Arduino Hardware
Create an arduino
connection.
arduinoObj = arduino("COM16","Leonardo","Libraries",{'SPI','Serial','I2C'});
Create a connection to the serial device on the Arduino hardware.
serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate', 115200, 'DataBits',8, 'StopBits', 1, 'Parity', 'even')
serialdevObj = device with properties: Interface: 'Serial' SerialPort: 1 TxPin: 'D1' RxPin: 'D0' BaudRate: 115200 (bits/s) NumBytesAvailable: 0 Show all properties, functions
Connect the Rx and Tx pins in loopback. Write 3 bytes of data to the serial device.
write(serialdevObj,[88 99 65]);
Check if data is available to read.
numBytes = serialdevObj.NumBytesAvailable
numBytes = 3
Read data from the serial device.
read(serialdevObj,3)
ans = 1×3
88 99 65
Specify Precision to Read Data from Serial Device on Arduino Hardware
Create an arduino
connection
arduinoObj = arduino("COM16","Leonardo","Libraries",{'SPI','Serial','I2C'});
Create a connection to the serial device on the Arduino hardware.
serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate', 115200, 'DataBits',8, 'StopBits', 1, 'Parity', 'even')
serialdevObj = device with properties: Interface: 'Serial' SerialPort: 1 TxPin: 'D1' RxPin: 'D0' BaudRate: 115200 (bits/s) NumBytesAvailable: 0 Show all properties, functions
Connect the Rx and Tx pins in loopback. Write data to the serial device.
write(serialdevObj,256,'uint16');
Check if data is available to read.
numBytes = serialdevObj.NumBytesAvailable
numBytes = 2
Read data from serial device with precision of uint16.
read(serialdevObj,1,'uint16')
ans = 256
Input Arguments
serialdevObj
— Serial device connection
device object
Serial device connection, specified as a device
object.
numBytes
— Number of bytes to read
scalar
Number of bytes of data to read from the serial device, specified as a
scalar
.
Note
For the Arduino® Mega2560, Mega ADK, Leonardo, and Micro boards read
works only for up to 63 bytes of data as the Arduino libraries do not read data into last byte of serial receive buffer.
See this link for more details.
precision
— Data precision
'uint8'
(default) | 'int8'
| 'uint16'
| 'int16'
| 'uint32'
| 'int32'
| 'uint64'
| 'int64'
Data precision, specified as one of the following character vectors:
'uint8'
'int8'
'uint16'
'int16'
'uint32'
'int32'
'uint64'
'int64'
Output Arguments
out
— Data read from serial device
scalar | vector
Data read from serial device, returned as a scalar
or
vector
based on the precision
.
More About
Code Generation Using MATLAB Function Block
Use
read
in a MATLAB® Function block with the Simulink® Support Package for Arduino Hardware to generate code that can be deployed on Arduino Hardware.
Version History
Introduced in R2019b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)