ABB 实现tcp套接字通讯(客户端程序)

1、了解通讯原理:

Tcp通讯:
客户端: 服务(器)端:
1.SocketCreat: 创建套接字 1.SocketCreat: 创建套接字
2.Conect: 连接IP地址 2.bind(): 绑定端口
3.Send/Receive: 发送或接收数据 3.listen: 监听
4.Close: 关闭 4.accept:(接收连接,等待客户端的连接)
等待所有输入连接,接受连接请求,并返回已建立连接客户端套接字
5.Send/Receive: 发送或接收数据
6.Close: 关闭

2、实现程序思路:

MODULE BackMainModule
PERS num StN:=25;
PERS num F:=0;
PERS num N:=2;
PERS num R1:=0;
PERS num R2:=0.2;
PERS num N2:=1;
PERS num N3:=24;
PERS num PD:=10;
PERS num NX:=0.513095;
PERS num NY:=1;
PERS num NEZ:=1;
PERS num HN:=6;
PERS num EulerY;
PERS num EZ1;
PERS num EulerX;
CONST robtarget pPickBase:=[[536.62,272.59,131.66],[0.000200255,-0.0156747,-0.999872,-0.00320096],[0,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];
CONST robtarget pPutBase:=[[530.47,-173.80,153.21],[0.000375863,-0.015006,-0.999885,0.00224063],[-1,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];
PERS robtarget pCCD1:=[[506.355,-173.472,133.05],[0.000380874,-0.00969365,-0.999945,0.00398781],[0,-1,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];
PERS robtarget pPut;
PERS robtarget pPick;
PERS robtarget pPJZBase;
PERS robtarget pPJZ;
PERS robtarget pPhotoPos;
VAR socketdev socket_C;
VAR socketdev socket_S;
PERS string Send1:=“DD,608.845,-143.7,178.889”;
PERS string Send2;
PERS string SendJZ;
VAR string Accpet:="";
VAR string client_ip:="";
PERS string PNx:=“608.845”;
PERS string PNy:="-143.7";
PERS string YY;
PERS string PEz:=“178.889”;
CONST string KG:=",";
PERS bool StoN_OK;
PERS bool Auto_JZ;

PROC Backmain()
    rBackInit;
    WHILE TRUE DO
        rAccpet;
        rSend;
        WaitTime 0.2;
    ENDWHILE
ENDPROC

PROC rBackInit()
    SocketClose socket_S;
    SocketClose socket_C;
    client_ip:="";
    SocketCreate socket_S;
    SocketBind socket_S,"192.168.1.10",8002;
    SocketListen socket_S;
    SocketAccept socket_S,socket_C\ClientAddress:=client_ip\Time:=WAIT_MAX;
    TPWrite client_ip;
ENDPROC

PROC rAccpet()
    Accpet:="";
    StN:=0;
    N2:=0;
    SocketReceive socket_C\Str:=Accpet\Time:=WAIT_MAX;
    TPWrite Accpet;
    FOR i FROM 0 TO 3 DO
        N3:=StN+1;
        StN:=StrFind(Accpet,N3,",");
        N2:=StN-N3;
        C:
        TEST i
        CASE 0:
            StoN_OK:=Strtoval(StrPart(Accpet,N3,N2),PD);
        CASE 1:
            StoN_OK:=Strtoval(StrPart(Accpet,N3,N2),Nx);
        CASE 2:
            StoN_OK:=Strtoval(StrPart(Accpet,N3,N2),Ny);
        CASE 3:
            StoN_OK:=Strtoval(StrPart(Accpet,N3,N2),NEZ);
        DEFAULT:
            GOTO C;
        ENDTEST
    ENDFOR
    TEST PD
    CASE 0:
        TPWrite "Communication_OK";
        WaitTime 1;
    CASE 1:
        pCCD1.trans.x:=Nx;
        pCCD1.trans.y:=Ny;
        IF Auto_JZ THEN
            EulerX:=EulerZYX(\X,pPJZBase.rot);
            EulerY:=EulerZYX(\Y,pPJZBase.rot);
            PCCD1.rot:=OrientZYX(NEZ,EulerY,EulerX);
            pPJZ:=pCCD1;
        ELSEIF Send2="T1" THEN
            !+ValToStr(N+1)
            EulerX:=EulerZYX(\X,pPickBase.rot);
            EulerY:=EulerZYX(\Y,pPickBase.rot);
            PCCD1.rot:=OrientZYX(NEZ,EulerY,EulerX);
            pPick:=pCCD1;
        ELSEIF Send2="T3"+ValToStr(F+1) THEN
            EulerX:=EulerZYX(\X,pPutBase.rot);
            EulerY:=EulerZYX(\Y,pPutBase.rot);
            EZ1:=NEZ;
            IF F+1=3 THEN
                EulerX:=EulerX+R1;
            ENDIF
            IF F+1=4 THEN
                EulerX:=EulerX+R2;
            ENDIF
            PCCD1.rot:=OrientZYX(NEZ,EulerY,EulerX);
            pPut:=pCCD1;
        ENDIF
        Reset Vo_1;
    CASE 2:
        TPWrite "AUTOJZ_OK";
        Auto_JZ:=FALSE;
        Stop;
    CASE -2:
        TPWrite "CCD,NG!";
        IF Send2="T1" THEN
            Incr N;
            TPWrite "Nothing!";
            YY:="NOthing";
            IF N>=HN THEN
                Clear N;
            ENDIF
            Reset Vo_1;
        ELSE
            TPWrite "T1_CW";
            Stop;
        ENDIF
    CASE 3:
        TPWrite "T2Photo_OK";
        IF Send2="T2" THEN
            Reset Vo_1;
        ELSE
            TPWrite "DD_CW";
            Stop;
        ENDIF
    CASE -3:
        TPWrite "T2Photo_NG";
        IF Send2="T2" THEN
            YY:="NG";
            Reset Vo_1;
        ELSE
            TPWrite "DD_CW";
            Stop;
        ENDIF
    CASE 10:
        TPWrite "3DPhoto_OK";
        IF Send2="DD" THEN
            EulerX:=EulerZYX(\X,pPut.rot);
            EulerY:=EulerZYX(\Y,pPut.rot);
            EulerY:=Nx;
            pPut.rot:=OrientZYX(EZ1,EulerY,EulerX);
            TPWrite "3DEY:"\Num:=EulerY;
            Reset Vo_1;
        ELSE
            TPWrite "DD_CW";
            Stop;
        ENDIF
    DEFAULT:
        TPWrite "CCD,NG!";
        Stop;
    ENDTEST
ERROR
    IF ERRNO=ERR_SOCK_CLOSED THEN
        rBackInit;
        RETRY;
    ENDIF
    IF ERRNO=ERR_SOCK_TIMEOUT THEN
        WaitTime 0.2;
        RETRY;
    ENDIF
ENDPROC

PROC rSend()
    WaitDI Vi_1,1;
    PNx:=ValToStr(pPhotoPos.trans.x);
    PNy:=ValToStr(pPhotoPos.trans.y);
    PEz:=ValToStr(EulerZYX(\Z,pPhotoPos.rot));
    Send1:=Send2+KG+PNx+KG+PNy+KG+PEz;
    WaitTime 0.2;
    SocketSend socket_C\Str:=Send1;
    TPWrite Send1;
 ERROR
    IF ERRNO = ERR_SOCK_CLOSED OR ERRNO = SOCKET_LISTENING THEN
        rBackInit;
        RETRY;
    ENDIF
ENDPROC

PROC rTEST1()
    Accpet:="";
    StN:=0;
    N2:=0;
    SocketReceive socket_C\Str:=Accpet;
    TPWrite Accpet;
ERROR
    IF ERRNO=ERR_SOCK_CLOSED THEN
        rBackInit;
        RETRY;
    ENDIF
    IF ERRNO=ERR_SOCK_TIMEOUT THEN
        WaitTime 0.2;
        RETRY;
    ENDIF
ENDPROC

ENDMODULE

在Delphi中,套接字(Socket)编程用到的基本类是TServerSocket与TClientSocket。这两个类全部位于ScktComp单元中。其类型定义如下: type TServerSocket = class (ScktComp.TCustomServerSocket); TClientSocket = class (ScktComp.TCustomSocket)。      在编写程序时,首先要对TServerSocket(在服务器端)与TClientSocket(在客户端)进行实例化。对于TServerSocket的对象,主要设置其服务类型(ServerType)与端口(Port)的属性,然后编写“OnClientRead”事件处理程序的代码,处理来自客户机的请求。如要启动服务器,设置TServerSocket对象的Active属性为真(即Active := True),如要停止服务器,则设置TServerSocket对象的Active属性为假(即Active := False)。      对于TClientSocket的对象,主要设置对方服务器的服务类型(ServerType)、端口(Port)以及IP地址(Address)的属性,之后编写“OnConnect与OnRead”事件处理程序的代码“OnConnect”事件处理程序用来检查与服务器连接成功与否(必须在这里进行检查才有效),“OnRead”事件处理程序用来读取服务器发来的信息。如要连接服务器,设置TClientSocket对象的Active属性为真(即Active := True;注意:检查连接是否成功,必须在“OnConnect”事件处理程序中进行),如要断开与服务器的连接,则设置TClientSocket对象的Active属性为假(即Active := False)。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值