更新 opcua client 版本

This commit is contained in:
caixiang 2022-03-20 09:19:25 +08:00
parent 83ac4db80f
commit aa38946d02
4 changed files with 40 additions and 35 deletions

14
pom.xml
View File

@ -21,24 +21,30 @@
<dependency> <dependency>
<groupId>org.eclipse.milo</groupId> <groupId>org.eclipse.milo</groupId>
<artifactId>sdk-client</artifactId> <artifactId>sdk-client</artifactId>
<version>0.6.3</version> <version>0.6.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.milo</groupId> <groupId>org.eclipse.milo</groupId>
<artifactId>stack-client</artifactId> <artifactId>stack-client</artifactId>
<version>0.6.3</version> <version>0.6.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.milo</groupId> <groupId>org.eclipse.milo</groupId>
<artifactId>stack-server</artifactId> <artifactId>stack-server</artifactId>
<version>0.6.3</version> <version>0.6.4</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.eclipse.milo</groupId> <groupId>org.eclipse.milo</groupId>
<artifactId>sdk-server</artifactId> <artifactId>sdk-server</artifactId>
<version>0.6.3</version> <version>0.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency> </dependency>
<!-- OPC UA 模块依赖 结束 --> <!-- OPC UA 模块依赖 结束 -->

View File

@ -1,9 +1,6 @@
package com.qgs.dc.s7.controller; package com.qgs.dc.s7.controller;
import com.qgs.dc.opcua.controller.R; import com.qgs.dc.opcua.controller.R;
import com.qgs.dc.s7.my.s7connector.api.DaveArea;
import com.qgs.dc.s7.my.s7connector.api.S7Connector;
import com.qgs.dc.s7.my.s7connector.api.utils.ByteUtils;
import com.qgs.dc.s7.my.s7connector.enmuc.PlcVarActual; import com.qgs.dc.s7.my.s7connector.enmuc.PlcVarActual;
import com.qgs.dc.s7.my.s7connector.enmuc.S7Client; import com.qgs.dc.s7.my.s7connector.enmuc.S7Client;
@ -16,13 +13,12 @@ import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/s7") @RequestMapping("/s7")
public class S7Controller { public class S7DemoController {
private static final Logger logger = LoggerFactory.getLogger(S7Controller.class); private static final Logger logger = LoggerFactory.getLogger(S7DemoController.class);
@Autowired @Autowired
S7Service s7Service; S7Service s7Service;

View File

@ -4,6 +4,7 @@ import com.qgs.dc.s7.my.s7connector.api.DaveArea;
import com.qgs.dc.s7.my.s7connector.api.S7Connector; import com.qgs.dc.s7.my.s7connector.api.S7Connector;
import com.qgs.dc.s7.my.s7connector.api.factory.S7ConnectorFactory; import com.qgs.dc.s7.my.s7connector.api.factory.S7ConnectorFactory;
import com.qgs.dc.s7.my.s7connector.api.utils.ByteUtils; import com.qgs.dc.s7.my.s7connector.api.utils.ByteUtils;
import com.qgs.dc.s7.my.s7connector.enmuc.S7Client;
import java.util.List; import java.util.List;
@ -19,13 +20,15 @@ public class MainForRead {
//缺陷 不支持 DB3.1.1 //缺陷 不支持 DB3.1.1
//Create connection //Create connection
S7Connector connector = // S7Connector connector =
S7ConnectorFactory // S7ConnectorFactory
.buildTCPConnector() // .buildTCPConnector()
.withHost("192.168.0.51") // .withHost("192.168.0.51")
.withRack(0) //optional rack 是机架号 // .withRack(0) //optional rack 是机架号
.withSlot(0) //optional slot 是插槽号 // .withSlot(0) //optional slot 是插槽号
.build(); // .build();
S7Connector connector = S7Client.S7_1200.getConnector();
// // [0] // // [0]

View File

@ -101,9 +101,10 @@ public enum S7Client {
for(int z=0;z<c;z++){ for(int z=0;z<c;z++){
S7Connector connect = connect(host, rack, slot); S7Connector connect = connect(host, rack, slot);
if(connect!=null){ if(connect!=null){
logger.info("(check_ping()) 检测到有断线 ==》 现在断线恢复成功");
connections.add(connect); connections.add(connect);
}else { }else {
logger.info("check_ping 断线重连出现异常。。"); logger.info("(check_ping()) 检测到有断线 ==》 现在断线恢复失败");
} }
} }
@ -111,13 +112,14 @@ public enum S7Client {
try { try {
Thread.sleep(10000); Thread.sleep(10000);
}catch (Exception e){ }catch (Exception e){
logger.info("check_ping"+e.getMessage()); logger.info("(check_ping()) Thread.sleep 异常,异常原因:"+e.getMessage());
} }
} }
} }
}); });
} }
//todo 当网络断开之后下面这个ping() 循环不生效 可能是read = connector.read( 出来是null但还是继续通过了也可能是 当连接全部断开后 getConnector(); 应该是取不到数据的也就会报null这个明天再看看
private void ping(){ private void ping(){
executor.execute(new Runnable() { executor.execute(new Runnable() {
@Override @Override
@ -137,7 +139,7 @@ public enum S7Client {
System.out.println(connector.hashCode()+" : ping"); System.out.println(connector.hashCode()+" : ping");
Thread.sleep(100); Thread.sleep(100);
}catch (Exception e){ }catch (Exception e){
logger.info(connector.hashCode()+" : connection error"+"errMessage is : "+e.getMessage()); logger.info("(ping) "+connector.hashCode()+" : connection error"+"errMessage is : "+e.getMessage());
//先把 socket close掉 //先把 socket close掉
try { try {
connector.close(); connector.close();
@ -145,22 +147,20 @@ public enum S7Client {
//如果是网络波动照成的socket断开 等个1S 再重连试试 //如果是网络波动照成的socket断开 等个1S 再重连试试
Thread.sleep(100); Thread.sleep(100);
}catch (Exception ee){ }catch (Exception ee){
logger.info("connector.close() 出现异常errMessage is : "+ee.getMessage()); logger.info("(ping) "+"connector.close() 出现异常errMessage is : "+ee.getMessage());
} }
}
//todo 把之前的连接close 然后新增一个连接到connections
//todo 把之前的连接close 然后新增一个连接到connections if(connections.size()!=coreSize){
int c = coreSize-connections.size();
if(connections.size()!=coreSize){ for(int z=0;z<c;z++){
int c = coreSize-connections.size(); S7Connector connect = connect(host, rack, slot);
for(int z=0;z<c;z++){ if(connect!=null){
S7Connector connect = connect(host, rack, slot); logger.info("(ping) "+"ping时候出现异常尝试重连 重连成功!!");
if(connect!=null){ connections.add(connect);
connections.add(connect); }else {
}else { logger.info("(ping) "+"ping时候出现异常尝试重连 重连时候还是 出现异常。。");
logger.info("ping时候出现异常尝试重连 重连时候还是 出现异常。。");
}
} }
} }
} }
@ -170,7 +170,7 @@ public enum S7Client {
Thread.sleep(30000); Thread.sleep(30000);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
logger.info(e.getMessage()); logger.info("(ping) "+e.getMessage());
} }
} }
} }