write
说明
示例
用串行端口设备读写数据
创建到串行端口设备的连接。在此示例中,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]
。
write(device,1:5,"uint8")
由于该端口连接到环回设备,您写入该设备的数据将返回到 MATLAB。读取所有数据。
read(device,5,"uint8")
ans = 1×5 1 2 3 4 5
输入参数
device
— 串行端口连接
serialport
对象
串行端口连接,指定为 serialport
对象。
示例: write(device,1:5,"uint8")
写入串行端口连接 device
。
data
— 数值或 ASCII 数据
数值 | 字符向量 | 字符串标量
数值或 ASCII 数据,指定为由数值组成的行 (1×N
) 或列 (N
×1) 向量或文本类型的字符向量或字符串标量。对于所有数值 datatype
类型,data
是值的行向量。
示例: write(device,[20:24],"int16")
写入值 [20,21,22,23,24]
。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| char
| string
datatype
— 每个值的大小和格式
"uint8"
| "int8"
| "uint16"
| "int16"
| "uint32"
| "int32"
| "uint64"
| "int64"
| "single"
| "double"
| "char"
| "string"
每个值的大小和格式,指定为字符向量或字符串。datatype
确定对每个值要写入的字节数以及如何将这些字节解释为 MATLAB 数据类型。对于 ASCII 文本,您可以将 datatype
指定为 "char"
或 "string"
。
示例: write(device,1:5,"int16")
以 int16 数据类型格式写入数据。
数据类型: char
| string
版本历史记录
在 R2019b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
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)