April 25, 2019, 02:15:37 AM
Robotforum | Industrial Robots Community

 Sending UDP packets

Author Topic:  Sending UDP packets  (Read 182 times)

0 Members and 1 Guest are viewing this topic.

April 09, 2019, 09:04:03 AM
Read 182 times
Offline

emh1812


Hi everyone,

I am trying to send some values from my iiwa to my Laptop using UDP. I followed simple tutorials for sending a String to an external machine. The Robot class for sending the packets Looks like this:

Code: [Select]
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class UDPClient {
private int port = 30001;
byte[] ControllerIP={(byte)172,31,1,(byte)147}; //IP cabinet
private byte[] bufferO = new byte[1024];
InetAddress address;

public void startClient(){
try {
address = InetAddress.getByAddress(ControllerIP);
String msg = "AA" ; 
bufferO = msg.getBytes();   

DatagramSocket Socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(bufferO, bufferO.length, address, port);
Socket.send(packet);

Socket.close();

} catch (UnknownHostException e) { 
e.printStackTrace();
}
  //catch (InterruptedException e) { e.printStackTrace(); }
  catch (IOException e) {
  e.printStackTrace();
  }

}

}

and on the PC side I created a simple listener in python but it never receives any data:

Code: [Select]
import socket

Udp_Port = 30001

SocketS = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
SocketS.bind(('', Udp_Port))

while True:
    print("Waiting for the message: ")
    data = SocketS.recvfrom(1024)
    print("received message: ", data)


My Laptop is directly connected to the X66 port of the cabinet and I have tried in AUT, T1 but still doesnt work. Is there any configuration I have to do in sunrise or do you have any idea why it is not working?

Tnhanks in advance

Today at 02:15:37 AM
Reply #1

Advertisement

Guest

April 12, 2019, 08:29:20 AM
Reply #1
Offline

emh1812


Do I need to have any optional package installed for allowing the communication with the PC through UDP? I can´t make it work  :icon_frown:

April 13, 2019, 08:29:39 AM
Reply #2
Offline

NullReference


Are you sending the data from the cabinet to the cabinet?

When you construct your datagram packet you need to pass the destination IP address not the source

April 15, 2019, 11:12:30 AM
Reply #3
Offline

emh1812


You are right @NullReference. I am trying to send from the cabinet to the pc. Which port do I have to pass when constructing the datagram packet?

I am able to receive info in my cabinet sent from the PC and then get the port of the received datagram to send an answer, but I noticed the port always changes with every datagram and what I really want to do is to first send from the cabinet but I dont know which port to use. Thank you!

April 16, 2019, 01:43:32 PM
Reply #4
Offline

kiiwa



You don't need to install any optional package to use TCP/UDP. You need to use ports only in the [30000-30010] Range. So, use 30.000 to send from PC--> Robot and 30.000 to send from Robot-->PC, i.e.
It didn't work because you were sending to your Robot (local machine) and not to the external PC, and you didn't use a robot application to run your class.
Here's your example adapted. copy both of the to your work-space and run the second. Be sure to use your PC's IP address (the one in the example is (172.31.1.135)
 
Code: [Select]
package apps;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException; 

public class UDPClientClass { 
private int port ;
private byte[] ComputerIP;
private InetAddress address;

UDPClientClass(byte[] IP,int port) {
this.ComputerIP = IP;
this.port =port;
try { address = InetAddress.getByAddress(this.ComputerIP); }
catch (UnknownHostException e) { e.printStackTrace();
}
public void sendMessage(String msg){
try { 
byte[] bufferO = new byte[1024];
bufferO = msg.getBytes();   
DatagramPacket packet = new DatagramPacket(bufferO, bufferO.length,this.address, this.port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();

} catch (UnknownHostException e) { e.printStackTrace(); }
  catch (IOException e) { e.printStackTrace();  } 
}
}

Code: [Select]
package apps;

import javax.inject.Inject;
import apps.UDPClientClass;
import com.kuka.roboticsAPI.controllerModel.Controller;
import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication; 

public class UDPTest extends RoboticsAPIApplication {
@Inject
private Controller contoller;
private int port = 30001;
private byte[] ComputerIP={(byte)172,31,1,(byte)135}; //IP of External PC
 
@Override
public void initialize() {
contoller = (Controller) getContext().getControllers().toArray()[0]; 
}
@Override
public void run() {
System.out.println("Start");
UDPClientClass udpClient = new UDPClientClass(ComputerIP,port );
System.out.println("udpClient" );
String message = "Hello";
udpClient.sendMessage(message); 
System.out.println("sent: " + message + "to client: " + ComputerIP.toString()); 
}
}







Share via facebook Share via linkedin Share via pinterest Share via reddit Share via twitter

xx
sending x,y coordinate

Started by jjf on KUKA Robot Forum

2 Replies
409 Views
Last post October 23, 2018, 12:02:21 PM
by panic mode
xx
sending coordinate

Started by Robotmey on KUKA Robot Forum

4 Replies
256 Views
Last post February 26, 2019, 05:28:33 PM
by SkyeFire
xx
Sending $VEL_ACT out via RSI / UDP

Started by mdowns53 on KUKA Robot Forum

7 Replies
733 Views
Last post August 07, 2018, 09:32:50 PM
by mdowns53
xx
Sending A Dvar to PLC

Started by Mr Goo on Yaskawa Motoman Robot Forum

5 Replies
507 Views
Last post November 03, 2018, 08:09:04 PM
by Mr Goo