背景
前面我們搭建了一個(gè)本地的 PLC
仿真環(huán)境,并通過(guò) KEPServerEX6
讀取 PLC
上的數(shù)據(jù),最后還使用 UAExpert
作為 OPC
客戶(hù)端完成從 KEPServerEX6
這個(gè)OPC服務(wù)器的數(shù)據(jù)讀取與訂閱功能:SpringBoot集成Milo庫(kù)實(shí)現(xiàn)OPC UA客戶(hù)端:連接、遍歷節(jié)點(diǎn)、讀取、寫(xiě)入、訂閱與批量訂閱。
注意,如果實(shí)際工作中,僅僅需要測(cè)試下 OPC UA
客戶(hù)端的功能,那么就Duck不必搭建本地的 PLC
仿真環(huán)境,而是借助一些 OPC UA
服務(wù)端的模擬工具。在這篇文章中,我們將使用 Prosys OPC UA Simulation Server
作為 OPC UA
的服務(wù)端,并通過(guò)我們前面自己實(shí)現(xiàn)的 OPC UA
客戶(hù)端來(lái)連接這個(gè)模擬的 OPC UA
的服務(wù)器,即:
通過(guò)
Milo
實(shí)現(xiàn)的OPC UA
客戶(hù)端連接Prosys OPC UA Simulation Server
模擬的OPC UA
服務(wù)器。
下載安裝:Prosys OPC UA Simulation Server
https://downloads.prosysopc.com/opc-ua-simulation-server-downloads.php
模擬OPC UA服務(wù)器
雙擊啟動(dòng) Prosys OPC UA Simulation Server
后,首頁(yè)顯示了服務(wù)器的地址信息。
如果需要修改這個(gè)默認(rèn)的連接地址,可通過(guò) Endpoints
菜單進(jìn)行設(shè)置(我這里用的是默認(rèn)的地址)。
在 Objects
菜單下,可以看到 Prosys OPC UA Simulation Server
默認(rèn)自帶了計(jì)數(shù)器、隨機(jī)數(shù)、梯形圖、鋸齒波、正弦波、三角波等節(jié)點(diǎn),可通過(guò) OPC UA
客戶(hù)端進(jìn)行讀取測(cè)試。
基于Milo實(shí)現(xiàn)的OPC UA客戶(hù)端測(cè)試
作為示例,以下通過(guò)連接服務(wù)器(匿名連接)、讀取指定節(jié)點(diǎn)的值以及訂閱指定節(jié)點(diǎn)來(lái)完成與 Prosys OPC UA Simulation Server
模擬 OPC UA
服務(wù)器的操作。
在實(shí)際編碼測(cè)試之前,可以先通過(guò) UAExpert
作為 OPC
客戶(hù)端完成從模擬服務(wù)器的數(shù)據(jù)讀取與訂閱功能,順便再次明確下 NodeId
的信息。
public class OpcUaStart {
public void start() throws Exception {
OpcUaClientService opcUaClientService = new OpcUaClientService();
// 與OPC UA服務(wù)端建立連接,并返回客戶(hù)端實(shí)例
OpcUaClient client = opcUaClientService.connectOpcUaServer("你的機(jī)器名稱(chēng)", "53530", "/OPCUA/SimulationServer");
// 兩種方式定義節(jié)點(diǎn)
NodeId nodeId = new NodeId(3, 1002); // 注意第2個(gè)參數(shù)類(lèi)型為數(shù)字
// NodeId nodeId = NodeId.parse("ns=3;i=1002"); // 通過(guò)parse靜態(tài)方法定義
// 讀取指定節(jié)點(diǎn)的值
opcUaClientService.readNodeValue(client, nodeId);
// 訂閱指定節(jié)點(diǎn)
opcUaClientService.subscribe(client, nodeId);
}
}
測(cè)試結(jié)果如下:
可能遇到的問(wèn)題
Prosys OPC UA Simulation Server 界面上沒(méi)有 Endpoints 菜單?
解決方法: Prosys OPC UA Simulation Server
界面上如果沒(méi)有 Endpoints
菜單,可通過(guò)左上角的 Options
菜單 Switch to Expert Mode
切換一下。
基于Milo實(shí)現(xiàn)的OPC UA客戶(hù)端如何讀取、訂閱 Prosys OPC UA Simulation Server 中的節(jié)點(diǎn)數(shù)據(jù)?
StatusCode{name=Bad_NodeIdUnknown, value=0x80340000, quality=bad}
原因分析: 根據(jù)狀態(tài)提示,再結(jié)合我們讀取節(jié)點(diǎn)數(shù)據(jù)的實(shí)現(xiàn): readNodeValue
方法關(guān)鍵的參數(shù)分別為: int namespaceIndex
, String identifier
。之前連接 KEPServer
和 Milo Server
時(shí) identifier
的類(lèi)型都是 String
,可以正常讀取,但是連接 Prosys OPC UA Simulation Server
后,無(wú)法讀取,我嘗試直接改為 int
類(lèi)型后,讀取成功。
解決方法:
方法1:將 readNodeValue
方法的 String identifier
參數(shù)改為 int identifier
,即在傳參時(shí)使用整數(shù)類(lèi)型,可以通過(guò)增加一個(gè)重載的方法實(shí)現(xiàn)。
public void readNodeValue(OpcUaClient client, int namespaceIndex, int identifier)
方法2: 修改 readNodeValue
方法直接接收 NodeId
類(lèi)型,這時(shí)可以通過(guò)各種方式定義 NodeId
, NodeId
提供了各種重載和解析方法。
public void readNodeValue(OpcUaClient client, NodeId nodeId)
// 方式1:構(gòu)造方法定義NodeId,注意第2個(gè)參數(shù)類(lèi)型為數(shù)字
NodeId nodeId = new NodeId(3, 1002);
// 方式2:靜態(tài)解析定義NodeId
NodeId nodeId = NodeId.parse("ns=3;i=1002");
Note:方式2:靜態(tài)解析定義NodeId,這種方法是我通過(guò)基于 GPT-4
大模型的 Cursor
問(wèn)答得知的:Cursor編程初體驗(yàn),搭載GPT-4大模型,你的AI助手,自然語(yǔ)言編程來(lái)了。
Reference
Prosys_OPC_UA_Simulation_Server_UserManual用戶(hù)手冊(cè)
Source Code
https://github.com/heartsuit/demo-spring-boot/tree/master/springboot-opcua
If you have any questions or any bugs are found, please feel free to contact me.文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-604559.html
Your comments and suggestions are welcome!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-604559.html
到了這里,關(guān)于通過(guò)Milo實(shí)現(xiàn)的OPC UA客戶(hù)端連接并訂閱Prosys OPC UA Simulation Server模擬服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!