Java socket communication

Hello I’m trying to do a simple socket communication in an URCap but i get this following print when calling
get_actual_joint_positions(), how to encoded it to something readable an example would be highly appreciated which
is something that generally lacks.
Thx my socket code is below:

import java.io.*;
import java.net.*;

public class SocketService {
       private Socket socket;
       private void connectToRobot() {
        try{
            socket = new Socket(InetAddress.getByName("127.0.0.1"),30001);
        }
        catch(IOException e){

        }

    }
    public void sendScript(String script){
        connectToRobot();
        StringBuffer instr = new StringBuffer();
        try{
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println(script);
            out.flush();
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
            InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
            int c;
            while((c = isr.read()) != 13)
                instr.append((char)c);
            closeConnectionToRobot();
            }
            catch(Exception e){

            }
            System.out.println(instr);
    }
    private void closeConnectionToRobot(){
        try{
		    socket.close();
	    }
        catch(IOException e){
		    e.printStackTrace();
	    }
    }
}

The data should be coming across as a buffer stream and so you will need to decode that data. I have never tried sending that command directly to the port but we read the stream coming from 30003 and 30001 as well as RTDE and we have to decode the data using unpack. I am not sure how you would do that in JAVA but I know in python and nodejs we use the unpack library and send it the encoding of the data. When it comes to joint position it is usually a list of 6 positions which are all a double. In the RTDE documents, this is referred to as a VECTOR6D.

Here is a link to one library I quickly looked at that might be what you need.

I have been doing a lot of socket communication with a URCap I have been developing. I had issues using PrintWriter and BufferedInputStream. Try this.


So setup the socket connection, check to make sure the socket is connected, get the output stream from that connection, then write to that connection with string s, and encode with UTF-8. To be honest not sure if the UTF-8 is needed but personally that socket connection is talking to a Raspberry PI.

Now for reading a message back you’ll do something like this

Make sure you socket connection is setup and connected. Usually I send a command close the socket then reopen it when I need to read from server.

Setup the BufferedReader --> using an InputStreamReader(‘socket connection’.getInputStream()) --> BufferedReader.readLine()!

Hope this helps!

thanks for the help guys I will look it up! :slight_smile:

Hi there. I am trying to do something like this. It would be really nice if @dhv could tell what you ended up doing. Best regards

Hi All,

Instead of using Socket class, I would suggest using SocketChannel class from java.nio package as it supports non-blocking I/O operations.

@dhv/@jea can you please explain me your use case when exactly you want to make a socket connection? I can help you out with right design and code.

Thanks,
Sankar
Founder of Daacoworks

Did you check out these two samples?

They both implement TCP Sockets in Java to communicate with in this case the client interfaces.

2 Likes

Jacob,

I have a question on the sample codes that you shared for socket communication. In one of the samples, the TimerTask opens up a new socket connection for each iteration.

Instead, can we open the socket connection once in openview() and close it in closeview() method? so that we can avoid multiple TCP/IP handshakes.

or Am I missing something?

Thanks,
Sankar

I am sure, that this would out just as fine as well.
So good idea.

Does anyone in this channel know how to clear the socket buffer as an interrupt? I am finding that my error recovery sequence is overloading the controller and I want to be able to clear the input buffer of ASCII messages. Any advice is appreciated!

Did you try calling .reset() on the buffer?

How would I do that from a script command in the GUI?
or from a socket command in an Alley Bradley PLC?

Try from your PLC. There is no reset with URScript.

What communication protocol would I send that over and to where?

We have established ethernet ip and TCPIP socket to the Dashboard server so far.