Matlab TCP/IP 服务器套接字未发送准确数据 [英] Matlab TCP/IP Server Sockets not sending accurate data
问题描述
我使用 Matlab R2019A 文档中关于 TCP/IP 连接的示例通过 TCP 在两个 Matlab 实例之间来回发送数据.
虽然收到的数据如下图所示:
如何确保正在发送的数据完全按原样接收?
我认为您需要做的是指定正在发送和接收的数据的精度(以确保正确解释字节):
>
% 发送fwrite(t, data, 'double');% 获得 1000 双打数据 = fread(t, 1000, 'double');
这至少是 Mathworks 建议的.请参阅部分 读取二进制数据
,以及fread 和的文档fwrite.
I am using the example from the Matlab R2019A documentation about TCP/IP connection to send data back and forth two Matlab instances over TCP.
https://www.mathworks.com/help/instrument/communicate-using-tcpip-server-sockets.html
I have a TCP client (client.m) file written as:
data = sin(1:64);
plot(data);
t = tcpip('localhost', 30000, 'NetworkRole', 'client');
fopen(t)
fwrite(t, data)
and a TCP server (server.m) file written as:
t = tcpip('0.0.0.0', 30000, 'NetworkRole', 'server');
fopen(t);
data = fread(t, t.BytesAvailable);
plot(data);
However, the data being sent from the client is a double
array, but the data received is a integer
array.
The data sent looks like the following plot: While the data received looks like the following plot:
How can I ensure that the data that is being sent is received exactly as it is?
I think what you need to do, is to specify also the precision of the data being send and received (to ensure the bytes are interpreted correctly):
% to send
fwrite(t, data, 'double');
% to receive 1000 doubles
data = fread(t, 1000, 'double');
This is at least what Mathworks suggests. See section Reading binary data
, as well as docs for fread and fwrite.
这篇关于Matlab TCP/IP 服务器套接字未发送准确数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!