read
通过 TCP/IP 读取远程主机上的数据
说明
示例
向远程主机写入和从中读取 uint8 数据
创建一个名为 t
的 TCP/IP 客户端连接,使用端口 4000 连接到 TCP/IP 回显服务器。为此,您必须有在端口 4000 上运行的 echotcpip
服务器。
echotcpip("on",4000) t = tcpclient("localhost",4000)
t = tcpclient with properties: Address: 'localhost' Port: 4000 NumBytesAvailable: 0 Show all properties, functions
write
函数以同步方式将数据写入连接到 t
的远程主机。首先指定数据,然后写入该数据。此函数将暂停 MATLAB 的执行,直到指定数量的值写入远程主机。
为变量 data
分配 10 个字节的 uint8
数据。
data = uint8(1:10)
data = 1×10 uint8 row vector
1 2 3 4 5 6 7 8 9 10
查看数据。
whos data
Name Size Bytes Class Attributes data 1x10 10 uint8
将数据写入回显服务器。
write(t,data)
通过查看 NumBytesAvailable
属性确认写入操作成功。
t.NumBytesAvailable
ans = 10
由于客户端连接到回显服务器,您写入服务器的数据将返回到客户端。读取所有可用的数据类型。
read(t)
ans = 1×10 uint8 row vector
1 2 3 4 5 6 7 8 9 10
如果使用不带任何参数的 read
函数,则会从连接到远程主机的 t
中读取所有可用字节数据,并返回这些数据。读取的值的数目由 NumBytesAvailable
属性确定,即输入缓冲区中可用的字节数。
通过清除 TCP/IP 客户端关闭该对象与远程主机之间的连接。关闭 echotcpip
服务器。
clear t echotcpip("off")
指定要从远程主机读取的数据大小和数据类型
创建一个名为 t
的 TCP/IP 客户端连接,使用端口 4000 连接到 TCP/IP 回显服务器。为此,您必须有在端口 4000 上运行的 echotcpip
服务器。
echotcpip("on",4000) t = tcpclient("localhost",4000)
t = tcpclient with properties: Address: 'localhost' Port: 4000 NumBytesAvailable: 0 Show all properties, functions
write
函数以同步方式将数据写入连接到 t
的远程主机。首先指定数据,然后写入该数据。此函数一直等到指定数量的值写入远程主机。
为变量 data
分配 10 个字节的数据。
data = (1:10)
data = 1×10
1 2 3 4 5 6 7 8 9 10
查看数据。
whos data
Name Size Bytes Class Attributes data 1x10 80 double
将数据写入回显服务器。
write(t,data)
通过查看 NumBytesAvailable
属性确认写入操作成功。
t.NumBytesAvailable
ans = 80
对于任何读写操作,该数据类型将转换为 uint8
以便传输数据。传输后,数据类型将还原为指定的 datatype
。由于一个 double
等于八个 uint8
字节,因此有 80 个字节可用。
由于客户端连接到回显服务器,您写入服务器的数据将返回到客户端。从服务器上读取 10 个双精度值。对象名称始终是第一个参数。size
参数必须为第二个参数,datatype
必须为第三个参数。
read(t,10,"double")
ans = 1×10
1 2 3 4 5 6 7 8 9 10
通过清除 TCP/IP 客户端关闭该对象与远程主机之间的连接。关闭 echotcpip
服务器。
clear t echotcpip("off")
输入参数
t
— TCP/IP 客户端
tcpclient
对象
TCP/IP 客户端,指定为 tcpclient
对象。
示例: read(t)
从 TCP/IP 客户端 t
读取所有可用数据。
count
— 要读取的值的数目
数值
要读取的值的数目,指定为正整数值。如果 count
大于 t
的 NumBytesAvailable
属性,该函数将暂停 MATLAB 的执行并等待,直到读取了指定的数据量或发生超时。
示例: read(t,5)
读取五个 uint8
类型的数据值。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
datatype
— 每个值的大小和格式
"uint8"
(默认) | "int8"
| "uint16"
| "int16"
| "uint32"
| "int32"
| "uint64"
| "int64"
| "single"
| "double"
| "char"
| "string"
每个值的大小和格式,指定为字符向量或字符串。datatype
确定对每个值要读取的字节数以及如何将这些字节解释为 MATLAB 数据类型。
示例: read(t,10,"double")
读取 10 个双精度数据值。
数据类型: char
| string
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
版本历史记录
在 R2014b 中推出
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)