Sanjoy Basak
Sanjoy Basak

Reputation: 33

float data transmit and receive with 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?

Upvotes: 1

Views: 865

Answers (1)

horchler
horchler

Reputation: 18484

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.

Upvotes: 1

Related Questions