Main Content

read

Bluetooth 设备读取数据

自 R2020b 起

说明

示例

data = read(device,count) 从 Bluetooth® 连接 device 读取由 count 指定的数目的值,并以双精度或文本类型的行向量或列向量形式返回数据。该函数暂停 MATLAB® 的执行,直到读取了指定数目的值或发生超时。

示例

data = read(device,count,datatype) 读取 count 个以 datatype 指定的形式的值并返回数据。datatype 参量是标准 MATLAB 数据类型的字符向量。对于所有数值 datatype 类型,data 是双精度值的行向量。对于 "char""string" 的文本类型 datatypedata 的值属于指定类型。

示例

全部折叠

创建到附近的 Bluetooth 设备的连接。在此示例中,HC-06 Bluetooth 模块配置为环回设备。

device = bluetooth("HC-06")
device = 
  bluetooth with properties:

                 Name: "HC-06"
              Address: "98D331FB3B77"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

将值 1:10 写入设备。

write(device,1:10)

由于设备配置为环回设备,写入设备的数据将返回到 MATLAB。读取所有数据。

read(device,10)
ans = 1×10

     1     2     3     4     5     6     7     8     9    10

创建到附近的 Bluetooth 设备的连接。在此示例中,HC-06 Bluetooth 模块配置为环回设备。

device = bluetooth("HC-06")
device = 
  bluetooth with properties:

                 Name: "HC-06"
              Address: "98D331FB3B77"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

将字符串 "helloworld" 写入设备。

write(device,"helloworld","string")

由于设备配置为环回设备,写入设备的数据将返回到 MATLAB。以字符串形式读取数据的前五个值。

read(device,5,"string")
ans = 

    "hello"

再次使用 read 命令读取接下来的五个数据值。

read(device,5,"string")
ans = 

    "world"

输入参数

全部折叠

Bluetooth 设备连接,指定为 bluetooth 对象。

示例: data = read(device,5) 从 Bluetooth 连接 device 读取数据。

要读取的值的数目,指定为正整数值。如果 count 大于 deviceNumBytesAvailable 属性,该函数将暂停 MATLAB 的执行并等待,直到读取了指定的数据量或发生超时。

示例: read(device,2) 读取 uint8 数据的两个值。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

每个值的大小和格式,指定为字符向量或字符串。datatype 确定对每个值要读取的字节数以及如何将这些字节解释为 MATLAB 数据类型。

示例: read(device,1,"uint16") 读取 uint16 数据的值。每个 uint16 值占用两个字节。

数据类型: char | string

版本历史记录

在 R2020b 中推出

另请参阅

函数