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