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

 Accepted Answer

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 Comments

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
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 ;-) )
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".
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!