Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HELP! ! Send data to remote machine using TCP/IP or UDP

663 views
Skip to first unread message

Karan

unread,
Jun 17, 2010, 7:57:04 PM6/17/10
to
Hey,

I am trying to make a simple simulink model for testing purpose and if it works then use it in a project. I need to send data to remote machine but on the same gateway (192.168.1.1, I am using a router). I tried to send data on the same machine say 192.168.1.101:30000 and receive on the same machine with the same port and ip, it works fine.

But when I try sending data to 30000 and go to other machine with ip 192.168.1.102 and in TCP/IP receive block I set receive data from 192.168.1.101:30000, it gives me an ERROR. :S

So is it possible to send data to remote machine? (UDP or TCP/IP, doesn't matter !)

Note: My simulation was running the whole time... (it was set on inf). I tried first running it on the one comp and then run it on the comp that receives it but no luck.

Please help ! ! !:) :)
Thank you !

Walter Roberson

unread,
Jun 17, 2010, 10:47:46 PM6/17/10
to
Karan wrote:

> But when I try sending data to 30000 and go to other machine with ip
> 192.168.1.102 and in TCP/IP receive block I set receive data from
> 192.168.1.101:30000, it gives me an ERROR. :S

Matlab cannot act as a TCP server -- that is, it cannot be the
destination that a TCP connection is initiated to.

> So is it possible to send data to remote machine? (UDP or TCP/IP,
> doesn't matter !)

You have to use UDP and you have to specifically bind the local port so
that the other machine knows the right place to send to.

Karan

unread,
Jun 18, 2010, 1:12:05 PM6/18/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <m7BSn.39325$7d5....@newsfe17.iad>...

Thank you for the quick reply. :)

Can you guide on how to bind the specific local port ?

Once the local port is set, I should enter IP address of the machine I want to send:192.168.1.102, UDP port and local port in send block and then in the receive block same info., am I correct?

Thank you ! :)

Karan

unread,
Jun 18, 2010, 5:22:03 PM6/18/10
to
"Karan " <kara...@gmail.com> wrote in message <hvg9d5$b4f$1...@fred.mathworks.com>...

Hey..
I figured it out how to do it...but thanks for guiding me right direction.

For anyone else....
Following instruction will help you:
1) UDP Send Block
Example:
IP:192.168.1.102 (IP of the remote machine you want to send the data to - Host IP)
Port:35353
Local port:4444 (It is very important to define local port, Thanks to Walter ...why see above posts)

2)UDP Receive Block
IP:192.168.1.101 (IP of the machine you are going to receive data from - Server IP)
Port: 4444 (Same as local port of Server....NO IDEA WHY)
Local port: 35353 (Same as port of Server...NO IDEA WHY Again )

Make sure ports are available, do the port verification !
Done, Run the sumulation and waallahh !!

Walter Roberson

unread,
Jun 18, 2010, 5:44:51 PM6/18/10
to
Karan wrote:

> For anyone else....
> Following instruction will help you:
> 1) UDP Send Block
> Example:
> IP:192.168.1.102 (IP of the remote machine you want to send the data to
> - Host IP)
> Port:35353
> Local port:4444 (It is very important to define local port, Thanks to
> Walter ...why see above posts)
>
> 2)UDP Receive Block
> IP:192.168.1.101 (IP of the machine you are going to receive data from -
> Server IP)
> Port: 4444 (Same as local port of Server....NO IDEA WHY)
> Local port: 35353 (Same as port of Server...NO IDEA WHY Again )

Think of it like mailing letters to each other; when you send out a letter
then you have to put the address of the other person in the destination area,
and you have to put your own address in the return address area; when the
other person gets your letter, they address their reply by reading your
address out of the return area and writing that in as the destination for the
letter, and they put in their address in the return address of the outgoing
message.

It is true that the IP address contains some of this kind of information, but
think of an IP address as giving the address of an apartment building: you
need to know the room number as well in order to get to the right location.

