在 ssh框架中使用jeasyopc 连接 OPCserver

package com.luguang.ba.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;

import javax.annotation.PostConstruct;

import javafish.clients.opc.JCustomOpc;
import javafish.clients.opc.JEasyOpc;
import javafish.clients.opc.JOpc;
import javafish.clients.opc.asynch.AsynchEvent;
import javafish.clients.opc.asynch.OpcAsynchGroupListener;
import javafish.clients.opc.component.OpcGroup;
import javafish.clients.opc.component.OpcItem;
import javafish.clients.opc.exception.CoInitializeException;
import javafish.clients.opc.exception.ComponentNotFoundException;
import javafish.clients.opc.exception.ConnectivityException;
import javafish.clients.opc.exception.SynchReadException;
import javafish.clients.opc.exception.UnableAddGroupException;
import javafish.clients.opc.exception.UnableAddItemException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.luguang.ba.model.LgpIoR;

 

@Service
public class LgpOpcClient {
 
 static private String host="localhost";
 static private String serverPID="ICONICS.SimulatorOPCDA.2";
 static private String serverClientHandler="JOPC1";
 private JEasyOpc jopc;
 private OpcGroup readGroup;
 private OpcItem statusItem0,statusItem1,statusItem2;
 private LgpIoR lgpIoR=new LgpIoR();
 private LgpRealDataService lgpRealDataService;
 
 @Autowired
 public void setLgpRealDataService(LgpRealDataService service){
  this.lgpRealDataService =service;
 }
 /**
  * 初始化
  */
 @PostConstruct
 private void initOpc()
 {
  
  try
  {
   JOpc.coInitialize();//初始化服务器
   //建立server对象
   jopc = new JEasyOpc(host, serverPID, serverClientHandler);
   jopc.connect();//建立连接
   
   //建立Group对象
   readGroup= new OpcGroup("kksrhsjz3639_1",true,200,.0f);//kksrhsjz3639_1:OPC Gorup
   //建立Item对象
   statusItem0=new OpcItem("kksrhsjz3639_1.test1",true,"");//kksrhsjz3639_1.test1:OPC item
   statusItem1=new OpcItem("kksrhsjz3639_1.test2",true,"");
   statusItem2=new OpcItem("kksrhsjz3639_1.test3",true,"");
   //将Item对象添加到Group对象下
   readGroup.addItem(statusItem0);
   readGroup.addItem(statusItem1);
   readGroup.addItem(statusItem2);
   
   //将Group对象添加到JOPC对象下
   jopc.addGroup(readGroup);
   //注册Group对象
   jopc.registerGroups();
   jopc.registerItem(readGroup, statusItem2);
   jopc.start();
   readGroup.addAsynchListener(new OpcAsynchGroupListener(){
    public void getAsynchEvent(AsynchEvent event)
    {
     System.out.println(((JCustomOpc) event.getSource()).getFullOpcServerName() + "=>");
     System.out.println("Package: " + event.getID());
     System.out.println(event.getOPCGroup());
     try {
      OpcGroup responseGroup = jopc.synchReadGroup(readGroup);
      for (OpcItem i:responseGroup.getItems())
      {
       System.out.println(i.getItemName());
       System.out.println(i.getTimeStamp());
       System.out.println(i.isQuality());
       if(i.getItemName().equals(statusItem0.getItemName()))
       {
        System.out.println(i.getValue());
       }
       if(i.getItemName().equals(statusItem1.getItemName()))
       {
        System.out.println(i.getValue());
       }
       if(i.getItemName().equals(statusItem2.getItemName()))
       {
        System.out.println(i.getValue());
       }
      }
     } catch (ComponentNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (SynchReadException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   });
  } catch (ConnectivityException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  } catch (UnableAddGroupException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (UnableAddItemException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch(CoInitializeException e1)
  {
   e1.printStackTrace();
  }
 }
}
jeasyOPC开发包 需要资源分1分

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值