想通过java 或C#读写和订阅OPCUA的数据,但是没有OPC相关的依赖jar包和资料(网上有好多用milo做的,但是我都找不全),有没有牛人能给提供下相关的jar包和demo程序呢,有需要的话可以交些学费
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
你可以使用OPC Foundation的UA-.NETStandard和UA-Java SDK来访问OPC UA数据。
在Java上,可以使用Eclipse Milo,一个开源的OPC UA Stack实现。Milo非常易于使用,它提供了可用于各种用例的高级API,例如:
以下是使用Eclipse Milo访问OPC UA数据的简单示例:
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
import org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.eclipse.milo.opcua.stack.core.types.enumerated.IdType;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.LocalizedText;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
public class Example {
public static void main(String[] args) throws Exception {
// Replace with the correct endpoint URL for your server
String endpointUrl = "opc.tcp://localhost:12686/sample";
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setEndpoint(endpointUrl)
.setIdentityProvider(new X509IdentityProvider(
cert -> "user",
cert -> cert.getSubject().getRfc822Name()))
.build();
OpcUaClient client = new OpcUaClient(config);
CompletableFuture<Void> future = client.connect();
// Block until connected, or timeout after 15 seconds
future.get();
// Browse for nodes on the server
CompletableFuture<List<ReferenceDescription>> browseFuture = client.browseFlat();
List<ReferenceDescription> nodes = browseFuture.get();
// Find the node you want to read or subscribe to
NodeId nodeId = new NodeId(0, "MyObject");
// Read the value of the node
CompletableFuture<DataValue> readFuture = client.readValue(nodeId);
DataValue value = readFuture.get();
System.out.println(value.getValue());
// Subscribe to updates on the node in real-time
CompletableFuture<StatusCode> subscribeFuture = client.subscribe(nodeId);
// Block until subscription is active, or timeout after 5 seconds
subscribeFuture.get();
for (int i = 0; i < 10; i++) {
// Wait for a notification to arrive
CompletableFuture<DataValue> notificationFuture = client.nextNotification();
DataValue notification = notificationFuture.get();
System.out.println(notification.getValue());
}
// Shutdown the client gracefully
client.disconnect().get();
}
}
在C#上,可以使用OPC Foundation .NET Stack实现OPC UA。
以下是使用OPC Foundation .NET Stack来访问OPC UA数据的简单示例:
using System;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
public class Example {
static async Task Main(string[] args) {
// Replace with the correct endpoint URL for your server
Uri endpointUrl = new Uri("opc.tcp://localhost:12686/sample");
// Create a new session using a self-signed certificate
var session = await Session.Create(
configuration: new SessionConfiguration {
UserIdentity = new UserIdentity(new AnonymousIdentityToken()),
Endpoint = new ConfiguredEndpoint(null, new EndpointDescription {
EndpointUrl = endpointUrl.ToString(),
SecurityMode = MessageSecurityMode.None,
SecurityPolicyUri = SecurityPolicy.None.Uri
}),
TransportSendingBufferSize = 10 * 1024 * 1024,
OperationTimeout = 60 * 1000,
});
// Browse for nodes on the server
var browseRequest = new BrowseRequest {
NodesToBrowse = new[] {
new BrowseDescription {
NodeId = NodeId.Parse("ns=0;i=85"), // Browse the root folder
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
NodeClassMask =
(uint) (NodeClass.Object | NodeClass.Variable | NodeClass.Method),
ResultMask = (uint) (BrowseResultMask.All)
}
}
};
var browseResponse = await session.BrowseAsync(browseRequest);
// Find the node you want to read or subscribe to
var nodeId = NodeId.Parse("ns=0;i=85/2:MyObject");
// Read the value of the node
var readRequest = new ReadRequest {
NodesToRead = new[] {
new ReadValueId {
NodeId = nodeId,
AttributeId = Attributes.Value
}
}
};
var readResponse = await session.ReadAsync(readRequest);
var value = readResponse.Results[0].Value;
Console.WriteLine(value);
// Subscribe to updates on the node in real-time
var subscription = new Subscription(session.DefaultSubscription) {
PublishingInterval = 1000,
KeepAliveCount = 10,
LifetimeCount = 20,
MaxNotificationsPerPublish = 1000,
PublishingEnabled = true,
Priority = 1
};
var monitoredItem = new MonitoredItem(subscription.DefaultItem) {
StartNodeId = nodeId,
AttributeId = Attributes.Value,
MonitoringMode = MonitoringMode.Reporting,
SamplingInterval = 100,
DiscardOldest = true
};
await subscription.AddItemAsync(monitoredItem);
subscription.Create();
Console.WriteLine("Subscribed to changes on node {0}", nodeId);
for (int i = 0; i < 10; i++) {
// Wait for a notification to arrive
var notification = await subscription.WaitNextNotificationAsync();
Console.WriteLine(notification.Value);
}
// Dispose of resources and close the session gracefully
await session.CloseAsync();
}
}
这些示例应该可以帮助你开始使用Java或C#访问OPC UA数据并进行开发。希望对你有所帮助!
如果我的回答解决了您的问题,请采纳!
参考GPT和自己的思路,当涉及到使用Java或C#读取和订阅OPC UA数据时,您可以使用以下开源库和工具:
Java
Eclipse Milo:Eclipse Milo是一个开源的OPC UA实现,支持Java平台。您可以从这里下载Eclipse Milo的jar包和文档。此外,Eclipse Milo还提供了示例代码和文档,帮助您快速入门。
https://projects.eclipse.org/projects/iot.milo
Unified Automation Java SDK:Unified Automation Java SDK是一个商业级OPC UA实现,支持Java平台。您可以从这里下载Unified Automation Java SDK,并参考其文档和示例代码。
https://www.unified-automation.com/downloads/opc-ua-development.html
C#
OPC Foundation .NET库:OPC Foundation提供了一组C#库,用于构建OPC UA客户端和服务器。您可以从这里下载OPC Foundation .NET库,并查看其文档和示例代码。
Prosys OPC UA .NET SDK:Prosys OPC UA .NET SDK是一个商业级OPC UA实现,支持C#平台。您可以从这里了解有关Prosys OPC UA .NET SDK的详细信息,并下载其示例代码和文档。
https://prosysopc.com/products/opc-ua-net-sdk/
希望这些资源能够帮助您开始使用Java或C#读取和订阅OPC UA数据。
该回答引用ChatGPT
如有疑问,可以回复我!
您可以尝试使用Eclipse Milo来读写和订阅OPC UA的数据。Eclipse Milo是一个基于Java实现的OPC UA库,可以帮助您轻松地与OPC UA服务器进行通信。
以下是使用Eclipse Milo进行读写和订阅OPC UA数据的步骤:
1、下载Eclipse Milo库
您可以从Eclipse Milo的官方网站(https://github.com/eclipse/milo%EF%BC%89%E4%B8%8B%E8%BD%BD%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84%E5%BA%93%E6%96%87%E4%BB%B6%E3%80%82
2、添加Eclipse Milo库文件
将Eclipse Milo库文件添加到您的项目中。您可以将Eclipse Milo的jar文件添加到您的项目中,或者使用Maven来添加依赖项。
3、创建OPC UA客户端
创建一个OPC UA客户端对象,以连接到您的OPC UA服务器。您可以使用以下代码来创建一个OPC UA客户端:
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english("My Application"))
.setApplicationUri("urn:example-application")
.setEndpoint( new EndpointDescription(endpointUrl) )
.build();
OpcUaClient client = new OpcUaClient(config);
在这里,endpointUrl是您的OPC UA服务器的URL地址。
4、连接到OPC UA服务器
使用客户端对象连接到您的OPC UA服务器。您可以使用以下代码连接到服务器:
client.connect().get();
5、读取OPC UA数据
使用客户端对象读取您的OPC UA服务器上的数据。您可以使用以下代码读取数据:
NodeId nodeId = new NodeId(namespaceIndex, identifier);
DataValue value = client.readValue(0, TimestampsToReturn.Both, nodeId).get();
在这里,namespaceIndex和identifier是节点的命名空间索引和标识符。
6、订阅OPC UA数据
使用客户端对象订阅您的OPC UA服务器上的数据。您可以使用以下代码订阅数据:
NodeId nodeId = new NodeId(namespaceIndex, identifier);
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
UaMonitoredItem item = subscription.createMonitoredItem(new MonitoredItemCreateRequest(nodeId, MonitoringMode.Reporting, new MonitoringParameters(uint(1), Duration.ofSeconds(1.0), null, uint(10), true))).get();
item.addValueChangedListener(value -> {
// 处理数据更新事件
});
在这里,namespaceIndex和identifier是节点的命名空间索引和标识符。
这些代码片段可以帮助您开始使用Eclipse Milo进行OPC UA通信。您可以在Eclipse Milo的官方文档(https://miloweb.readthedocs.io/en/latest/%EF%BC%89%E4%B8%AD%E6%89%BE%E5%88%B0%E6%9B%B4%E5%A4%9A%E5%85%B3%E4%BA%8EEclipse Milo的信息和示例代码。
milo demo 它的官方仓库就有,非常详细
git clone https://ghproxy.com/https://github.com/eclipse/milo.git
./milo/milo-examples/client-examples/src/main/java/org/eclipse/milo/examples/client/EventSubscriptionExample.java
./milo/milo-examples/client-examples/src/main/java/org/eclipse/milo/examples/client/ReadExample.java
./milo/milo-examples/client-examples/src/main/java/org/eclipse/milo/examples/client/WriteExample.java
基于bing、GPT部分内容和本人思考总结:
如果你想在Java或C#中读写和订阅OPC UA的数据,可以使用以下工具和库:
Java:
Eclipse Milo:Eclipse Milo是一款开源的Java OPC UA库,支持OPC UA协议的客户端和服务器端,可以用于读写和订阅OPC UA的数据。你可以在Eclipse Milo的Github页面下载相关的jar包和示例代码。
Unified Automation Java SDK:Unified Automation Java SDK是一款商业级别的Java OPC UA库,支持OPC UA协议的客户端和服务器端,提供了丰富的API和示例代码,可以用于读写和订阅OPC UA的数据。你可以在Unified Automation的官网购买该库并下载相关的jar包和示例代码。
C#:
OPC Foundation .NET Standard Stack:OPC Foundation .NET Standard Stack是一款开源的C# OPC UA库,支持OPC UA协议的客户端和服务器端,可以用于读写和订阅OPC UA的数据。你可以在OPC Foundation的Github页面下载相关的库和示例代码。
Unified Automation .NET SDK:Unified Automation .NET SDK是一款商业级别的C# OPC UA库,支持OPC UA协议的客户端和服务器端,提供了丰富的API和示例代码,可以用于读写和订阅OPC UA的数据。你可以在Unified Automation的官网购买该库并下载相关的库和示例代码。
以上是一些常用的Java和C# OPC UA库和示例代码,你可以根据自己的需求和实际情况选择合适的库和工具。
您可以使用以下资源来读写和订阅OPC UA数据:
Eclipse Milo:Eclipse Milo是一个Java实现的OPC UA协议栈,提供了一个完整的OPC UA客户端和服务器实现。您可以在其官方网站(https://github.com/eclipse/milo%EF%BC%89%E4%B8%8A%E6%89%BE%E5%88%B0%E5%85%B6jar%E5%8C%85%E4%BB%A5%E5%8F%8A%E6%96%87%E6%A1%A3%E5%92%8C%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%E3%80%82
Unified Automation SDK:Unified Automation是一个提供OPC UA解决方案的公司,他们提供了C++,.NET,Java等多个语言的SDK。您可以在其官方网站(https://www.unified-automation.com/products/development-tools.html%EF%BC%89%E4%B8%8A%E6%89%BE%E5%88%B0%E5%85%B6%E7%9B%B8%E5%85%B3%E7%9A%84jar%E5%8C%85%E5%92%8C%E6%96%87%E6%A1%A3%E3%80%82
Prosys OPC UA Java SDK:Prosys是一家提供OPC UA解决方案的公司,他们提供了Java SDK,您可以在其官方网站(https://prosysopc.com/products/opc-ua-java-sdk/%EF%BC%89%E4%B8%8A%E6%89%BE%E5%88%B0%E5%85%B6%E7%9B%B8%E5%85%B3%E7%9A%84jar%E5%8C%85%E5%92%8C%E6%96%87%E6%A1%A3%E3%80%82
关于示例代码,您可以在上述资源的文档中找到示例代码。此外,Eclipse Milo和Unified Automation也提供了完整的示例项目,您可以在其GitHub仓库中找到并下载示例代码。