主要内容

从串行设备读取数据

描述

例子

=阅读(serialdevObjnumBytes返回从串行设备读取的数据字节数。

例子

=阅读(serialdevObjnumBytes精度还指定数据精度。

例子

全部折叠

创建一个arduino连接。

arduinoObj = arduino(“COM16”“达芬奇”“库”, {SPI的“串行”“I2C”});

在Arduino硬件上创建到串行设备的连接。

serialdevObj = device(arduinoObj,“串口”, 1“波特率”, 115200,“DataBits”8“StopBits”, 1“平价”“甚至”
serialdevObj = device with properties: Interface: 'Serial' SerialPort: 1 TxPin: 'D1' RxPin: 'D0' BaudRate: 115200 (bits/s) NumBytesAvailable: 0显示所有属性,函数

连接环回中的Rx和Tx引脚。向串行设备写入3字节的数据。

write(serialdevObj,[88 99 65]);

检查是否有数据可读。

numBytes = serialdevObj。NumBytesAvailable
numBytes = 3

从串行设备读取数据。

读(serialdevObj, 3)
ans =1×388 99 65

创建一个arduino连接

arduinoObj = arduino(“COM16”“达芬奇”“库”, {SPI的“串行”“I2C”});

在Arduino硬件上创建到串行设备的连接。

serialdevObj = device(arduinoObj,“串口”, 1“波特率”, 115200,“DataBits”8“StopBits”, 1“平价”“甚至”
serialdevObj = device with properties: Interface: 'Serial' SerialPort: 1 TxPin: 'D1' RxPin: 'D0' BaudRate: 115200 (bits/s) NumBytesAvailable: 0显示所有属性,函数

连接环回中的Rx和Tx引脚。向串行设备写入数据。

写(serialdevObj, 256,“uint16”);

检查是否有数据可读。

numBytes = serialdevObj。NumBytesAvailable
numBytes = 2

从串行设备读取数据,精度为uint16。

读(serialdevObj 1“uint16”
Ans = 256

输入参数

全部折叠

串行设备连接,指定为设备对象。

从串行设备读取的数据字节数,指定为标量

请注意

对于Arduino®Mega2560, Mega ADK, Leonardo和Micro板由于Arduino库不将数据读入串行接收缓冲区的最后一个字节,因此仅适用于最多63字节的数据。看到这个链接欲知详情。

数据精度,指定为以下字符向量之一:

  • “uint8”

  • “int8”

  • “uint16”

  • “int16”

  • “uint32”

  • “int32”

  • “uint64”

  • “int64”

输出参数

全部折叠

从串行设备读取的数据,返回为标量向量基于精度

更多关于

全部折叠

使用代码生成MATLAB功能块

  • 使用在MATLAB中®函数块。金宝app®金宝appArduino硬件支持包生成可以部署在Arduino硬件上的代码。

版本历史

R2019b引入

另请参阅

||