Java如何使用KEPserver 實現OPC通信


一.PLC和OPC

使用的PLC:西門子PLC S7-200 SMART 

使用的OPC server軟件:

KEPServer V6  百度網盤    密碼:2080   (備注:KEPserver 需要付費的 正版價格8800元/1個電腦 ,非正版使用期為一個月,過期后KEPserver 使用2小時后 KEPserver 失效——————目前沒見過破解永久使用權的)

二.連接測試

OPC是工業控制和生產自動化領域中使用的硬件和軟件的接口標准,以便有效地在應用和過程控制設備之間讀寫數據。O代表OLE(對象鏈接和嵌入),P (process過程),C (control控制)。

OPC服務器包括3類對象(Object):服務器對象(Server)、項對象(Item)和組對象(Group)。

OPC標准采用C/S模式,OPC服務器負責向OPC客戶端不斷的提供數據

什么是OPC?原文介紹

 

Server和Client

要實現的是Client(Java)和Client(PLC)之間的通信

中間借助OPCServer,Server上設定好地址變量,不同的Client讀寫這些變量值實現通信。

示意圖如下

OPC和DCOM配置通信不成功都是配置的問題。。

配置OPCserver
一般一個電腦(win10)同時安裝Server(比如KEPServer)和Client(Java編寫的),就配置這個電腦就行
如果是在兩個電腦上,那就都需要配置。

三.實現通信

Utgard:

      

Github上的

四.實現過程

 1.opc的概念

2.關於OPC UA

  • OPC UA的西門子PLC至少是s7-1500

3.關於Utgard

  • utgard是一個開源的項目,基於j-interop做的,用於和OPC SERVER通訊。
  • j-interop是純java封裝的用於COM/DCOM通訊的開源項目,這樣就不必使用JNI

4.關於JeasyOPC

  • JeasyOPC源碼下載
  • 借助一個dll庫來實現的和OPCServer的通信,但是JCustomOpc.dll,,太老了,而且支持只32位系統

五.maven依賴

 1 <!--utgard -->
 2         <dependency>
 3             <groupId>org.openscada.external</groupId>
 4             <artifactId>org.openscada.external.jcifs</artifactId>
 5             <version>1.2.25</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.openscada.jinterop</groupId>
 9             <artifactId>org.openscada.jinterop.core</artifactId>
10             <version>2.1.8</version>
11         </dependency>
12         <dependency>
13             <groupId>org.openscada.jinterop</groupId>
14             <artifactId>org.openscada.jinterop.deps</artifactId>
15             <version>1.5.0</version>
16         </dependency>
17         <dependency>
18             <groupId>org.openscada.utgard</groupId>
19             <artifactId>org.openscada.opc.dcom</artifactId>
20             <version>1.5.0</version>
21         </dependency>
22         <dependency>
23             <groupId>org.openscada.utgard</groupId>
24             <artifactId>org.openscada.opc.lib</artifactId>
25             <version>1.5.0</version>
26         </dependency>
27         <dependency>
28             <groupId>org.bouncycastle</groupId>
29             <artifactId>bcprov-jdk15on</artifactId>
30             <version>1.61</version>
31         </dependency>
32         <dependency>
33             <groupId>ch.qos.logback</groupId>
34             <artifactId>logback-core</artifactId>
35             <version>1.3.0-alpha4</version>
36         </dependency>
37         <dependency>
38             <groupId>ch.qos.logback</groupId>
39             <artifactId>logback-classic</artifactId>
40             <version>1.3.0-alpha4</version>
41             <scope>test</scope>
42         </dependency>

 

 

 

  

說明

地址變量進行讀取數值和寫入數值操作,一般分循環和批量兩種方式,(同步和異步就不討論了)

  • 循環讀取:Utgard提供了一個AccessBase類來循環讀取數值
  • 循環寫入:啟動一個線程來循環寫入數值
  • 批量讀取:通過組(Group),增加項(Item)到組,然后對Item使用read()
  • 批量寫入:通過組(Group),增加項(Item)到組,然后對Item使用write()

讀取PLC 的點位值

 1 import java.util.concurrent.Executors;
 2 
 3 import org.jinterop.dcom.common.JIException;
 4 import org.jinterop.dcom.core.JIString;
 5 import org.jinterop.dcom.core.JIVariant;
 6 import org.openscada.opc.lib.common.ConnectionInformation;
 7 import org.openscada.opc.lib.da.AccessBase;
 8 import org.openscada.opc.lib.da.DataCallback;
 9 import org.openscada.opc.lib.da.Item;
