这是读D10和写D11的代码,后来写了一个可读写任意地址的函数,并使用速度更快的长连接,有需要的朋友可以联系我。
MODULE MainModule
VAR socketdev socket2;
VAR byte handshake{20}:=[70, 73, 78, 83, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
VAR byte readplcD10{34}:=[70, 73, 78, 83, 0, 0, 0, 26, 0, 0, 0, 2, 0, 0, 0, 0, 128, 0, 2, 0, 2, 0, 0, 1, 0, 0, 1, 1, 130, 0, 10, 0, 0, 1];
VAR byte writeplcD11{36}:=[70, 73, 78, 83, 0, 0, 0, 28, 0, 0, 0, 2, 0, 0, 0, 0, 128, 0, 2, 0, 2, 0, 0, 1, 0, 0, 1, 2, 130, 0, 11, 0, 0, 1, 0, 0];
VAR byte plcback{34}:=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
VAR num D10;
VAR num D10H;
VAR num D10L;
PROC main()
readomronD10;
writeomronD11;
ENDPROC
PROC readomronD10()
SocketClose socket2;
SocketCreate socket2;
SocketConnect socket2,"192.168.125.2", 9600 \Time:= 1;
socketSend socket2 \Data:=handshake;
SocketReceive socket2 \Data:=plcback;
plcback:=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
readplcD10:=[70, 73, 78, 83, 0, 0, 0, 26, 0, 0, 0, 2, 0, 0, 0, 0, 128, 0, 2, 0, 2, 0, 0, 1, 0, 0, 1, 1, 130, 0, 10, 0, 0, 1];
SocketSend socket2 \Data:=readplcD10;
SocketReceive socket2 \Data:=plcback;
SocketClose socket2;
D10H:=plcback{31};
D10L:=plcback{32};
D10 := plcback{31}*256 + plcback{32};
ENDPROC
PROC writeomronD11()
SocketClose socket2;
SocketCreate socket2;
SocketConnect socket2,"192.168.125.2", 9600 \Time:= 1;
socketSend socket2 \Data:=handshake;
SocketReceive socket2 \Data:=plcback;
plcback:=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
writeplcD11:=[70, 73, 78, 83, 0, 0, 0, 28, 0, 0, 0, 2, 0, 0, 0, 0, 128, 0, 2, 0, 2, 0, 0, 1, 0, 0, 1, 2, 130, 0, 11, 0, 0, 1, 0, 0];
writeplcD11{35}:=D10H;
writeplcD11{36}:=D10L;
SocketSend socket2 \Data:=writeplcD11;
SocketReceive socket2 \Data:=plcback;
SocketClose socket2;
ENDPROC
ENDMODULE
以上是读取D10,和写入D11的简单示例,后面有读取多个字,写入多个字的示例。
omron ip: 192.168.125.2
abb ip: 192.168.125.1
子网掩码:255.255.255.0
请求帧:
[70,73,78,83,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0];
内容 | 说明 |
---|---|
70,73,78,83, | ASCII:FINS |
0,0,0,12, | 后续长度 |
0,0,0,0, | 命令码 |
0,0,0,0, | 错误代码 |
0,0,0,1 | 客户端节点地址 0-254 0为自动分配 |
返回帧:
[70,73,78,83,0,0,0,16,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,2];
内容 | 说明 |
---|---|
70,73,78,83, | ASCII:FINS |
0,0,0,16, | 后续长度 |
0,0,0,1, | 命令码 |
0,0,0,0, | 错误代码 |
0,0,0,1 | 客户端节点地址 0-254 0为自动分配 |
0,0,0,2 | PLC节点地址 |
示例:读取D10
[70, 73, 78, 83, 0, 0, 0, 26, 0, 0, 0, 2, 0, 0, 0, 0, 128, 0, 2, 0, 2, 0, 0, 1, 0, 0, 1, 1, 130, 0, 10, 0, 0, 1];
内容 | 说明 |
---|---|
70,73,78,83, | ASCII:FINS |
0,0,0,26, | 后续长度 |
0,0,0,2, | 命令码 读写时为2 |
0,0,0,0, | 错误代码 |
- | 以下为FINS/UDP帧; |
128, | 发送:ICF = 0x80 (128);接收: ICF = 0xC0 300) |
0, 2, | RSV 0 GCT 2 |
0, 2, 0, | DNA 目标网络 本地:0 远程:0x01-0x7f DA1:目标节点 PLC IP最后一位 DA2 目标单元 CPU:0 网络模块: |
0, 1, 0, | SNA 源网络号 0 SA1 源节点号 本机IP最后一位 SA2 源单元号 0 |
0, 0, | SID 用于校验,原数据返回 |
- | 以下为FINS帧 |
1, 1, | 1, 1 读; 1,2 写 |
130, | 存储区 0x82 (130) 代表D区 其它见omron手册 |
0, 10, | 超始地址 |
0, 0, 1 | 数据个数 |
响应帧
内容 | 说明 |
---|---|
70,73,78,83, | ASCII:FINS |
0,0,0,24, | 后续长度 |
0,0,0,2, | 命令码 读写时为2 |
0,0,0,0, | 错误代码 |
- | 以下为FINS/UDP帧; |
300, | 发送:ICF = 0x80 (128);接收: ICF = 0xC0 300) |
0, 2, | RSV 0 GCT 2 |
0, 2, 0, | DNA 目标网络 本地:0 远程:0x01-0x7f DA1:目标节点 PLC IP最后一位 DA2 目标单元 CPU:0 网络模块: |
0, 1, 0, | SNA 源网络号 0 SA1 源节点号 本机IP最后一位 SA2 源单元号 0 |
0, 0, | SID 用于校验,原数据返回 |
- | 以下为FINS帧 |
1, 1, | 1, 1 读; 1,2 写 |
0, 0, | 结束码 |
0, 0, | 数据 |
读取和写入多个字的示例,以后有时间写个读写任意地址的
MODULE OmronModule
VAR socketdev socketOmron;
VAR socketstatus socketOmronStat;
CONST byte handshake{20}:=[70,73,78,83,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0];
VAR byte readplcD10_19{34};
VAR byte writeplcD21_29{54};
VAR byte plcback{60};
VAR num d10_PlcStep;
VAR num d11_pg;
VAR num d12_memory;
VAR num d13_wifi1;
VAR num d14_wifi2;
VAR num d15_cooler;
VAR num d16_base;
VAR num d17_qc;
VAR num d18_tag;
VAR num d19_Screws;
VAR num d20_robotStep;
VAR num d21_base;
VAR num d22_errcode;
VAR num d23_PG1ActionNo;
VAR num d24_PG2ActionNo;
VAR num d25_ActionStatus;
VAR num d26_GetParts;
VAR string OmronIP:="192.168.125.2";
VAR num SA1;
VAR num DA1;
PROC PlcRead()
funcDone:=FALSE;
WHILE funcDone=FALSE DO
funcDone:=ReadPLC();
ENDWHILE
ENDPROC
PROC PlcWrite()
funcDone:=FALSE;
WHILE funcDone=FALSE DO
funcDone:=WritePLC();
ENDWHILE
ENDPROC
FUNC bool ReadPLC()
Reset do06_SocketErr;
socketOmronStat:=SocketGetStatus(socketOmron);
IF socketOmronStat<>SOCKET_CONNECTED THEN
IF socketOmronStat<>SOCKET_CREATED THEN
SocketCreate socketOmron;
ENDIF
SocketConnect socketOmron,OmronIP,9600\Time:=1;
socketSend socketOmron\Data:=handshake;
SocketReceive socketOmron\Data:=plcback;
DA1:=plcback{24};
SA1:=plcback{20};
ENDIF
plcback{16}:=10;
readplcD10_19:=[70,73,78,83,0,0,0,26,0,0,0,2,0,0,0,0,128,0,2,0,DA1,0,0,SA1,0,0,1,1,130,0,10,0,0,10];
SocketSend socketOmron\Data:=readplcD10_19;
SocketReceive socketOmron\Data:=plcback;
IF plcback{16}<>0 THEN
SocketClose socketOmron;
RETURN FALSE;
ENDIF
d10_PlcStep:=plcback{31}*256+plcback{32};
d11_pg:=plcback{33}*256+plcback{34};
d12_memory:=plcback{35}*256+plcback{36};
d13_wifi1:=plcback{37}*256+plcback{38};
d14_wifi2:=plcback{39}*256+plcback{40};
d15_cooler:=plcback{41}*256+plcback{42};
d16_base:=plcback{43}*256+plcback{44};
d17_qc:=plcback{45}*256+plcback{46};
d18_tag:=plcback{47}*256+plcback{48};
d19_Screws:=plcback{49}*256+plcback{50};
RETURN TRUE;
ERROR
ResetRetryCount;
SocketClose socketOmron;
Set do06_SocketErr;
WaitTime 1;
Reset do06_SocketErr;
RETURN FALSE;
ENDFUNC
FUNC bool WritePLC()
Reset do06_SocketErr;
socketOmronStat:=SocketGetStatus(socketOmron);
IF socketOmronStat<>SOCKET_CONNECTED THEN
IF socketOmronStat<>SOCKET_CREATED THEN
SocketCreate socketOmron;
ENDIF
SocketConnect socketOmron,OmronIP,9600\Time:=1;
socketSend socketOmron\Data:=handshake;
SocketReceive socketOmron\Data:=plcback;
DA1:=plcback{24};
SA1:=plcback{20};
ENDIF
plcback{16}:=10;
writeplcD21_29:=[70,73,78,83,0,0,0,46,0,0,0,2,0,0,0,0,128,0,2,0,DA1,0,0,SA1,0,0,1,2,130,0,20,0,0,10,0,d20_robotStep,0,d21_base,0,d22_errcode,0,d23_PG1ActionNo,0,d24_PG2ActionNo,0,d25_ActionStatus,0,d26_GetParts,0,27,0,28,0,29];
SocketSend socketOmron\Data:=writeplcD21_29;
SocketReceive socketOmron\Data:=plcback;
IF plcback{16}<>0 THEN
SocketClose socketOmron;
RETURN FALSE;
ENDIF
d20_robotStep:=0;
d21_base:=0;
d22_errcode:=0;
d25_ActionStatus:=0;
d26_GetParts:=0;
RETURN TRUE;
ERROR
ResetRetryCount;
SocketClose socketOmron;
Set do06_SocketErr;
WaitTime 1;
Reset do06_SocketErr;
RETURN FALSE;
ENDFUNC
ENDMODULE
by 17551023102