필터 지우기
필터 지우기

How to receive UDP packets in matlab?

조회 수: 23 (최근 30일)
HJ Jay
HJ Jay 2014년 7월 23일
답변: Eddy H 2017년 7월 21일
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.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 7월 23일
편집: Geoff Hayes 2014년 7월 23일
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
Geoff Hayes
Geoff Hayes 2014년 8월 2일
Awesome!
taher azim
taher azim 2017년 3월 14일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Eddy H
Eddy H 2017년 7월 21일
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.

카테고리

Help CenterFile Exchange에서 MATLAB Mobile에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by