MATLAB 帮助中心
本页翻译不是最新的。点击此处可查看最新英文版本。
从串行端口读取数据
data = read(device,count,datatype)
data = read(device,count,datatype) 从串行端口连接 device 以 datatype 指定的格式读取 count 个值。对于所有数值 datatype 类型,data 是双精度值的行向量。对于 "char" 或 "string" 的文本类型 datatype,data 的值属于指定类型。该函数暂停 MATLAB® 的执行,直到读取了指定数目的值或发生超时。
data
device
count
datatype
"char"
"string"
示例
全部折叠
创建到串行端口设备的连接。在此示例中,COM3 上的串行端口连接到环回设备。
COM3
device = serialport("COM3",9600)
device = Serialport with properties: Port: "COM3" BaudRate: 9600 NumBytesAvailable: 0 Show all properties, functions
以 uint8 格式写入值 [1,2,3,4,5]。
[1,2,3,4,5]
write(device,1:5,"uint8")
由于该端口连接到环回设备,您写入该设备的数据将返回到 MATLAB。读取所有数据。
read(device,5,"uint8")
ans = 1×5 1 2 3 4 5
serialport
串行端口连接,指定为 serialport 对象。
示例: read(device,20,"uint32") 从串行端口连接 device 读取数据。
read(device,20,"uint32")
要读取的值的数目,指定为正整数值。如果 count 大于 device 的 NumBytesAvailable 属性,该函数将暂停 MATLAB 的执行并等待,直到读取了指定的数据量或发生超时。
NumBytesAvailable
示例: read(device,5,"uint32") 读取 uint32 数据的五个值。每个 uint32 值为 4 个字节,总共读取 20 个字节。
read(device,5,"uint32")
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
single
double
int8
int16
int32
int64
uint8
uint16
uint32
uint64
"uint8"
"int8"
"uint16"
"int16"
"uint32"
"int32"
"uint64"
"int64"
"single"
"double"
每个值的大小和格式,指定为字符向量或字符串。datatype 确定对每个值要读取的字节数以及如何将这些字节解释为 MATLAB 数据类型。
示例: read(device,5,"uint16") 读取 uint16 数据的五个值。每个 uint16 值为两个字节,总共读取 10 个字节。
read(device,5,"uint16")
数据类型: char | string
char
string
在 R2019b 中推出
readline
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 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处