作者:静乐思 | 来源:互联网 | 2023-06-19 07:21
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 DOrAccpet;rSend;WaitTime 0.2;ENDWHILE
ENDPROCPROC 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;
ENDPROCPROC rAccpet()Accpet:="";StN:=0;N2:=0;SocketReceive socket_C\Str:=Accpet\Time:=WAIT_MAX;TPWrite Accpet;FOR i FROM 0 TO 3 DON3:=StN+1;StN:=StrFind(Accpet,N3,",");N2:=StN-N3;C:TEST iCASE 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;ENDTESTENDFORTEST PDCASE 0:TPWrite "Communication_OK";WaitTime 1;CASE 1:pCCD1.trans.x:=Nx;pCCD1.trans.y:=Ny;IF Auto_JZ THENEulerX:=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) THENEulerX:=EulerZYX(\X,pPutBase.rot);EulerY:=EulerZYX(\Y,pPutBase.rot);EZ1:=NEZ;IF F+1=3 THENEulerX:=EulerX+R1;ENDIFIF F+1=4 THENEulerX:=EulerX+R2;ENDIFPCCD1.rot:=OrientZYX(NEZ,EulerY,EulerX);pPut:=pCCD1;ENDIFReset Vo_1;CASE 2:TPWrite "AUTOJZ_OK";Auto_JZ:=FALSE;Stop;CASE -2:TPWrite "CCD,NG!";IF Send2="T1" THENIncr N;TPWrite "Nothing!";YY:="NOthing";IF N>=HN THENClear N;ENDIFReset Vo_1;ELSETPWrite "T1_CW";Stop;ENDIFCASE 3:TPWrite "T2Photo_OK";IF Send2="T2" THENReset Vo_1;ELSETPWrite "DD_CW";Stop;ENDIFCASE -3:TPWrite "T2Photo_NG";IF Send2="T2" THENYY:="NG";Reset Vo_1;ELSETPWrite "DD_CW";Stop;ENDIFCASE 10:TPWrite "3DPhoto_OK";IF Send2="DD" THENEulerX:=EulerZYX(\X,pPut.rot);EulerY:=EulerZYX(\Y,pPut.rot);EulerY:=Nx;pPut.rot:=OrientZYX(EZ1,EulerY,EulerX);TPWrite "3DEY:"\Num:=EulerY;Reset Vo_1;ELSETPWrite "DD_CW";Stop;ENDIFDEFAULT:TPWrite "CCD,NG!";Stop;ENDTEST
ERRORIF ERRNO=ERR_SOCK_CLOSED THENrBackInit;RETRY;ENDIFIF ERRNO=ERR_SOCK_TIMEOUT THENWaitTime 0.2;RETRY;ENDIF
ENDPROCPROC 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;ERRORIF ERRNO = ERR_SOCK_CLOSED OR ERRNO = SOCKET_LISTENING THENrBackInit;RETRY;ENDIF
ENDPROCPROC rTEST1()Accpet:="";StN:=0;N2:=0;SocketReceive socket_C\Str:=Accpet;TPWrite Accpet;
ERRORIF ERRNO=ERR_SOCK_CLOSED THENrBackInit;RETRY;ENDIFIF ERRNO=ERR_SOCK_TIMEOUT THENWaitTime 0.2;RETRY;ENDIF
ENDPROC
ENDMODULE