Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: TCP communication of 2 computers running Matlab

Subject: TCP communication of 2 computers running Matlab

From: steven.bak

Date: 13 Jun, 2008 12:44:53

Message: 1 of 5

I am trying to send/receive data between two computers running Matlab
with the tcpip object.
From what I can tell, there is no way to have Matlab "LISTEN" for the
other computer to open a TCP connection.

Can someone verify this?
I find it strange we have the ability to open TCP connections, but do
not have the ability to allow another application to open a TCP
connection with Matlab.

Has anyone been able to get Matlab to listen for tcpip connections?
Before I make this project more complicated, I want to make sure I am
not missing an easier route.

Thanks,
Steve

Subject: TCP communication of 2 computers running Matlab

From: Trent Jarvi

Date: 13 Jun, 2008 21:52:22

Message: 2 of 5


"steven.bak" <steven.bak@gmail.com> wrote in message
news:7ada5081-a6ba-4243-91ae-bf7596897068@z66g2000hsc.googlegroups.com...
>I am trying to send/receive data between two computers running Matlab
> with the tcpip object.
> From what I can tell, there is no way to have Matlab "LISTEN" for the
> other computer to open a TCP connection.
>
> Can someone verify this?
> I find it strange we have the ability to open TCP connections, but do
> not have the ability to allow another application to open a TCP
> connection with Matlab.
>
> Has anyone been able to get Matlab to listen for tcpip connections?
> Before I make this project more complicated, I want to make sure I am
> not missing an easier route.

Hi Steve,

The TCPIP client implementation was initially for communicating with
instruments that handled the server side. We are listening to feedback from
customers regarding server sockets. For now, it is possible to use the UDP
connections while binding to LocalPorts.


>> u=udp('localhost', 'LocalPort', 15000, 'RemotePort', 15001);
>> u2=udp('127.0.0.1','LocalPort', 15001, 'RemotePort', 15000);
>> fopen(u);
>> fopen(u2);
>> fprintf(u, 'echo this string\n');
>> fscanf(u2)

ans =

echo this string


Subject: TCP communication of 2 computers running Matlab

From: steven.bak

Date: 1 Jul, 2008 01:58:41

Message: 3 of 5

On Jun 13, 5:52 pm, "Trent Jarvi" <tja...@mathworks.com> wrote:
> "steven.bak" <steven....@gmail.com> wrote in message
>
> news:7ada5081-a6ba-4243-91ae-bf7596897068@z66g2000hsc.googlegroups.com...
>
> >I am trying to send/receive data between two computers running Matlab
> > with the tcpip object.
> > From what I can tell, there is no way to have Matlab "LISTEN" for the
> > other computer to open aTCPconnection.
>
> > Can someone verify this?
> > I find it strange we have the ability to openTCPconnections, but do
> > not have the ability to allow another application to open aTCP
> > connection with Matlab.
>
> > Has anyone been able to get Matlab to listen for tcpip connections?
> > Before I make this project more complicated, I want to make sure I am
> > not missing an easier route.
>
> Hi Steve,
>
> The TCPIP client implementation was initially for communicating with
> instruments that handled the server side. We are listening to feedback from
> customers regarding server sockets. For now, it is possible to use the UDP
> connections while binding to LocalPorts.
>
> >> u=udp('localhost', 'LocalPort', 15000, 'RemotePort', 15001);
> >> u2=udp('127.0.0.1','LocalPort', 15001, 'RemotePort', 15000);
> >> fopen(u);
> >> fopen(u2);
> >> fprintf(u, 'echo this string\n');
> >> fscanf(u2)
>
> ans =
>
> echo this string

Trent,
Thank you for your response. It has been helpful. I am seeing some
weird behavior while using fread on a UDP object.

%-------- Computer Writing data
u=udp('192.168.1.135', 'LocalPort', 15000, 'RemotePort', 15001);
set(u, 'OutputBufferSize', 4048);
set(u, 'ByteOrder', 'littleEndian');
fopen(u);
data = [1:512];
fwrite(u,data,'int16');

%-------- Computer Recieving Data
u2=udp('192.168.1.122', 'LocalPort', 15001, 'RemotePort', 15000);
set(u2, 'InputBufferSize', 4048, 'ByteOrder', 'littleEndian');
fopen(u2)



I first execute the code that will receive, then execute the code to
write.
I enter this command on the receiving computer: (u2 displays 512
BytesAvailable)

A = fread(u2,512,'int16')

A contains the values 257-512 in a double array. What happened to
1-256? (u2 displays 0 BytesAvailable and 256 BytesReceived)
If I redo the test with:
A = fread(u2,512,'int16=>int16')
I get an error. Error using udp.fread at 211. Invalid PRECISION
specified.
The help on fread said to specify the destination type because DOUBLE
was default. How can I read that udp object and store it into an
int16? AND Get ALL the data?


Thanks,
I hope this isn't too unclear, It has been a long day.

