RobotStudio event

Send robot position from a semi-static background task through socket communication

rakshithb174
edited October 2019 in RAPID Programming
Hello,

I am trying to send robot position data [X,Y,Z] only through socket communication from a background task. 

My procedure for setting up socket communication is as follows: (This is inside main() module)
PERS bool socketActive := FALSE;

PROC Setup_Socket_Comm()
        ! WaitTime to delay start of client
        ! Sever application should start first
        WaitTime 5;

        SocketCreate server_socket;
        ! IP address of server/slave (PLC) is xxx.xxx.xxx.xxx
        SocketConnect server_socket,"xxx.xxx.xxx.xxx",1025;

        ! Communication start
        SocketSend server_socket\Str:="Hello PLC#";
        SocketReceive server_socket\Str:=received_string;
        TPWrite "Server wrote - "+received_string;

        ! Set socket active flag
        socketActive:=TRUE;

        ! Error handler to make it possible to handle power fail

    ERROR

        IF ERRNO=ERR_SOCK_TIMEOUT THEN

            RETRY;

        ELSEIF ERRNO=ERR_SOCK_CLOSED THEN

            SocketClose server_socket;

            ! WaitTime to delay start of client. 

            ! Server application should start first.

            WaitTime 10;

            SocketCreate server_socket;

            SocketConnect server_socket,"xxx.xxx.xxx.xxx",1025;

            RETRY;

        ELSE

            TPWrite "ERRNO = "\Num:=ERRNO;

            Stop;

        ENDIF


    ENDPROC
 I have created a new semi-static task called "robPos". Is it possible to reference a socket variable created in foreground task by a background semi-static task? My "robPos" task has following content:

MODULE robotPos
    ! Shared Global Persistant variables with tasks
    PERS bool socketActive:=FALSE;
    VAR num xpos;
    VAR num ypos;
    VAR num zpos;
           
    ! Socket comm
    VAR string received_string:="";
    
    PROC main()
        sendRobPos;        
    ENDPROC
    
    PROC sendRobPos()
        
        VAR pos currPos;
        VAR string str_x:="";
        VAR string str_y:="";
        VAR string str_z:="";

        WHILE (socketActive := TRUE) DO
            ! Get current robot position
            currPos:=CPos(\Tool:=tool0\WObj:=wObj0);
            xpos:=currPos.x;
            ypos:=currPos.y;
            zpos:=currPos.z;

            !Dont forgetaddition of termination character (#)
            str_x:="x"+NumToStr(xpos,4)+"#";
            str_y:="y"+NumToStr(ypos,4)+"#";
            str_z:="z"+NumToStr(zpos,4)+"#";

            SocketSend server_socket\Str:=str_x;
            SocketReceive server_socket\Str:=received_string;<br>            IF (received_string="ack") THEN
                ! Ack recieved
                SocketSend server_socket\Str:=str_y;<br>                SocketReceive server_socket\Str:=received_string;<br>
                IF (received_string="ack") THEN
                    ! Ack recieved
                    SocketSend server_socket\Str:=str_z;<br>                    SocketReceive server_socket\Str:=received_string;<br>
                    IF (received_string="ack") THEN
                        ! Ack recieved
                        ! All good
                    ENDIF
                ENDIF
            ENDIF        
        ENDWHILE
    ENDPROC

I am trying to reference "server_socket" defined in main module of foreground task in background "robPos: task. I am getting a syntax error. Server_socket variable is not visible in scope of robPos task. How can i solve this?

Comments

  • I am curious as to the construction of the beginning of your WHILE instruction:

     WHILE (socketActive := TRUE) DO
    It is my understanding := means assign.

    Are you sure you don't mean

    WHILE socketActive=TRUE  DO

    ?
  • SomeTekk said:
    I am curious as to the construction of the beginning of your WHILE instruction:

     WHILE (socketActive := TRUE) DO
    It is my understanding := means assign.

    Are you sure you don't mean

    WHILE socketActive=TRUE  DO

    ?
    Oops, yes that's a typo. It should be = 

    Can socket variables declared in another task used in a background task? 
  • Ought to be if they have the same name and are declared as PERS.
    Lee Justice
  • rakshithb174
    edited October 2019
    lemster68 said:
    Ought to be if they have the same name and are declared as PERS.
    I tried this. I get an error saying that the datatype "socketdev" used to declare "server_socket" can't be of type persistent. 
    This gives me an error when added to background task:
    PERS socketdev server_socket

    it looks like it can only be declared as a variable 

    VAR socketdev server_socket
    The manual says that socketdev is a non-value datatype

  • lemster68
    lemster68 ✭✭✭
    edited October 2019
    Oops!  But anyway, why would you also need it in your main task if the background task is doing the job?
    Lee Justice
  • lemster68 said:
    Oops!  But anyway, why would you also need it in your main task if the background task is doing the job?
    The main task is sending different parameters (these parameters are embedded in the main task rapid code and these parameters are sent at particular motion events). 

    Is it possible to have two socket connections to same server (PLC) with same IP address but different ports? That might solve the problem

  • I think, just different socketdevs.
    Lee Justice
  • rakshithb174
    edited October 2019
    lemster68 said:
    I think, just different socketdevs.
    so create two socketdevs, one in main task and another in background task? Can they connect to the same server (same IP address) but with a different port number?

    Manual says it is possible to have a max of 32 simultaneous socket devices at a time