How to receive UDP packets in matlab?

18 views (last 30 days)
HJ Jay
HJ Jay on 23 Jul 2014
Answered: Eddy H on 21 Jul 2017
I'm trying to get IMU sensor data from my android phone (from this app) The app Can anyone tell me the best way I can accomplish this?
Thank you.

Accepted Answer

Geoff Hayes
Geoff Hayes on 23 Jul 2014
Edited: Geoff Hayes on 23 Jul 2014
That's a neat little app. If you have the Instrument Control Toolbox, you could probably use the UDP object to get the information that you need.
I don't have that software, so went to the File Exchange and grabbed the Simple UDP Communications App code. It is just one function, judp, that allows you to send and receive UDP packets.
Assuming that the Wireless IMU app is running and that I just want to get the next 100 packets, then the following code would do the trick
% target port as set in the app
port = 5555;
payloadData = cell(100,1);
for k=1:100
% get the message/payload only assuming a max size of 200 bytes
[msg,~] = judp('RECEIVE',port,200);
% save the payload to the array
payloadData{k} = msg;
% convert the message to ASCII and print it out
fprintf('%s\n',char(msg)');
end
Some sample payload data was
1406156316.20970, 3, 0.157, 0.333, 9.850, 4, -0.000, 0.001, -0.000, 5,
14.699,-0.420,-49.500
1406156316.27678, 3, 0.271, 0.287, 9.832
1406156316.34447, 3, 0.199, 0.281, 9.844
1406156316.41106, 3, 0.201, 0.286, 9.844, 4, -0.001, 0.001, 0.001, 5,
14.699,-0.479,-49.680
1406156316.47826, 3, 0.242, 0.319, 9.856
1406156316.54561, 3, 0.208, 0.283, 9.861
1406156316.61258, 3, 0.216, 0.250, 9.862, 4, 0.000, 0.000, -0.000, 5,
14.699,-0.420,-49.860
As described in the Wireless IMU app, a comma separated list of data is sent in the UDP payload. I found that the payload sizes were either 44 bytes or 100 bytes. The smaller payloads contained a timestamp and the accelerometer data only, whereas the larger messages had the timestamp, accelerometer, gyroscope and magnetometer data. Two of the former appeared for one of the latter at the Slow (Normal) update rate.
All data seemed to match that which was displayed in the app on my phone.
Try the above and see what happens!
  3 Comments
taher azim
taher azim on 14 Mar 2017
hey, i was also using the above code but it gave me error stating that Error using judp (line 166) judp.m--Failed to receive UDP packet; connection timed out.
Error in Untitled (line 8) [msg,~] = judp('RECEIVE',port,200);
how to get rid of that error..
% target port as set in the app port = 5555; payloadData = cell(100,1);
for k=1:100
% get the message/payload only assuming a max size of 200 bytes
[msg,~] = judp('RECEIVE',port,200);
% save the payload to the array
payloadData{k} = msg;
% convert the message to ASCII and print it out
fprintf('%s\n',char(msg)');
end

Sign in to comment.

More Answers (1)

Eddy H
Eddy H on 21 Jul 2017
This solution works for me, but has anyone got this working using the Instrument Control Toolbox? I can't get it to work this way.
echoudp('on',5555)
u = udp('PHONEIP',5555);
fopen(u)
A = fread(u,200)
ERROR: Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.

Categories

Find more on MATLAB Mobile in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!