opc 带有session
现在使用java来做工控系统的几种方式:
知识储备:
一、OPC Server端目前常见的有以下几种协议:
参考博客:
https://www.cnblogs.com/ioufev/articles/9697717.html
https://www.cnblogs.com/ioufev/articles/9697890.html
二、DOCM的配置
可以参考https://www.cnblogs.com/ioufev/p/9365919.html
三、OPCServer服务器属性
java 获取OPCServer的方式有两种,
1、jeasyopc:适用于32位操作系统
2、Utgrad :可跨平台
可以根据具体情况具体分析,相比较对于初次接触来说jeasyopc相对简单。
四、试验模拟OPC
1、实验用模拟OPCServer(50M):MatrikonOPC
2、实际OPCServer使用(450M,中文):KEPServer V6
Utgard:
官网:http://openscada.org/projects/utgard/
博客参考
https://www.cnblogs.com/ioufev/articles/9894452.html
https://elfasd.iteye.com/blog/2064410
JeasyOPC:
博客参考:
https://blog.csdn.net/qq_33720460/article/details/78478430
https://blog.csdn.net/wangzhi291/article/details/45029799
https://blog.csdn.net/diyu122222/article/details/77645668
五:本人只采用了UtgradUt开发用例
/**
* OPCServer 客户端连接
*/
public class UtgrandClient {
private final String LOCAL_HOST="10.0.0.17";//OPCServer服务器IP
private final String USER_NAME ="OPCServer";//DOCM配置时OPCServer的用户名
private final String PASS_WORD ="Admin123";//密码
private final String CLS_ID ="7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729";//cls_id
private final String DOMAIN ="";
private int count;
Server server =null;
public void utGrandReadData(){
System.out.print("*********************************");
System.out.print("************启动加载配置项*********");
System.out.print("*********************************");
final ConnectionInformation ci =new ConnectionInformation();
ci.setHost(LOCAL_HOST);
ci.setUser(USER_NAME);
ci.setPassword(PASS_WORD);
ci.setClsid(CLS_ID);
ci.setDomain(DOMAIN);
if (ci.getClsid() !=null ){
JISession session = JISession.createSession ( ci.getDomain (),ci.getUser (),ci.getPassword () );
session.setGlobalSocketTimeout ((int) System.currentTimeMillis());
session.useSessionSecurity(true);
server =new Server(ci,Executors.newSingleThreadScheduledExecutor());
}else if (ci.getProgId () !=null){
JISession session = JISession.createSession ( ci.getDomain (), ci.getUser (), ci.getPassword () );
session.setGlobalSocketTimeout ((int) System.currentTimeMillis());
session.useSessionSecurity(true);
server =new Server(ci,Executors.newSingleThreadScheduledExecutor());
}
try {
server.connect();
Group group = server.addGroup("VC");
Item item = group.addItem("k.k.k");
// Map items = group.addItems("Random.Real1",
// "Random.Real2", "Random.Real3", "Random.Real4");
dumpItem(item);
// for (Entry temp : items.entrySet()) {
// dumpItem(temp.getValue());
// }
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取监测点
* @param item
* @throws JIException
*/
private void dumpItem(Item item)throws JIException {
System.out.println("[" + (++count) +"],ItemName:[" + item.getId()
+"],value:" + item.read(true).getValue());
}
}
/**
* 使用线程来控制 opc client连接,优化运行机内存。(48小时测试内存无增长)
*/
public class ThreadUtgrad {
private Date date;//对象锁
private boolean flag =false;//控制唤醒或等待线程
public ThreadUtgrad() {
date =new Date();
}
public ThreadUtgrad(Date date){
this.date = date;
}
public void run() {
while(true){
synchronized(date){
if(flag){
try {
Thread.sleep(500);
}catch (InterruptedException e) {
e.printStackTrace();
}
date.notify();
flag =false;
}
}
}
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
class PrintMessageextends Thread{
@Override
public void run() {
UtgrandClient utgrandClient =new UtgrandClient();
while(true){
synchronized(date){
if(!flag){
try {
flag =true;
date.wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("0.5秒的消息");
utgrandClient.utGrandReadData();
}
}
}
}
@PostConstruct
public void startUtgrandClient() {
Date date =new Date();
//打印5秒消息的线程
ThreadUtgrad threadUtgrad =new ThreadUtgrad(date);
PrintMessage printMessage = threadUtgrad.new PrintMessage();
printMessage.start();
threadUtgrad.run();
// //不影响打印线程的7秒消息线程
// new Thread(new Runnable(){
//
// @Override
// public void run() {
// while(true){
// try {
// Thread.sleep(7000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("7秒的消息");
// }
//
// }
//
// }).start();
}
作者:MartinX_c56e
链接:https://www.jianshu.com/p/afb45f585731
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。