how to send packet over UDP socket to controller
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
i want to send packet which contain following data
header=0x1234(uint16); <---this is hex number
command=2(uint16)
num_samples=1(uint32);
can anyone help how can i create packet
what i am doing is
my pc, ipA = '192.168.1.10'; portA = 49153;
ATI F/T sensor, ipB = '192.168.1.2'; portB = 49152;
udpA = udp(ipB,portB,'LocalPort',portA);
fopen(udpA);
request=zeros(1,3)%created array
header = '0x1234';
command= 2;
num_samples = 1;
a = char(header)
b=command;
c=num_samples;
request(1,1)=int16(a)
request(1,2)=int16(b)
request(1,3)=int32(c)
fwrite(udpA, request)
when i send this packet robot should start to send data to my pc
but i am not getting any data when i run fread(udpA) command.
here i had attached my C code file same functionality i would like to build in matlab
採用された回答
Walter Roberson
2013 年 12 月 16 日
編集済み: Walter Roberson
2014 年 1 月 16 日
a = hex2dec(header);
request = [ typecast(int16([a, b]), 'uint8'), typecast(int32(c), 'uint8') ];
It is uncommon for commands or headers or counts to be signed integers. You should re-check whether they are really int16() and int32() or are instead uint16() and uint32()
11 件のコメント
Lalji goti
2013 年 12 月 18 日
編集済み: Lalji goti
2013 年 12 月 18 日
thank you for your valuable answer i think it will work but still i am getting problem when i am sending this packet and at receiving end converting this packet in hex than i am getting value like 34 12 02 00 01 00 00 00 but actually it should be 12 34 02 00 01 00 00 00
and as you said data type of variable is unsigned integers. you are right
when i am experimenting on matlab command window i am getting result which mentioned below
dec2bin(unt16([a, b]))
ans=
1001000110100 %this one is right
0000000000010
dec2bin(typecast(uint16([a, b]), 'uint8'))
ans=
110100
010010
000010
000000
Walter Roberson
2013 年 12 月 18 日
See also http://en.wikipedia.org/wiki/Endianness. In particular, by convention you should be using Network Byte Order to send multi-byte values.
Lalji goti
2013 年 12 月 19 日
編集済み: Lalji goti
2013 年 12 月 19 日
thank you very very very very very much finally it works....
this is my whole program
import java.net.*
import java.io.*
import com.atiia.automation.sensors.*
import java.net.DatagramSocket
import java.net.DatagramPacket
import java.net.InetAddress
clientSocket = DatagramSocket;
IPAddress = InetAddress.getByName('192.168.1.2');
port=49152;
header = '1234';
a=hex2dec(header);
a=uint16(a);
a=swapbytes(a);
command= 2;
b=command;
b=uint16(b);
b=swapbytes(b);
num_samples = 1;
c=num_samples;
c=uint32(c);
c=swapbytes(c);
request1= typecast(([a,b]), 'uint8');
request2= typecast([c], 'uint8');
request = [ request1,request2 ];
i=0;
while (i<2)
try
%request = [ typecast(([a, b]), 'uint8'), typecast(int32(c), 'uint8') ];
request = [ request1,request2 ];
socket = DatagramSocket;
socket.setSoTimeout(1500);
socket.connect( IPAddress, port);
sendPacket = DatagramPacket(request, length(request), IPAddress, port);
clientSocket.send(sendPacket);
catch sendPacketError
end
try
packetLength=36; %total byte received from netFT
receivePacket = DatagramPacket(zeros(1,packetLength,'uint8'),packetLength);
clientSocket.receive(receivePacket);
mssg = receivePacket.getData;
fprintf(1, 'Received %d bytes\n', receivePacket.getLength)
mssg = mssg(1:receivePacket.getLength);
rdt_sequence = tyepcast(mssg(1:4), 'uint32')
ft_sequence = tyepcast(mssg(5:8), 'uint32')
status = tyepcast(mssg(9:12), 'uint32')
Fx=typecast(mssg(13:16), 'uint32');
Fy=typecast(mssg(17:20), 'uint32');
Fz=typecast(mssg(21:24), 'uint32');
Tx=typecast(mssg(25:28), 'uint32');
Ty=typecast(mssg(19:32), 'uint32');
Tz=typecast(mssg(33:36), 'uint32');
fprintf(1, 'rdt_sequence: %d\n', rdt_sequence)
fprintf(1, 'ft_sequence: %d\n', ft_sequence)
fprintf(1, 'status: %d\n', status)
fprintf(1, 'Fx: %d\n', Fx)
fprintf(1, 'Fy: %d\n', Fy)
fprintf(1, 'Fz: %d\n', Fz)
fprintf(1, 'Tx: %d\n', Tx)
fprintf(1, 'Ty: %d\n', Ty)
fprintf(1, 'Tz: %d\n', Tz)
inetAddress = receivePacket.getAddress;
sourceHost = char(inetAddress.getHostAddress);
fprintf(1, 'inetAddress: %s',sourceHost);
catch receiveError
end
end
clientSocket.close;
can i do such stuff by using simulink block....can u suggest me in simulink how can i make this packet??
Walter Roberson
2013 年 12 月 19 日
I am aware that there is a UDP Simulink block, but I have never looked at it.
Lalji goti
2013 年 12 月 19 日
here i am sending you my simulink file... i am trying to create same packet in simulink but i cant, can you tell me where i am doing mistake
Walter Roberson
2013 年 12 月 19 日
Sorry, I do not have Simulink to experiment with. (Donations of software are accepted ;-) )
Lalji goti
2013 年 12 月 20 日
編集済み: Lalji goti
2013 年 12 月 20 日
yes, i will, send me your email id, i will send you link of my outbox so u can download it
Lalji goti
2013 年 12 月 21 日
hello, i have one more question regarding my code which i had written above.
in matlab how can i stop my infinity loop.. when my cord start receiving data its not stop i have to close whole matlab if i want to stop infinity loop
Walter Roberson
2013 年 12 月 21 日
The code is infinite because you are looping while i < 2 but you never change i.
You could assign a value >= 2 to "i", or you could use "break".
Lalji goti
2014 年 1 月 16 日
編集済み: Lalji goti
2014 年 1 月 16 日
Hello Walter Roberson,
how can i run two program parallel in matlab. means my one program getting position data continuously from robot. anotther program sending command in between first program is running..
data listening program
while 1
while ~(iStream.available)
end
readS(iStream);
end
command sending program
while 1
userInput = input('Server: ', 's');
oStream.sendS(userInput);
end
how can i run this both infinite loop program parallel in matlab i want some commands to controller while datal listening infinite loop is runnig...
Plz Help me out
Walter Roberson
2014 年 1 月 16 日
This has been answered in your Question that you created about it.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
