JAVA使用JeasyOPC读取OPC 服务器数据

首先在src下新建包javafish.clients.opc 将JCustomOpc.properties 拷贝到包中, 然后导入三个jar包

jeasyopc.jar commons-logging-1.1.jar和log4j-1.2.13.jar 

在项目下新建一个lib目录,也可以自定义名字和路径,但是在JCustomOpc.properties 配置中能指定这个目录。

将JCustomOpc.dll拷贝到 你定义好的目录下。


写一个测试类,添加以下代码。

[java]  view plain  copy
  1.   public static void main(String[] args) throws InterruptedException {  
  2.           Opc test = new Opc();  
  3.               
  4.             JOpc.coInitialize();  
  5.               
  6.             JOpc jopc = new JOpc("localhost""Matrikon.OPC.Simulation""JOPC1");  
  7.   
  8. //          JOpc jopc = new JOpc("192.168.10.68", "Matrikon.OPC.Simulation.1", "JOPC1");  
  9.             OpcItem item1 = new OpcItem("Random.ArrayOfReal8"true"");  
  10. //          OpcItem item1 = new OpcItem("Random.Int2", true, "");  
  11.             OpcGroup group = new OpcGroup("group1"true10000.0f);  
  12.               
  13.             group.addItem(item1);  
  14.             jopc.addGroup(group);  
  15.               
  16.             try {  
  17.               jopc.connect();  
  18.               System.out.println("JOPC client is connected...");  
  19.             }  
  20.             catch (ConnectivityException e2) {  
  21.               e2.printStackTrace();  
  22.             }  
  23.               
  24.             try {  
  25.               jopc.registerGroups();  
  26.               System.out.println("OPCGroup are registered...");  
  27.             }  
  28.             catch (UnableAddGroupException e2) {  
  29.               e2.printStackTrace();  
  30.             }  
  31.             catch (UnableAddItemException e2) {  
  32.               e2.printStackTrace();  
  33.             }  
  34.               
  35.             synchronized(test) {  
  36.               test.wait(50);  
  37.             }  
  38.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");  
  39.             // Synchronous reading of item  
  40.             int cycles = 100;  
  41.             int acycle = 0;  
  42.             while (acycle++ < cycles) {  
  43.               synchronized(test) {  
  44.                 test.wait(1000);  
  45.               }  
  46.               try {  
  47.                 OpcItem responseItem = jopc.synchReadItem(group, item1);  
  48.                 System.out.println(responseItem);  
  49.                 System.out.println(sdf.format(responseItem.getTimeStamp().getTime())+":"+ Variant.getVariantName(responseItem.getDataType()) + ": " + responseItem.getValue());  
  50.               }  
  51.               catch (ComponentNotFoundException e1) {  
  52.                 e1.printStackTrace();  
  53.               }  
  54.               catch (SynchReadException e) {  
  55.                 e.printStackTrace();  
  56.               }  
  57.             }  
  58.               
  59.             JOpc.coUninitialize();  
  60.           }  


确保自己机器已经运行了服务端。

如图:




JCustomOpc.properties 和JCustomOpc.dll 目录要准确,不然就容易报错找不到配置文件。

猜你喜欢

转载自blog.csdn.net/knight_key/article/details/80813954