Steve

Subject: TCP communication of 2 computers running Matlab

From: Ankit Desai

Date: 1 Jul, 2008 03:52:01

Message: 4 of 5

"steven.bak" <steven.bak@gmail.com> wrote in message <b04cd41d-91cc-
4e00-884a-d4387b008a27@m36g2000hse.googlegroups.com>...
> On Jun 13, 5:52 pm, "Trent Jarvi" <tja...@mathworks.com> wrote:
> > "steven.bak" <steven....@gmail.com> wrote in message
> >
> > news:7ada5081-a6ba-4243-91ae-
bf7596897068@z66g2000hsc.googlegroups.com...
> >
> > >I am trying to send/receive data between two computers running
Matlab
> > > with the tcpip object.
> > > From what I can tell, there is no way to have Matlab "LISTEN" for the
> > > other computer to open aTCPconnection.
> >
> > > Can someone verify this?
> > > I find it strange we have the ability to openTCPconnections, but do
> > > not have the ability to allow another application to open aTCP
> > > connection with Matlab.
> >
> > > Has anyone been able to get Matlab to listen for tcpip connections?
> > > Before I make this project more complicated, I want to make sure I am
> > > not missing an easier route.
> >
> > Hi Steve,
> >
> > The TCPIP client implementation was initially for communicating with
> > instruments that handled the server side. We are listening to feedback
from
> > customers regarding server sockets. For now, it is possible to use the
UDP
> > connections while binding to LocalPorts.
> >
> > >> u=udp('localhost', 'LocalPort', 15000, 'RemotePort', 15001);
> > >> u2=udp('127.0.0.1','LocalPort', 15001, 'RemotePort', 15000);
> > >> fopen(u);
> > >> fopen(u2);
> > >> fprintf(u, 'echo this string\n');
> > >> fscanf(u2)
> >
> > ans =
> >
> > echo this string
>
> Trent,
> Thank you for your response. It has been helpful. I am seeing some
> weird behavior while using fread on a UDP object.
>
> %-------- Computer Writing data
> u=udp('192.168.1.135', 'LocalPort', 15000, 'RemotePort', 15001);
> set(u, 'OutputBufferSize', 4048);
> set(u, 'ByteOrder', 'littleEndian');
> fopen(u);
> data = [1:512];
> fwrite(u,data,'int16');
>
> %-------- Computer Recieving Data
> u2=udp('192.168.1.122', 'LocalPort', 15001, 'RemotePort', 15000);
> set(u2, 'InputBufferSize', 4048, 'ByteOrder', 'littleEndian');
> fopen(u2)
>
>
>
> I first execute the code that will receive, then execute the code to
> write.
> I enter this command on the receiving computer: (u2 displays 512
> BytesAvailable)
>
> A = fread(u2,512,'int16')
>
> A contains the values 257-512 in a double array. What happened to
> 1-256? (u2 displays 0 BytesAvailable and 256 BytesReceived)
> If I redo the test with:
> A = fread(u2,512,'int16=>int16')
> I get an error. Error using udp.fread at 211. Invalid PRECISION
> specified.
> The help on fread said to specify the destination type because DOUBLE
> was default. How can I read that udp object and store it into an
> int16? AND Get ALL the data?
>
>
> Thanks,
> I hope this isn't too unclear, It has been a long day.
>
> Steve

Hi Steve,

The argument passed to fread helps the function interpret the data in the
buffer. In order to convert the received data to int16 or any other datatype
you might want to use either 'typecast' or converting functions like int16()
depending upon your usecase.

Hope this helps.

-Ankit

Subject: TCP communication of 2 computers running Matlab

From: steven.bak

Date: 19 Jul, 2008 20:56:46

Message: 5 of 5

This issue has me pulling my hair out.

Ankit, Thanks for the cast help, but that did not address my problem.
(It does help however B = cast(A,'int16') in order to play the audio
back via wavplay(B) )
I believe there is a bug when using fread on a UDP object.

I am sending int16 data with fwrite:
data = [1:512]
data1 = cast(data, 'int16')
fwrite(u,data1,'int16');

Receiving end is broke.
1. A = fread(u2,512,'int16'); %% A is of type Double. How can I
specify the output type to be int16???
OR
2. A = fread(u2,512,'int16=>int16'); %% Error using udp.fread at
211Invalid Precision Specified...... This is directly out of help
file, why the error?


Finally
A = fread(u2,1,'int16')
ans =

1
2
.
.
.
256

3. WHY did it read 256 values instead of 1 (Specified by count)???
Seems to read 512 regardless of what is specified by count.

Questions are numbered above 1, 2, and 3. Any assistance is greatly
appreciated!


Thanks,
Steve

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
udp Ankit Desai 30 Jun, 2008 23:55:11
tcpip Ankit Desai 30 Jun, 2008 23:55:11
server sockets Ankit Desai 30 Jun, 2008 23:55:11
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics