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;
IF (received_string="ack") THEN
! Ack recieved
SocketSend server_socket\Str:=str_y;
SocketReceive server_socket\Str:=received_string;
IF (received_string="ack") THEN
! Ack recieved
SocketSend server_socket\Str:=str_z;
SocketReceive server_socket\Str:=received_string;
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?