找到你要的答案

Q:float data transmit and receive with tcpip matlab?

Q:浮点数据的发送和接收用TCPIP的MATLAB?

I am trying to transmit and receive data over TCP/IP (interfacing with GNU Radio):

data = rand(1,128);
t = tcpip('127.0.0.1',2012,'Timeout', 120);
s = whos('data');
set(t,'OutputBufferSize',s.bytes);
fopen(t);
fwrite(t,data,'double')
m=tcpip('127.0.0.1',2022,'Timeout', 120);
set(m,'InputBufferSize',s.bytes)
fopen(m);
fread(m)

When I read my data, these are all 0 or 1, I need to get the float data that I am transmitting for my application. Could someone please tell me how to do that?

我试图发送和接收的数据通过TCP / IP(GNU无线电接口):

data = rand(1,128);
t = tcpip('127.0.0.1',2012,'Timeout', 120);
s = whos('data');
set(t,'OutputBufferSize',s.bytes);
fopen(t);
fwrite(t,data,'double')
m=tcpip('127.0.0.1',2022,'Timeout', 120);
set(m,'InputBufferSize',s.bytes)
fopen(m);
fread(m)

当我阅读我的数据,这些都是0或1,我需要得到的浮动数据,我为我的应用程序传输。有人能告诉我怎么做吗?

answer1: 回答1:

I can't replicate your issue with all zeros or ones, but I think that you need to use one of the additional input arguments to icinterface/fread. Here's a simple example:

data = rand(128, 1);
echotcpip('on', 2012);
t = tcpip('127.0.0.1', 2012, 'Timeout', 120);
bytes_per_double = 8;
set(t, 'InputBufferSize', bytes_per_double*numel(data), ...
       'OutputBufferSize', bytes_per_double*numel(data));
fopen(t);
fwrite(t,data(:),'double');
data_out=fread(t, t.InputBufferSize/bytes_per_double, 'double'); % Read in as doubles
echotcpip('off');
fclose(t);
delete(t);
isequal(data, data_out)

The values in data and data_out should both be doubles and equal to each other.

我不能与所有的0或1复制你的问题,但我认为你需要使用一个额外的输入参数icinterface /中。这里有一个简单的例子:

data = rand(128, 1);
echotcpip('on', 2012);
t = tcpip('127.0.0.1', 2012, 'Timeout', 120);
bytes_per_double = 8;
set(t, 'InputBufferSize', bytes_per_double*numel(data), ...
       'OutputBufferSize', bytes_per_double*numel(data));
fopen(t);
fwrite(t,data(:),'double');
data_out=fread(t, t.InputBufferSize/bytes_per_double, 'double'); % Read in as doubles
echotcpip('off');
fclose(t);
delete(t);
isequal(data, data_out)

在数据和data_out值都应该是双打和相等。

matlab  tcp  gnuradio