10 import org.openscada.opc.lib.da.ItemState;
11 import org.openscada.opc.lib.da.Server;
12 import org.openscada.opc.lib.da.SyncAccess;
13  
14 public class UtgardTutorial1 {
15  
16     public static void main(String[] args) throws Exception {
17         // 連接信息
18         final ConnectionInformation ci = new ConnectionInformation(); 
19         ci.setHost("192.168.0.1");         // 電腦IP
20         ci.setDomain("");                  // 域,為空就行
21         ci.setUser("OPCUser");             // 電腦上自己建好的用戶名 (之前DCOM 配置過)
22         ci.setPassword("123456");          // 密碼(用戶名密碼)
23  
24         // 使用MatrikonOPC Server的配置
25         // ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305"); // MatrikonOPC的注冊表ID,可以在“組件服務”里看到
26         // final String itemId = "u.u";    // MatrikonOPC Server上配置的項的名字按實際
27  
28         // 使用KEPServer的配置
29         ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注冊表ID,可以在“組件服務”里看到
30         final String itemId = "u.u.u";    // KEPServer上配置的項的名字,沒有實際PLC,用的模擬器:simulator (KEPserver 建立通道,建立設備,建立點位)通過標記點位名字定位要讀取的值
31         // final String itemId = "通道 1.設備 1.標記 1";
32  
33         // 啟動服務
34         final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
35  
36         try {
37             // 連接到服務
38             server.connect();
39             // add sync access, poll every 500 ms,啟動一個同步的access用來讀取地址上的值,線程池每500ms讀值一次
40             // 這個是用來循環讀值的,只讀一次值不用這樣
41             final AccessBase access = new SyncAccess(server, 500);
42             // 這是個回調函數,就是讀到值后執行這個打印,是用匿名類寫的,當然也可以寫到外面去
43             access.addItem(itemId, new DataCallback() {
44                 @Override
45                 public void changed(Item item, ItemState itemState) {
46                     int type = 0;
47                     try {
48                         type = itemState.getValue().getType(); // 類型實際是數字,用常量定義的
49                     } catch (JIException e) {
50                         e.printStackTrace();
51                     }
52                     System.out.println("監控項的數據類型是:-----" + type);
53                     System.out.println("監控項的時間戳是:-----" + itemState.getTimestamp().getTime());
54                     System.out.println("監控項的詳細信息是:-----" + itemState);
55  
56                     // 如果讀到是short類型的值
57                     if (type == JIVariant.VT_I2) {
58                         short n = 0;
59                         try {
60                             n = itemState.getValue().getObjectAsShort();
61                         } catch (JIException e) {
62                             e.printStackTrace();
63                         }
64                         System.out.println("-----short類型值: " + n); 
65                     }
66  
67                     // 如果讀到是字符串類型的值
68                     if(type == JIVariant.VT_BSTR) {  // 字符串的類型是8
69                         JIString value = null;
70                         try {
71                             value = itemState.getValue().getObjectAsString();
72                         } catch (JIException e) {
73                             e.printStackTrace();
74                         } // 按字符串讀取
75                         String str = value.getString(); // 得到字符串
76                         System.out.println("-----String類型值: " + str); 
77                     }
78                 }
79             });
80             // start reading,開始讀值
81             access.bind();
82             // wait a little bit,有個10秒延時
83             Thread.sleep(10 * 1000);
84             // stop reading,停止讀取
85             access.unbind();
86         } catch (final JIException e) {
87             System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode())));
88         }
89     }
90 }

  注意:if(type == JIVariant.VT_BSTR)//返回數字隊形返回值的類型   這里的對應的類型不是java的類型  

       類型對照表:

讀取數值與寫入數值

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.core.JIVariant;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.AccessBase;
import org.openscada.opc.lib.da.DataCallback;
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
import org.openscada.opc.lib.da.ItemState;
import org.openscada.opc.lib.da.Server;
import org.openscada.opc.lib.da.SyncAccess;
 
public class UtgardTutorial2 {
    
