TCP/IP communication

  • MODULE MOD1
    !***********************************************************
    ! Customer: Precix
    ! Robot System: IRB 120
    !
    ! Program Written by:
    ! Author: Nur Fatihah
    ! Date Created: 08/03/2019
    ! Last Modified: 00/00/0000

    !***********************************************************


    VAR socketdev Server_Socket;
    VAR socketdev Client_Socket;
    VAR bool listening:=TRUE;



    VAR string Receive_string;
    CONST string IP_ComAddress:="192.168.125.1";
    !"192.168.125.1";
    CONST num Port_Number:=0004;
    VAR num retryNo:= 0;
    VAR socketstatus state;
    VAR string Send_String;
    VAR bool ok;
    VAR num modNum;
    VAR num check;



    !***********************************************************
    ! Procedure main
    !***********************************************************


    ! A:
    ! SocketClose Server_Socket;
    ! SocketClose Client_Socket;
    ! Receive_string:="";
    ! SocketCreate Server_Socket;
    ! SocketCreate Client_Socket;
    !! SocketConnect Server_Socket,"192.168.125.1",2000;
    ! SocketConnect Server_Socket,IP_ComAddress,Port_Number\Time:=5;
    ! SocketAccept Server_Socket,Client_Socket;
    ! TPWrite "Reader 1 Connect Success";
    ! SocketAccept Server_Socket,Client_Socket;

    ! state:=SocketGetStatus(Server_Socket);

    ! IF state=SOCKET_CONNECTED THEN
    ! Robot_Receive1;


    ! ELSEIF state=SOCKET_CONNECTED THEN
    ! Robot_Receive1;
    ! ELSE
    ! TPWrite "unknown status";
    ! GOTO A;
    ! ENDIF


    ! ERROR
    ! IF ERRNO=ERR_SOCK_TIMEOUT THEN
    ! TPErase;
    ! TPWrite "Socket Timeout, Retry ";
    ! TPWrite "Socket Timeout, Check Connection IP";
    ! WaitTime 1;
    ! retryNo:=retryNo+1;
    ! IF retryNo>=5 THEN
    ! TPErase;
    ! TPWrite"Socket Connection failed after 5 retry";
    ! retryNo:=0;
    ! WaitTime 5;
    ! ExitCycle;
    ! ELSE
    ! RETRY;
    ! ENDIF
    ! ELSEIF ERRNO=ERR_SOCK_CLOSED THEN
    ! SocketClose Server_Socket;
    ! TPWrite "Socket Closed, Retry... ";
    ! WaitTime 1;
    ! Stop;
    !! ELSE
    !! TPWrite "ERRNO = "\Num:=ERRNO;


    ! ENDIF


    ! ENDPROC
    PROC ConnectToServer()
    A:


    SocketClose Server_Socket;
    SocketClose Client_Socket;
    Receive_string:="";

    WaitTime 0.2;
    SocketCreate Server_Socket;
    SocketCreate Client_Socket;
    ! SocketBind Server_Socket,IP_ComAddress,Port_Number;
    TPErase;
    TPWrite"Waiting for computer to response...";
    ! SocketListen Server_Socket;
    ! WHILE listening DO
    SocketConnect Server_Socket,IP_ComAddress,0004;
    ! SocketAccept Server_Socket,Client_Socket;
    ! TPErase;
    ! TPWrite "Connection Success";
    ! ENDWHILE



    state:= SocketGetStatus(Server_Socket);

    IF state=SOCKET_CONNECTED THEN
    TPWrite "Reader 1 Connecting Success";
    Robot_Send;
    bQcheck:=FALSE;
    ELSEIF state=SOCKET_CONNECTED THEN
    TPWrite "Reader 1 Connecting Success";
    Robot_Receive1;
    Robot_Send;
    ! Robot_SendInner;
    !Robot_Receive;
    bQcheck:=TRUE;
    GOTO A;
    ELSE
    TPWrite"Unknown socket status";
    Stop;
    ENDIF

    ERROR
    IF ERRNO=ERR_SOCK_TIMEOUT THEN
    TPErase;
    TPWrite "Socket Timeout, Retry ";
    TPWrite "Socket Timeout, Check Connection IP";
    WaitTime 1;
    retryNo:=retryNo+1;
    IF retryNo>=5 THEN
    TPErase;
    TPWrite"Socket Connection failed after 5 retry";
    retryNo:=0;
    WaitTime 5;
    ExitCycle;
    ELSE
    RETRY;
    ENDIF
    ELSEIF ERRNO=ERR_SOCK_CLOSED THEN
    SocketClose Server_Socket;
    TPWrite "Socket Closed, Retry... ";
    WaitTime 1;
    Stop;
    ! ELSE
    ! TPWrite "ERRNO = "\Num:=ERRNO;
    !! GOTO A;
    ENDIF
    ENDPROC


    PROC Robot_Send()
    SocketSend Server_Socket\Str:="Check1";

    IF ERRNO=ERR_SOCK_TIMEOUT THEN
    TPErase;
    TPWrite "Socket Timeout, Retry ";
    TPWrite "Socket Timeout, Check Connection IP";
    WaitTime 1;
    retryNo:=retryNo+1;
    IF retryNo>=5 THEN
    TPErase;
    TPWrite"Socket Connection failed after 5 retry";
    retryNo:=0;
    WaitTime 5;
    ExitCycle;


    ENDIF
    ELSEIF ERRNO=ERR_SOCK_CLOSED THEN
    SocketClose Server_Socket;
    TPWrite "Socket Closed, Retry... ";
    WaitTime 1;
    Stop;
    ! ELSE
    ! TPWrite "ERRNO = "\Num:=ERRNO;
    !! GOTO A;
    ENDIF


    ENDPROC
    PROC Robot_Send2()
    SocketSend Server_Socket\Str:="Check2";


    IF ERRNO=ERR_SOCK_TIMEOUT THEN
    TPErase;
    TPWrite "Socket Timeout, Retry ";
    TPWrite "Socket Timeout, Check Connection IP";
    WaitTime 1;
    retryNo:=retryNo+1;
    IF retryNo>=5 THEN
    TPErase;
    TPWrite"Socket Connection failed after 5 retry";
    retryNo:=0;
    WaitTime 5;
    ExitCycle;
    ! ELSE ERRNO=ERR_SOCK_CLOSED THEN
    ! SocketClose Server_Socket;
    ! TPWrite "Socket Closed, Retry... ";
    ! WaitTime 1;
    ! Stop;



    ENDIF

    ! ELSE
    ! TPWrite "ERRNO = "\Num:=ERRNO;
    !! GOTO A;
    ENDIF


    ENDPROC


    PROC Robot_Receive1()

    SocketReceive Server_Socket\Str:=Receive_string\Time:=5;
    WaitTime 0.1;
    SocketClose Server_Socket;
    ok := StrToVal(Receive_string,modNum);

    IF ERRNO=ERR_SOCK_TIMEOUT THEN
    TPErase;
    TPWrite "Socket Timeout, Retry ";
    TPWrite "Socket Timeout, Check Connection IP";
    WaitTime 1;
    retryNo:=retryNo+1;
    IF retryNo>=5 THEN
    TPErase;
    TPWrite"Socket Connection failed after 5 retry";
    retryNo:=0;
    WaitTime 5;
    ExitCycle;
    ELSE
    ! RETRY;
    ENDIF
    ELSEIF ERRNO=ERR_SOCK_CLOSED THEN
    SocketClose Server_Socket;
    TPWrite "Socket Closed, Retry... ";
    WaitTime 1;
    Stop;
    ! ELSE
    ! TPWrite "ERRNO = "\Num:=ERRNO;
    !! GOTO A;
    ENDIF

    ENDPROC

    PROC Robot_Receive2()

    SocketReceive Server_Socket\Str:=Receive_string\Time:=5;
    WaitTime 0.1;
    SocketClose Server_Socket;
    ok := StrToVal(Receive_string,check);


    IF ERRNO=ERR_SOCK_TIMEOUT THEN
    TPErase;
    TPWrite "Socket Timeout, Retry ";
    TPWrite "Socket Timeout, Check Connection IP";
    WaitTime 1;
    retryNo:=retryNo+1;
    IF retryNo>=5 THEN
    TPErase;
    TPWrite"Socket Connection failed after 5 retry";
    retryNo:=0;
    WaitTime 5;
    ExitCycle;
    ELSE
    ! RETRY;
    ENDIF
    ELSEIF ERRNO=ERR_SOCK_CLOSED THEN
    SocketClose Server_Socket;
    TPWrite "Socket Closed, Retry... ";
    WaitTime 1;
    Stop;
    ! ELSE
    ! TPWrite "ERRNO = "\Num:=ERRNO;
    !! GOTO A;
    ENDIF

    ENDPROC


    i>this program should connect between robot and computer through tcp/ip.
    II>after robot connect. it should appear "connecting success".
    III> robot wait until receive computer send model 1,2,3 or 4
    IV> robot run program pick and place and when at check 1 and 2 other send/receive happen to check part ok or bad part.


    Based from this program i want ask few question:


    1. when i run the program , it stuck at SocketConnect few second then it appear socket time out and if i run it again it just said waiting computer response. how i can fix it?


    2. ip address and port number.. where i can get it? and ipcomaddress should be ip computer or robot address?


    3. if anyone have other sample program for socket communication using tcp/ip and it in very simple way to understand.. can i have it for reference


    tq..

  • Hi,


    1. when i run the program , it stuck at SocketConnect few second then it appear socket time out and if i run it again it just said waiting computer response. how i can fix it?


    You are using 192.168.125.1. Change the IP address because it's robot service port address. Set robot LAN or WAN ip like 192.168.10.XXX.

    2. ip address and port number.. where i can get it? and ipcomaddress should be ip computer or robot address?


    IP address and port number are user-defined. But some IP and port number are restricted to use because robot and PC manufacturer will be using those.


    3. if anyone have other sample program for socket communication using tcp/ip and it in very simple way to understand.. can i have it for reference


    CONST string sVisionServerIPaddress:="192.168.10.102"; ! ip addres of your device ( for ex PC )
    CONST num nVisionServerPortNumber:=3645; ! port number should be match with the device you are going to communicate.(port number is user-defined )


    SocketClose socketdevVisionServer; ! closing the socket
    WaitTime 0.05; ! delay
    SocketCreate socketDevVisionServer; ! opening the socket
    SocketConnect socketDevVisionServer,sVisionServerIPaddress,nVisionServerPortNumber,\Time:=WAIT_MAX; ! connecting the socket
    SocketSend socketdevVisionServer\Str:="Trig,10;"; ! sending the string to the device i.e., PC
    SocketReceive socketDevVisionServer\Str:=sReceive\Time:= WAIT_MAX; ! wait and receive the responce from pc


    Hope this helps


    - robot freak

Advertising from our partners