MATLAB 帮助中心
通过 TCP/IP 读取远程主机上的数据
data = read(t)
data = read(t,count)
data = read(t,count,datatype)
data = read(t) 从 TCP/IP 客户端 t 指定的远程主机读取所有可用的数值或 ASCII 数据,并以双精度或文本类型的行向量或列向量形式返回数据。读取的值的数目由 t 的 NumBytesAvailable 属性指定。该函数暂停 MATLAB® 的执行,直到读取了指定数目的值或发生超时。
data
t
NumBytesAvailable
示例
data = read(t,count) 读取 count 个值并返回数据。
count
data = read(t,count,datatype) 读取 count 个以 datatype 指定的形式的值并返回数据。datatype 参量是标准 MATLAB 数据类型的字符向量。
datatype
全部折叠
创建一个名为 t 的 TCP/IP 客户端连接,使用端口 4000 连接到 TCP/IP 回显服务器。为此,您必须有在端口 4000 上运行的 echotcpip 服务器。
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 的执行,直到指定数量的值写入远程主机。
write
为变量 data 分配 10 个字节的 uint8 数据。
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 属性确定,即输入缓冲区中可用的字节数。
read
通过清除 TCP/IP 客户端关闭该对象与远程主机之间的连接。关闭 echotcpip 服务器。
clear t echotcpip("off")
write 函数以同步方式将数据写入连接到 t 的远程主机。首先指定数据,然后写入该数据。此函数一直等到指定数量的值写入远程主机。
为变量 data 分配 10 个字节的数据。
data = (1:10)
data = 1×10 1 2 3 4 5 6 7 8 9 10
Name Size Bytes Class Attributes data 1x10 80 double
ans = 80
对于任何读写操作,该数据类型将转换为 uint8 以便传输数据。传输后,数据类型将还原为指定的 datatype。由于一个 double 等于八个 uint8 字节,因此有 80 个字节可用。
double
由于客户端连接到回显服务器,您写入服务器的数据将返回到客户端。从服务器上读取 10 个双精度值。对象名称始终是第一个参量。size 参量必须为第二个参量,datatype 必须为第三个参量。
size
read(t,10,"double")
ans = 1×10 1 2 3 4 5 6 7 8 9 10
tcpclient
TCP/IP 客户端,指定为 tcpclient 对象。
示例: read(t) 从 TCP/IP 客户端 t 读取所有可用数据。
要读取的值的数目,指定为正整数值。如果 count 大于 t 的 NumBytesAvailable 属性,该函数将暂停 MATLAB 的执行并等待,直到读取了指定的数据量或发生超时。
示例: read(t,5) 读取五个 uint8 类型的数据值。
read(t,5)
数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
single
int8
int16
int32
int64
uint16
uint32
uint64
"uint8"
"int8"
"uint16"
"int16"
"uint32"
"int32"
"uint64"
"int64"
"single"
"double"
"char"
"string"
每个值的大小和格式,指定为字符向量或字符串。datatype 确定对每个值要读取的字节数以及如何将这些字节解释为 MATLAB 数据类型。
示例: read(t,10,"double") 读取 10 个双精度数据值。
数据类型: char | string
char
string
全部展开
在 R2014b 中推出
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 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
欧洲
亚太
联系您当地的办事处