Karan

unread,
Jun 21, 2010, 1:30:21 PM6/21/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hvgpgi$6l3$1...@canopus.cc.umanitoba.ca>...

Hey
Walter, you are amazing.....

Thanks to you, I got my simulation running and I was able to send data to remote machine :).
I want to try something new, I am not sure if it possible or not so before I start my research I thought I would have expert's opinion.

This is what I want to do, I would like to use GUI to run simulation. GUI is on Comp1, and simulation file is on Comp2. I want to use my GUI to run simulation on Comp2 and send data back to Comp1. Is this possible? If so please guide me where to start.

I have GUI and simulink file ready for the simulation.

Thank you very much ! :)

Walter Roberson

unread,
Jun 21, 2010, 4:11:28 PM6/21/10
to
Karan wrote:

> This is what I want to do, I would like to use GUI to run simulation.
> GUI is on Comp1, and simulation file is on Comp2. I want to use my GUI
> to run simulation on Comp2 and send data back to Comp1. Is this
> possible? If so please guide me where to start.
>
> I have GUI and simulink file ready for the simulation.

Yes, there should not be any great problem with that.

This will be UDP, so you cannot count on replies making it through.

On the receiver, Comp2, open the UDP block and start looping. In each loop,
send out a "I am here" message, wait for a bit of time, test to see if you got
anything in response, and if you did not, loop back around to try again. If
you did get something and it was big enough to be a full command, read the
command out of the link, break it up in to arguments, and call the simulation
file the appropriate arguments. When the run is finished, enter a loop. In
each loop, send out the results, wait for a bit of time, test to see if you
got anything in response, and if you did not, loop back and try again; if you
did get an acknowledgement then you are done this cycle. If though you got
another command then the implication is that your sending of the command
response did not get through and you need to send it again.

On the other side, Comp1, open the UDP block and start looping. In each loop,
wait for a bit of time, test to see if you got anything, and if not then loop
back around again; if you did get a "I am here" message, then send the command
to the link. Enter a loop. In each loop, wait a bit of time, test to see if
you got a response and if not then loop back again; if you did get a result
response then you are done this cycle so send an acknowledgement. If, though,
what you got back was instead an "I am here" message then the implication is
that the command did not make it over to the other side and you need to resend
the command.

Karan

unread,
Jun 21, 2010, 6:49:05 PM6/21/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hvoh5n$eeg$1...@canopus.cc.umanitoba.ca>...

Hey

Thanks again.
Sorry but I am could not understand correctly. And I might not have given enough information to you.

Say Comp1 has a GUI in which I can enter number (constant), so if the simulink model was on local comp I would just use set_param to set parameter in simulink. But GUI is on comp1 while simulink model is on comp2. That model has a constant block in which I can set a integer. I want to set that on comp2 while I enter number on comp1. Once that is done, for now I want to add one (I can do that using simulink block) and send the result back to comp1.

This is what I would like to do.....
THANK YOU VERY MUCH ! ! You Rock ! :)

Walter Roberson

unread,
Jun 22, 2010, 2:31:07 PM6/22/10
to
Karan wrote:

> Say Comp1 has a GUI in which I can enter number (constant), so if the
> simulink model was on local comp I would just use set_param to set
> parameter in simulink. But GUI is on comp1 while simulink model is on
> comp2. That model has a constant block in which I can set a integer. I
> want to set that on comp2 while I enter number on comp1. Once that is
> done, for now I want to add one (I can do that using simulink block) and
> send the result back to comp1.

My instructions remain unchanges. You send data from A to B with safeguards to
ensure that B eventually receives something even if the UDP packets go
missing. Eventually the parameter from A makes it to B and you read out the
parameter and place the simulink call with that value. When you want to send
the result back, do so, but keep a copy of the result and keep retrying in
case the other end says "I haven't received anythign yet" in case the packet
goes missing.

UDP packets are NOT reliable transmission. Think of reduced-rate post cards
that the post office is allowed to throw away if the delivery service gets
busy. For that matter, think of the postal carrier throwing away post-cards if
the carrier just didn't feel like delivering them, with the post office saying
"Tough tootsies, you paid reduced postage for a _chance_ the card would get
through and we can hold bonfires with your post-cards if we feel like it!"

Thus if you want to know whether a postcards gets through, the only way is to
keep sending more cards, each asking to RSVP and for the recipient to keep
sending back replies; eventually _something_ will get through, seconds or
hours later.

Hooman

unread,
Jun 22, 2010, 3:06:04 PM6/22/10
to

Hello Karan and Walter,

This is something that I am very much interested in. I haven't started
to play with this idea, but it has been on my mind for a long time.
Since my knowledge of UDP and TCP/IP is next to none, it would be very
helpful if one you guys can post a simple example to the list or
directly to me. I am interested in doing something like Karan
explained; two computers, one has a GUI to change the parameters and
such, the other one where MATLAB/Simulink resides and the simulation
will happen.

Would it be possible for the Simulink model to send data back to a GUI
plot element? And if yes, will this happen while the simulation is
running or one has to wait until simulation is completed?

Thanks,
Hooman

Walter Roberson

unread,
Jun 22, 2010, 3:11:35 PM6/22/10
to
Hooman wrote:

> This is something that I am very much interested in. I haven't started
> to play with this idea, but it has been on my mind for a long time.
> Since my knowledge of UDP and TCP/IP is next to none, it would be very
> helpful if one you guys can post a simple example to the list or
> directly to me. I am interested in doing something like Karan
> explained; two computers, one has a GUI to change the parameters and
> such, the other one where MATLAB/Simulink resides and the simulation
> will happen.
>
> Would it be possible for the Simulink model to send data back to a GUI
> plot element? And if yes, will this happen while the simulation is
> running or one has to wait until simulation is completed?

Yes, this can happen while the simulation is going on. Reading and writing of
these kind of objects can be asynchronous (and is by default... at least
within Matlab itself; I am not familiar with how the blocks are handled in
Simulink but they are generally faster in Simulink.)

Karan

unread,
Jun 22, 2010, 3:57:06 PM6/22/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hvr21h$k0a$1...@canopus.cc.umanitoba.ca>...

Like Walter said, it is possible to read and write data while simulation is running. You can use To Workspace block to write data in. Update you workspace block every one second or depends on how fast u want the data to be read. Read data from workspace block. I did that and it worked fine except when I stop the simulation my Matlab kept crashing. So I am not sure if the problem was with the way I used the block or my matlab. Let me know how is goes for you.

Hey Walter, So like you said I tried sending a command but I got an error.
First I opened two udp ports (Remote and Local) on the local machine, did the same on remote machine. After that use fopen command to open each other for communication.
To make sure connection works, I did fwrite(remote,2+2) on local machine and on remote machine fread(local), worked fine I got ans 4.
After that I tried fwrite(remote,set_param('WiFiTCP_IP/Constant','value','num'))
I get this error - Invalid Simulink object name: WiFiTCP_IP/Constant.

Remote computer has a simulink file name WiFiTCP_IP and it is open. ! !
any idea what I did wrong?

Thanks
Karan

Walter Roberson

unread,
Jun 22, 2010, 4:39:37 PM6/22/10
to
Karan wrote:

> After that I tried
> fwrite(remote,set_param('WiFiTCP_IP/Constant','value','num'))
> I get this error - Invalid Simulink object name: WiFiTCP_IP/Constant.
>
> Remote computer has a simulink file name WiFiTCP_IP and it is open. ! !
> any idea what I did wrong?

That would try to set the parameter *locally* and send the return result to
the other system. You have to send the parameters to the other system and have
it make the call.

fwrite(remote, sprintf('%s\n%s\n', 'WiFiTCP_IP/Constant', 'num'));

remote side:

