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
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??
I am aware that there is a UDP Simulink block, but I have never looked at it.
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
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
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
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
