RobotStudio event

RobotStudio communication with TiaPortal via socket

Options
Hello.
I need some help with project.
I have to make communication between Tiaportal V14 and RobotStudio using TCP/IP. Does anybody some experiences with this?
Thanks for help.

Comments

  • ko414du4
    Options
    Hi,

    I have done socket connection with Rockwell PLC once and below is how I did it (in a separate SemiStatic task). I reckon it should be the same since it's the same protocol. I am not sure on the TIAPortal side but I believe you have to have the GSD file for IRC5 to recognise the controller on the network.


    MODULE MainModule
        VAR socketdev clientSocket;
        VAR socketstatus socketStat;
        VAR rawbytes raw_send;
        VAR rawbytes raw_receive;
        
        PROC main()
            VAR signaldi plcAlive;
            AliasIO diComms_Alive,plcAlive;
            
            ! Eternal loop to wait until PLC is ON
            WHILE plcAlive=0 DO
                WaitTime 5;
                
                Set doCommsError;
                
                IF (diForceStop=1) Stop; ! Force stop prog execution from pendant. diForceStop is on Virtual_1 unit
            ENDWHILE
            
            ! Reset error
            Reset doCommsError;
            
            ! Clear raw bytes
            ClearRawBytes raw_send; ! Clear raw data first
            ClearRawBytes raw_receive; ! Clear raw data first
            
            SocketClose clientSocket;
            
            socketStat:=SocketGetStatus(clientSocket); ! Get status of client socket & act accordingly
            WHILE NOT(socketStat=SOCKET_CONNECTED) DO
                WaitTime 2; ! Recommended wait time before closing a socket
                SocketClose clientSocket;
                WaitTime 2; ! Recommended wait time between closing & opening a socket

                IF (plcAlive=0) ExitCycle; ! Go to eternal loop to wait until PLC is back ON
                
                connectToServer "192.168.1.1",1025; ! Connect to server by address and port no. in arguments
                socketStat:=SocketGetStatus(clientSocket); ! Update status of client socket
            ENDWHILE

            WHILE (socketStat=SOCKET_CONNECTED) DO
                IF (plcAlive=0) ExitCycle; ! Go to eternal loop to wait until PLC is back ON
                
                !WaitTime 1;
                sendData;
                !WaitTime 1;
                receiveData;
            ENDWHILE
        ENDPROC

        PROC connectToServer(string IPAddress,num portNo)
            VAR num retryNo:=0;
            
            SocketCreate clientSocket; ! Create client socket
            SocketConnect clientSocket,IPAddress,portNo; ! Connect to server with IPAddress and portNo supplied
            TPErase;
            TPWrite "Connection successful";

        ERROR
            IF ERRNO=ERR_SOCK_TIMEOUT THEN
                WaitTime 1;
                retryNo:=retryNo+1;
                IF retryNo>=3 THEN
                    ErrWrite\I, "Connection failed", "Trying to connect to server (PLC) failed."\RL2:="PP goes to Main to check if PLC alive."\RL3:="Retries is reset.";
                    TPWrite "Socket connection failed after 3 retries";
                    retryNo:=0;
                    WaitTime 5;
                    ExitCycle;
                ELSE
                    RETRY;
                ENDIF
            ELSEIF ERRNO=ERR_SOCK_CLOSED THEN
                WaitTime 5;
                ExitCycle;
            ELSE
                TPWrite "ERRNo = "\Num:=ERRNO;
                Stop;
            ENDIF
        ENDPROC
        
        PROC sendData()
            ClearRawBytes raw_send; ! Clear raw data first
            
            ! Prep data to send
            packet_send.x:=packet_receive.x;
            packet_send.y:=packet_receive.y;
            packet_send.z:=packet_receive.z;
            packet_send.Rx:=packet_receive.Rx;
            packet_send.Ry:=packet_receive.Ry;
            packet_send.Rz:=packet_receive.Rz;
            packet_send.Status:=packet_receive.Status;
                
            ! Pack pieces of data in the packet into raw_send
            PackRawBytes packet_send.x,raw_send,(RawBytesLen(raw_send)+1)\Float4; 
            PackRawBytes packet_send.y,raw_send,(RawBytesLen(raw_send)+1)\Float4;
            PackRawBytes packet_send.z,raw_send,(RawBytesLen(raw_send)+1)\Float4;
            PackRawBytes packet_send.Rx,raw_send,(RawBytesLen(raw_send)+1)\Float4;
            PackRawBytes packet_send.Ry,raw_send,(RawBytesLen(raw_send)+1)\Float4;
            PackRawBytes packet_send.Rz,raw_send,(RawBytesLen(raw_send)+1)\Float4;
            PackRawBytes packet_send.Status,raw_send,(RawBytesLen(raw_send)+1)\IntX:=1; 
                
            SocketSend clientSocket \RawData:=raw_send;
            
        ERROR
            IF ERRNO=ERR_SOCK_CLOSED THEN
                TPWrite "Socket closed during sending data";
                ExitCycle;
            ENDIF    
        ENDPROC
        
        PROC receiveData()
            ClearRawBytes raw_receive; ! Clear raw data first
            
            SocketReceive clientSocket \RawData:=raw_receive\Time:=10;
                
            ! Unpack raw_receive and save individual values in packet_receive
            UnpackRawBytes raw_receive,1,packet_receive.x\Float4;
            UnpackRawBytes raw_receive,5,packet_receive.y\Float4;
            UnpackRawBytes raw_receive,9,packet_receive.z\Float4;
            UnpackRawBytes raw_receive,13,packet_receive.Rx\Float4;
            UnpackRawBytes raw_receive,17,packet_receive.Ry\Float4;
            UnpackRawBytes raw_receive,21,packet_receive.Rz\Float4;
            UnpackRawBytes raw_receive,25,packet_receive.Status\IntX:=1;
            
            socketStat:=SocketGetStatus(clientSocket); ! Update status of client socket
            
            ERROR
            IF ERRNO = ERR_SOCK_TIMEOUT OR ERRNO=ERR_SOCK_CLOSED THEN
                WaitTime 5; ! Wait so server application starts first
                TPWrite "Comms time out during receiving data";
                ExitCycle;
            ENDIF
        ENDPROC
    ENDMODULE