    public static void main(String[] args) throws Exception {
 
        // 連接信息 
        final ConnectionInformation ci = new ConnectionInformation();
        
        ci.setHost("192.168.0.1");          // 電腦IP
        ci.setDomain("");                   // 域,為空就行
        ci.setUser("OPCUser");              // 用戶名,配置DCOM時配置的
        ci.setPassword("123456");           // 密碼
        
        // 使用MatrikonOPC Server的配置
        // ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305"); // MatrikonOPC的注冊表ID,可以在“組件服務”里看到
        // final String itemId = "u.u";    // 項的名字按實際
 
        // 使用KEPServer的配置
        ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注冊表ID,可以在“組件服務”里看到
        final String itemId = "u.u.u";    // 項的名字按實際,沒有實際PLC,用的模擬器:simulator
        // final String itemId = "通道 1.設備 1.標記 1";
        
        // create a new server,啟動服務
        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
        try {
            // connect to server,連接到服務
            server.connect();
 
            // add sync access, poll every 500 ms,啟動一個同步的access用來讀取地址上的值,線程池每500ms讀值一次
            // 這個是用來循環讀值的,只讀一次值不用這樣
            final AccessBase access = new SyncAccess(server, 500);
            // 這是個回調函數,就是讀到值后執行再執行下面的代碼,是用匿名類寫的,當然也可以寫到外面去
            access.addItem(itemId, new DataCallback() {
                @Override
                public void changed(Item item, ItemState state) {
                    // also dump value
                    try {
                        if (state.getValue().getType() == JIVariant.VT_UI4) { // 如果讀到的值類型時UnsignedInteger,即無符號整形數值
                            System.out.println("<<< " + state + " / value = " + state.getValue().getObjectAsUnsigned().getValue());
                        } else {
                            System.out.println("<<< " + state + " / value = " + state.getValue().getObject());
                        }
                    } catch (JIException e) {
                        e.printStackTrace();
                    }
                }
            });
 
            // Add a new group,添加一個組,這個用來就讀值或者寫值一次,而不是循環讀取或者寫入
            // 組的名字隨意,給組起名字是因為,server可以addGroup也可以removeGroup,讀一次值,就先添加組,然后移除組,再讀一次就再添加然后刪除
            final Group group = server.addGroup("test"); 
            // Add a new item to the group,
            // 將一個item加入到組,item名字就是MatrikonOPC Server或者KEPServer上面建的項的名字比如:u.u.TAG1,PLC.S7-300.TAG1
            final Item item = group.addItem(itemId);
 
            // start reading,開始循環讀值
            access.bind();
 
            // add a thread for writing a value every 3 seconds
            // 寫入一次就是item.write(value),循環寫入就起個線程一直執行item.write(value)
            ScheduledExecutorService writeThread = Executors.newSingleThreadScheduledExecutor();
            writeThread.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    final JIVariant value = new JIVariant("24");  // 寫入24
                    try {
                        System.out.println(">>> " + "寫入值:  " + "24");
                        item.write(value);
                    } catch (JIException e) {
                        e.printStackTrace();
                    }
                }
            }, 5, 3, TimeUnit.SECONDS); // 啟動后5秒第一次執行代碼,以后每3秒執行一次代碼
 
            // wait a little bit ,延時20秒
            Thread.sleep(20 * 1000);
            writeThread.shutdownNow();  // 關掉一直寫入的線程
            // stop reading,停止循環讀取數值
            access.unbind();
        } catch (final JIException e) {
            System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode())));
        }
    }
}

 

數組類型

如果地址變量的數據類型是數組類型呢?

 1 // 讀取Float類型的數組
 2 if (type == 8196) { // 8196是打印state.getValue().getType()得到的
 3     JIArray jarr = state.getValue().getObjectAsArray(); // 按數組讀取
 4     Float[] arr = (Float[]) jarr.getArrayInstance();  // 得到數組
 5     String value = "";
 6     for (Float f : arr) {
 7         value = value + f + ",";
 8     }
 9     System.out.println(value.substring(0, value.length() - 1); // 遍歷打印數組的值,中間用逗號分隔,去掉最后逗號
10 }
11 
12 // 寫入3位Long類型的數組
13 Long[] array = {(long) 1,(long) 2,(long) 3}; 
14 final JIVariant value = new JIVariant(new JIArray(array));
15 item.write(value);

 

值(十進制) 數據類型 描述
0 VT_EMPTY 默認/空(無)
2 VT_I2 2字節有符號整數
3 VT_I4 4字節有符號整數
4 VT_R4 4字節實數
5 VT_R8 8字節實數
6 VT_C currency
7 VT_DATE 日期
8 VT_BSTR 文本
10 VT_ERROR 錯誤代碼
11 VT_BOOL 布爾值(TRUE = -1,FALSE = 0)
17 VT_I1 1個字節有符號字符
18 VT_UI1 1個字節無符號字符
19 VT_UI2 2字節無符號整數
20 VT_UI4 4字節無符號整數
+8192 VT_ARRAY 值數組(即8200 =文本值數組)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM