From 38429926953fdcebd7b06a20d51284c86e034cdd Mon Sep 17 00:00:00 2001 From: caixiang <939387484@qq.com> Date: Sat, 2 Apr 2022 14:02:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20=E3=80=82S7=20=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=9C=A8=E5=BC=95=E7=94=A8=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dc/s7/controller/S7DemoController.java | 25 ++++++++++++++++++- .../dc/s7/my/s7connector/enmuc/S7Client.java | 4 +-- .../impl/nodave/TCPConnection.java | 19 +++++++++----- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java b/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java index 1d06a2c..893b30f 100644 --- a/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java +++ b/src/main/java/com/qgs/dc/s7/controller/S7DemoController.java @@ -74,7 +74,7 @@ public class S7DemoController { String[] toWrite = new String[60]; for(int i=0;i<60;i++){ int i1 = new Random().nextInt(100); - toWrite[i] = "3011001210530111"+ i1; + toWrite[i] = "2212"+ i1; } ////测试结果 c => 57ms long c1 = System.currentTimeMillis(); @@ -88,6 +88,29 @@ public class S7DemoController { return R.ok().put("l",(l1-l)).put("c",(c2-c1)); } + + @PostMapping("/testForString1200") + public R testForString1200() throws UnsupportedEncodingException, ParseException { + //测试结果 l => 66ms + long l = System.currentTimeMillis(); + String[] subs = (String[])s7Service.read(PlcVarActual.SubIdArrays1200, S7Client.S7_1200); + long l1 = System.currentTimeMillis(); + + String[] toWrite = new String[60]; + for(int i=0;i<60;i++){ + int i1 = new Random().nextInt(100); + toWrite[i] = "2212"+ i1; + } + ////测试结果 c => 57ms + long c1 = System.currentTimeMillis(); + s7Service.write(PlcVarActual.SubIdArrays1200,toWrite,S7Client.S7_1200); + long c2 = System.currentTimeMillis(); + + + String[] subs2 = (String[])s7Service.read(PlcVarActual.SubIdArrays1200, S7Client.S7_1200); + + return R.ok().put("l",(l1-l)).put("c",(c2-c1)); + } @PostMapping("/testFor1200") public R testFor1200() throws UnsupportedEncodingException, ParseException { Object subs = s7Service.read(PlcVarActual.INT1200, S7Client.S7_1200); diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java index 97e7f8f..1867c34 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/enmuc/S7Client.java @@ -18,9 +18,9 @@ import java.util.concurrent.ScheduledExecutorService; public enum S7Client { //TODO 步骤1 这里是配置多PLC 的,,,有多个plc 就在这里配置一个枚举类 //1500 虽然设备上显示机架-0 插槽-1(这个是默认的) 但是我们这里 rack、slot 都是0,反正我们这里就让电控rack和solt都是默认的 然后我们这边都配置0就行了。 - S7_1500("192.168.0.51",0,0,3,PlcVarActual.HeartBeat), + S7_1500("192.168.0.51",0,1,3,PlcVarActual.HeartBeat), //1500 机架-0 插槽-1 - S7_1200("192.168.0.52",0,0,3,PlcVarActual.HeartBeatFor1200) + S7_1200("192.168.0.52",0,1,3,PlcVarActual.HeartBeatFor1200) //后续 在这里扩展 多PLC应用。 diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/impl/nodave/TCPConnection.java b/src/main/java/com/qgs/dc/s7/my/s7connector/impl/nodave/TCPConnection.java index 132daec..83c3797 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/impl/nodave/TCPConnection.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/impl/nodave/TCPConnection.java @@ -67,13 +67,13 @@ public final class TCPConnection extends S7Connection { (byte) 0xC1, //parameter-code:dst-tsap 上位机 (byte) 0x02, //parameter-length - (byte) 0x01, //Source rack 机架 - (byte) 0x00, //Source slot 槽位号 + (byte) 0x10, //Source TSAP: 0x01->PG; 0x02->OP; 0x03->S7单边通讯(服务端模式); 0x10->S7双边通讯;;; 这里是0x01 + (byte) 0x00, //机架(rack)(前4个bit位)+ cpu槽位(slot)(后4个bit位) ,, 因为是上位机端 所以这里都是00(rack=0 slot=2) (byte) 0xC2, //parameter-code:dst-tsap PLC (byte) 0x02, //parameter-length - (byte) 0x01, //Destination rack 机架 (Des rack 指的就是 plc rack) (msgOut[17]) - (byte) 0x02, //Destination slot 槽位号 (Des slot 指的就是 plc slot) (msgOut[18]) + (byte) 0x03, //Destination TSAP: 0x01->PG; 0x02->OP; 0x03->S7单边通讯(服务端模式); 0x10->S7双边通讯;;; 这里是0x01 + (byte) 0x01, //机架(rack)(前4个bit位)+ cpu槽位(slot)(后4个bit位),, 西门子200smart、1200、1500默认 机架号=0,槽位号=1 ;;;; 西门子300、400默认 机架号=0,槽位号=2 (byte) 0xC0, //parameter-code:tpdu-size (byte) 0x01, //parameter-length @@ -84,8 +84,10 @@ public final class TCPConnection extends S7Connection { //之所以目的位置4 是因为,前面4个字节是TPKT的内容(第一次握手) // msgOut 是这个socketChannel 往外写的 bytes(上位机 => plc 写) ;msgIn 是指这个socketChanne 外部往里写的bytes(上位机 <= plc 写) System.arraycopy(b4, 0, this.msgOut, 4, b4.length); - this.msgOut[17] = (byte) (this.rack + 1); - this.msgOut[18] = (byte) this.slot; + //this.msgOut[17] = (byte) (this.rack + 1); //这里写的不太对,这里应该是配置dst-tsap(plc) + //this.msgOut[18] = (byte) this.slot; + this.msgOut[18] = combineToByte(rack,slot); + this.sendISOPacket(b4.length); this.readISOPacket(); /* @@ -95,6 +97,11 @@ public final class TCPConnection extends S7Connection { return this.negPDUlengthRequest(); } + private static byte combineToByte(Integer i,Integer j){ + Integer cc = ((i << 4)+j); + return cc.byteValue(); + } + /** * 构建S7COMM 的COTP部分 3个字节(第二次握手) *