MATLAB 帮助中心
将数据写入 Bluetooth 设备
write(device,data)
write(device,data,datatype)
write(device,data) 将行向量或列向量 data 写入 Bluetooth® 连接 device。
device
data
示例
write(device,data,datatype) 以 datatype 指定的格式写入 data,而不考虑 data 采用哪种格式。
datatype
全部折叠
创建到附近的 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 写入设备。
1:10
write(device,1:10)
由于设备配置为环回设备,写入设备的数据将返回到 MATLAB®。读取所有数据。
read(device,10)
ans = 1×10 1 2 3 4 5 6 7 8 9 10
将字符串 "helloworld" 写入设备。
"helloworld"
write(device,"helloworld","string")
由于设备配置为环回设备,写入设备的数据将返回到 MATLAB。以字符串形式读取数据的前五个值。
read(device,5,"string")
ans = "hello"
再次使用 read 命令读取接下来的五个数据值。
read
ans = "world"
bluetooth
Bluetooth 设备连接,指定为 bluetooth 对象。
示例: write(device,10) 将数据写入 Bluetooth 连接 device。
write(device,10)
N
数值或 ASCII 数据,指定为由数值组成的 1×N 向量或文本类型的字符向量或字符串标量。对于所有数值 datatype 类型,data 是值的行向量。
示例: write(device,300) 写入值 300。
write(device,300)
300
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string
single
double
int8
int16
int32
int64
uint8
uint16
uint32
uint64
char
string
"uint8"
"int8"
"uint16"
"int16"
"uint32"
"int32"
"uint64"
"int64"
"single"
"double"
"char"
"string"
每个值的大小和格式,指定为字符向量或字符串。datatype 确定对每个值要写入的字节数以及如何将这些字节解释为 MATLAB 数据类型。
对于任何读写操作,该数据类型将转换为 uint8 以便传输数据。传输后,数据类型将还原为指定的 datatype。
示例: write(device,1:5,"double") 将值 [1,2,3,4,5] 作为双精度数据写入。
write(device,1:5,"double")
[1,2,3,4,5]
数据类型: char | string
在 R2020b 中推出
writeline
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处