Advertising

How to run a TCP Client program as a background task

  • Hey, all, I need your help, please. Recently, I am working on writing a TCP client program on sunrise. And I want to run it as a background task. I wrote the code by reading the manual. But when I just synchronised the code to the cabinet, the light was red and I couldn't see the error log. The code is shown below, can anyone of you who know how to write a background task help me work out of it. Thanks very much!!!


    That is the code of background task:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;


    import javax.inject.Inject;
    import com.kuka.roboticsAPI.applicationModel.tasks.RoboticsAPIBackgroundTask;
    import com.kuka.roboticsAPI.controllerModel.Controller;
    import com.kuka.roboticsAPI.uiModel.ApplicationDialogType;
    import com.kuka.task.ITaskFunctionMonitor;
    import com.kuka.task.TaskFunctionMonitor;


    /**
    * Implementation of a background task.
    * <p>
    * The background task provides a {@link RoboticsAPIBackgroundTask#initialize()}
    * and a {@link RoboticsAPIBackgroundTask#run()} method, which will be called
    * successively in the task lifecycle.<br>
    * The task will terminate automatically after the <code>run</code> method has
    * finished or after stopping the task.
    * <p>
    * <b>It is imperative to call <code>super.dispose()</code> when overriding the
    * {@link RoboticsAPITask#dispose()} method.</b>
    * @see UseRoboticsAPIContext
    *
    */
    public class BackgroundTask extends RoboticsAPIBackgroundTask {
    @Inject
    Controller kUKA_Sunrise_Cabinet_1;
    private IApplicationInformationFunction appInfoFunction;
    private ITaskFunctionMonitor appInfoMonitor;
    @Override
    public void initialize() {
    // initialize your task here
    appInfoFunction =getTaskFunction(IApplicationInformationFunction.class);
    appInfoMonitor =TaskFunctionMonitor.create(appInfoFunction);
    }


    @Override
    public void run() {
    // your task execution starts here
    try{
    Socket server = new Socket("172.31.1.100", 30000);
    final BufferedReader in = new BufferedReader(new InputStreamReader(
    server.getInputStream()));
    PrintWriter out = new PrintWriter(server.getOutputStream());
    BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
    new Thread() {
    public void run() {
    while (true) {
    try {
    System.out.println(in.readLine());
    } catch (IOException e) {
    e.printStackTrace();
    break;
    }
    }
    };
    }.start();
    while (true) {
    String str = wt.readLine();
    if(str=="finished")
    {
    appInfoFunction.SetCondition(true);
    }
    if(appInfoFunction.isSelectingFinished())
    {
    out.println("finished");
    out.flush();
    }
    if (str.equals("1")) {
    break;
    }


    }
    server.close();
    }
    catch(IOException e)
    {
    System.out.println(e);
    }
    }
    }

  • public interface IApplicationInformationFunction {
    public boolean isOtherFinished();
    public boolean isSelectingFinished();
    public void SetCondition(boolean Isfinished);
    public void setCondition(boolean isfinished);
    }


    public class ApplicationInformation implements IApplicationInformationFunction {
    public boolean isfinished;
    public boolean Isfinished;
    public boolean isSelectingFinished()
    {
    return isfinished;
    }
    public void setCondition(boolean isfinished)
    {
    this.isfinished=isfinished;
    }
    public boolean isOtherFinished()
    {
    return Isfinished;
    }
    public void SetCondition(boolean Isfinished)
    {
    this.Isfinished=Isfinished;
    }
    }

  • Why did you create a Thread inside a backgroundTask? Then an infinite while loop with another one just below?


    If you want to do something recurrent, you can either create your own class that implements Runnable then launch it in a thread yo instantiate in a normal robot program.
    Otherwise, no need to use the while (true) statement , just write your loop's code inside a cyclic Background Task "RoboticsAPICyclicBackgroundTask.


    Check the SunriseOS documentation, Chapter 16, for more info.

  • Actually I want to display the string information on the smart pad. And for the while loop, it is used for reading the socket information. As far as I am concerned, cyclic background task will run periodically. And every time it runs, it will create a new socket. I don't know whether it will works when requesting the server. Actually, I want to run this program once. And it should finish after the providing task program had changed the string to "1". Thank you very much!!!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account
Sign up for a new account in our community. It's easy!
Register a new account
Sign in
Already have an account? Sign in here.
Sign in Now