param_name = fgetl(remote);
param_val = fgetl(remote);
set_param(param_name, 'Value', param_val);


except that because you are using UDP and _any_ packet can go missing, you
have to guard against the possibility that the parameter packet didn't make it.

Karan

unread,
Jun 22, 2010, 7:47:06 PM6/22/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hvr76j$saa$1...@canopus.cc.umanitoba.ca>...

Hey, Walter again you are amazing and thank you very much for the help ! !
I can now set variable on remote machine using my gui on local machine. One thing I changed was on remote side when you do param_name = fgetl(local), it will read both strings. Therefore when param_val is done, you get an error because there is nothing to receive.
Once that is done, I read param_name using textscan and converted into string so that I can use both words separately. And it worked. =)

Again, thank you so much ! !

Hooman

unread,
Jun 23, 2010, 9:46:20 AM6/23/10
to
On Jun 22, 7:47 pm, "Karan " <karan7...@gmail.com> wrote:
> Walter Roberson <rober...@hushmail.com> wrote in message <hvr76j$sa...@canopus.cc.umanitoba.ca>...

Hello Karan and Walter,

I am closely following the discussion that's going on between you too,
but unfortunately I don't get most of it. Can you point me to some
documentation regarding these types of things? Specifically, sending
and receiving data through TCP or UDP and the blocks that should be
used in a Simulink model to achieve this. Do you have to use a toolbox
or blockset for that?

Again, if there is an example it can prove very helpful.

Regards,
Hooman

Walter Roberson

unread,
Jun 23, 2010, 10:37:11 AM6/23/10
to
Hooman wrote:

> I am closely following the discussion that's going on between you too,
> but unfortunately I don't get most of it. Can you point me to some
> documentation regarding these types of things? Specifically, sending
> and receiving data through TCP or UDP and the blocks that should be
> used in a Simulink model to achieve this. Do you have to use a toolbox
> or blockset for that?

Sorry, I do not use Simulink.

There is a toolbox that has TCP and UDP, but you can also use the
tcpudpip Matlab File Exchange contribution.

I haven't use Matlab for TCP or UDP myself; my background happens to
include networking work so I know the theory but not so much of how
Matlab implements it.

Karan

unread,
Jun 23, 2010, 1:34:04 PM6/23/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <s_oUn.65012$h57....@newsfe22.iad>...

Hey,
So there are two ways you can send data to remote machine. Like Walter said, using command line or there are Simulink blocks that can be used. In Simulink go to Instrument Control Toolbox, you will find block to send and receive data over UDP and TCP/IP. Use UDP - Read posts before to know why, Walter explained it quite well !
So with UDP blocks, you can only send final results like Data, Numbers or Graph.

If you want to send string to remote machine use command line.

http://www.mathworks.nl/access/helpdesk/help/toolbox/instrument/f16-60757.html
Look at Example: Communicating Between Two Hosts.

Walter Roberson

unread,
Jun 23, 2010, 1:57:36 PM6/23/10
to
Karan wrote:

> So with UDP blocks, you can only send final results like Data, Numbers
> or Graph.
>
> If you want to send string to remote machine use command line.
>
> http://www.mathworks.nl/access/helpdesk/help/toolbox/instrument/f16-60757.html
>
> Look at Example: Communicating Between Two Hosts.

You can send anything you want between machines with UDP -- as far as UDP is
concerned, it is all just a string of bytes.

One small trick to be aware of is that in Matlab char() is generally two bytes
not one (because Matlab supports Unicode).

Ankit Desai

unread,
Jun 23, 2010, 2:13:05 PM6/23/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hvti2s$nrs$1...@canopus.cc.umanitoba.ca>...

Hello,

There are many threads that talk about similar stuff.
Walter, your description was spot on. Kudos to you for that.

For others who want to see simplified version of the same using UDP via Instrument Control Toolbox do check:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/165759

Hope that helps,

-Ankit

0 new messages