I am doing school project and am getting desperate at this moment. I hope in somewhere you will be able to give me some hints, as soon as possible.
Here is what I got. I need to send data from a PC to the other, they are both running matlab. The data transmission will be done with two sets of wireless network card.
I intended to set the data transmission done in a UDP object. Before I made the attempt, an ad-hoc network has been set up, and it is tested working. Here is some useful network information,
For PC1 (host) the IP address is 192.168.0.100 (not static IP)
For PC2 (client) the IP address is 192.168.0.223 (not static IP)
I made a simple test to try sending data with matlab via port 9090, here is what I did,
In PC1, I try to write an integer array to the client port
u=udp('192.168.0.223', 9090);
fopen(u)
fwrite(u, 1:10);
fclose(u)
In PC2, I try to read the integer array from its port
u=udp('192.168.0.223', 9090);
fopen(u)
A=fread(u, 10);
fclose(u)
But no matter how I try, I cannot retrieve the data. I cannot switch to do the whole network programming in Linux as it is too late for me, and I cannot do it in winsock either as I have written a whole bunch of other programs in matlab already.
Please help.
Yours Sincerely,
Peter
You need to bind the local port.
% on 192.168.0.100
u=udp('192.168.0.223', 9090,'LocalPort', 9091);
fopen(u)
fwrite(u, 1:10);
fclose(u)
% on 192.168.0.223
u=udp('192.168.0.100', 9091, 'LocalPort' 9090 );
Thanks so much for your help.
I just did a few tests, it is working now!!
I wonder what the meaning of 'bind the port' or 'echo udp server' are.
Again, thanks so much.
Peter
Hi Peter
Opening interfaces in a thight loop is an expensive operation. I would
suggest moving the fopen() outside of your loop to avoid problems.
> u=udp('192.168.0.223', 9090, 'LocalPort', 9091);
> fopen(u);
> for m=1:1000
> A=ceil(10*rand);
> B=ceil(10*rand+10);
> fwrite(u, A:B, 'uint16');
> end
> fclose(u);
>
Also note that clear does not destroy interfaces. This can lead to
conflicts.
>> clear u
>> u=instrfindall
UDP Object : UDP-jarvitlinux
Communication Settings
RemotePort: 3030
RemoteHost: foo
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
>> delete(u)
>> instrfindall
ans =
[]
>>
>In that case, is there a speed limit for local port binding?
Yes, there usually is. At the socket level, sockets are usually
set to "linger" so as to catch packets that might be in transmission
at the time the socket is closed. The relevant socket options
are SO_LINGER and SO_REUSE (which has a slightly different purpose) --
but I do not think you would be able to set or clear these options on
a Matlab generated socket.
--
"I think Walter was a very smart man." -- Gene Autry
I have got the other problem regarding to read data from the InputBuffer.
For a UDP object, if the DatagramTerminationMode='on',
then I can do 'fread' to take each individual package. But 'fread' function is limited to read 512 bytes only.
And if I set DatagramTerminationMode='off',
then I can do 'fread' to take as much data in the buffer as I like, but I cannot separate each individual packages. That means I have to manually separate them. But if I lost some data it would be disaster.
Any suggestions would be warmly welcome...
Regards,
Pete
I have an additional question. I am a mech engineer and am
very new to MATLAB and computing in general.
My Question is:
Do I need to execute both commands simultaneously on both
computers? If not, how much time gap is permissible between
the two commands? I am using the same code discussed above.
Thanks a great deal
Shanker
You don't need to run both programs at the same time;
obviously sends data first and read it right after.
you also can specify the timeout easily to wait to complete
a read operation by
u.Timeout = ...; (in sec)
hope this helps,
Kamyar
512 is the default value. You can increase it by using the
following command:
u.InputBufferSize = Val;
or
u = udp(..., 'InputBufferSize', Val);
Best,
Kamyar
Thanks Kamyar!
It really helped. And now Im closer to understanding about
UDP. However I still have a problem with sending 2 arrays of
floats one after the other. I am able to send one array but
somehow when I try two it does not work.
Here's the program that sends the array:
is = load('C:\Documents and
Settings\Tothom\Escritorio\Shanker\Wajahat Program\Real
Data\_IS.log');
for(i=1:2)
u = udp('192.168.1.147',9000,'LocalPort',9001);
u.OutputBufferSize = 60000;
u.Timeout = 300;
fopen(u);
fwrite(u,is(i,:),'double');
fclose(u);
delete(u);
end
and the program that receives:
clear all;
for i = 1:2
u1 = udp('192.168.1.147',9001,'LocalPort',9000);
u1.InputBufferSize = 60000;
u1.Timeout = 300;
fopen(u1);
fwrite(u1,8*103,'double');
a2 = fread(u1,8*103,'double')
fclose(u1);
delete(u1);
clear u1;
end
As you can see I have set a sufficient buffer size(It was
30000 and then I changed it to 60000 but no change), and the
timeout is quite high. Now, I run both programs on the same
computer one after the other(reciever than sender, since if
sender is executed first the data may be lost).
As I said earlier it worked perfectly on just one row of the
matrix. About the matrix, it has 103 columns is of double type.
Is it possible for you to help me on this? I ask because you
were able to solve the problem well on the previous question.
Thanks in advance :)
Shanker
Shanker,
Don't open and close the udp object inside the loop:
is = load('...');
u = udp('192.168.1.147',9000,'LocalPort',9001);
u.OutputBufferSize = ...;
u.Timeout = ...;
fopen(u);
for i=1:2
fwrite(u,is(i,:),'double');
end
fclose(u);
delete(u);
same for reading process; let me know if it doesn't work.
Kamyar
Thanks Kamyar!
It did work!!
Thanks a lot. :)
u1=udp('10.0.0.2') % the ip address of my lan connection
set(u1,'InputBufferSize',3000000)
set(u1,'Timeout',10)
fopen(u1)
fscanf(u1)
fclose(u1)
but im not sure how to write (fwrite?) the received data such that i get back the file being sent. is that possible? and what units of time is the timeout in? thank you so so much!
Jerry
"Shanker Keshavdas" <shankerk...@gmail.remove.this.com> wrote in message <g64nav$l4v$1...@fred.mathworks.com>...
Assuming that you are already getting the file contents (data) via fscanf, you can use MATLAB's fprintf: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/fprintf.html&http://www.google.com/search?client=safari&rls=en-us&q=fprintf+matlab&ie=UTF-8&oe=UTF-8
Depending on the data type.
Timeout is in seconds for instrument control objects - including UDP objects.
Hope this helps.
-Ankit
I also tried this out.
But it doesnot worked for me. :(
Here are the matlab warnings i received
Warning: The specified amount of data was not returned within the Timeout period.
A =
Empty matrix: 1-by-0
I would be glad if some body can help me.
I want to read Machine B's udp port from machine A. That's all i need.
Hoping a soon reply.
Thank you.
I also tried this out.
u1=udp('192.168.1.55', 9091,'LocalPort', 9090);
set(u1, 'OutputBufferSize', 8192)
set(u1, 'TimeOut', 10)
fopen(u1)
get(u1, 'Status')
while(1)
fwrite(u1, ubuffer(1,:), 'double'); %ubuffer is of size 1x1024
end
fclose(u1)
delete(u1)
My receiver code is:
u=udp('192.168.1.166', 9090, 'LocalPort', 9091, 'InputBufferSize', 8192, 'TimeOut', 10);
fopen(u);
while(1)
[a,count]= fread(u, 1024, 'double');
end
fclose(u)
delete(u)
My problem is that as soon as i increase the number of elements transmitted past 64, i can only receive 64 elements at receiver. Like if i transmit 1x1024 element array, at receiver i get first 64 elements only. Like at this line of code at receiver:
[a,count]= fread(u, 1024, 'double');
i only get 'a' of size 64x1, although i should get 1024x1 size of 'a'. The value of count is also 64, it should be 1024.
To rectify this i have to use a loop and make a 2d array at receiver like 64x16 for 1024 element transmitted array. Also i get a warning "The specified amount of data was not returned in time" after reading every 64 elements. Is there any solution to receive all elements like 1024 in one go??? like i dont have to use loop just
[a,count]= fread(u, 1024, 'double');
and i get all the transmitted elements.
My second problem is that i cannot transmit complex numbers using UDP. I transmit 3 complex numbers and at receiver i only get real part of these complex numbers. Any ideas????
Best...
> My second problem is that i cannot transmit complex numbers usingUDP. I transmit 3 complex numbers and at receiver i only get real part of these complex numbers. Any ideas????
I used the code as above but i can only send a UDP message to the MC, but i cannt not receive the reply UDP message. I used Wireshark to monitor the traffic and it showed that there were 2 messages: one from matlab and the other from the MC.
Message of matlab : source port 9004, des port : 9005,
source IP : my PC-IP, des IP: MC-IP
Message of MC : source port 9005, des port : 9004
source IP: MC-IP, des IP: my PC-IP
there was no error regards checksum or malformat.
My code is:
re_port = 9005;
lo_port = 9004;
lan_con = udp('144.37.155.160',re_port); % MC IP: 144.37.155.160
lan_con.LocalPort = lo_port;
lan_con.InputBufferSize = 3072;
lan_con.TimeOut = 5; %Time out in seconds
fopen(lan_con);
all_con = instrfindall
% fwrite(lan_con,127);
fprintf(lan_con,'hello world MPC AB')
% fscanf(lan_con,'char')
fread(lan_con,'char')
fclose(lan_con);
delete(lan_con);
clear(lan_con)
(the MC just act like a echo server and send back 'hello world MPC AB')
And anybody know how to clear the used port ? I tried to run the code 2 or more time, matlab always said: "port already used, cannt not bind", so i have to change the ports every time.
Any help would be nice.
Thank u all.
"Nam Tran " <nap...@htwg-konstanz.de> wrote in message <icgo0v$jt1$1...@fred.mathworks.com>...
Hi Noman,
thank for ur answer, but matlab still cannt receice any package from the MC.
The MC send every 3 secs a UDP and matlab works passive as receiver but it couldnt receive anything. I tried the RX code with a PC-PC connection and it worked.
Is there a different between a UDP package from MC and UDP package from PC???
"Nam Tran " <nap...@htwg-konstanz.de> wrote in message <icm7eo$8ju$1...@fred.mathworks.com>...
Hi Noman,
thank for ur answer. Regarding the ARP i already have a auto ARP-Reply on Request, so the MAC of MC is registed in ARP-Table.
I have figured out, that the problem was checksum of UDP-Package. I set the UDP-Checksum to 0x0000(none) and it worked. (dont know why???)
Thank u for ur time.
Best wish.
Nam Tran
"Nam Tran " <nap...@htwg-konstanz.de> wrote in message <ico5k6$l93$1...@fred.mathworks.com>...