Java 如何连接到plc并获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19796184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to plc and get data
提问by keysuke
i need to embed a OPC server to a eclipse RCP application but i don't know where to begin. I'm using the opcua4j as example to create the server, but can't figure out how to connect to the remote PLC device.
我需要将 OPC 服务器嵌入到 Eclipse RCP 应用程序中,但我不知道从哪里开始。我使用 opcua4j 作为示例来创建服务器,但无法弄清楚如何连接到远程 PLC 设备。
My question is how to communicate with a remote client from server using OPC, given the client's ip address?
我的问题是如何使用 OPC 从服务器与远程客户端通信,给定客户端的 IP 地址?
Ps: What is the best tutorial/book about opc ua java programming?
Ps:关于 opc ua java 编程的最佳教程/书籍是什么?
回答by James
This may give you a run down on OPC servers work, I'm not too sure how much you know, so sorry if I relay. A OPC Server polls rather than a request/reply style of communication. It polls for tags rather than devices.
这可能会让您对 OPC 服务器的工作感到厌烦,我不太确定您知道多少,所以很抱歉如果我转达。OPC 服务器轮询而不是请求/回复通信方式。它轮询标签而不是设备。
Check this link for Java - OPC Deve - http://www.opcconnect.com/java.php#javacom
检查此链接以获取 Java - OPC Deve - http://www.opcconnect.com/java.php#javacom
OPC 101
OPC 101
You will need to develop:
您将需要开发:
- Channel
- Device
- Variables
- 渠道
- 设备
- 变量
Each channel specifies two things. The two aspects being specified are the : 1. Physical medium (Network adapter such as your standard Ethernet device or a Serial port) 2. Protocol being used (MODBUS, Allen Bradley, GE-Fanuc etc). This means each channel cannot host devices on either different mediums or protocols.
每个通道指定两件事。指定的两个方面是: 1. 物理介质(网络适配器,例如标准以太网设备或串行端口) 2. 使用的协议(MODBUS、Allen Bradley、GE-Fanuc 等)。这意味着每个通道都不能在不同的介质或协议上托管设备。
The Device This field specifies a few things. These being:
设备 此字段指定了一些内容。这些是:
- Address. This can be an IP address (for Ethernet passed protocols – such as IDEC Serial Encapsulated Protocol), or an identification number for serial communications such as Profibus or Modbus ASCII/RTU.
- Driver Specific information - This may include specifying a CPU model, or I/O card being used. This varies among drivers.
- 地址。这可以是 IP 地址(对于以太网通过的协议——例如 IDEC 串行封装协议),或者是串行通信的标识号,例如 Profibus 或 Modbus ASCII/RTU。
- 驱动程序特定信息 - 这可能包括指定 CPU 型号或正在使用的 I/O 卡。这因司机而异。
The Variables are where the “Tags” are defined.
变量是定义“标签”的地方。
- Tag Name
- PLC Address. Such as M1 or RW1 or 49075 (Driver specific though)
- Data Type – Boolean, Word etc.
- 标签名称
- PLC 地址。例如 M1 或 RW1 或 49075(虽然特定于驱动程序)
- 数据类型——布尔型、字型等。
Each tag or process variable has both data and meta-data. This can includes variable, last updated, the health of the tag etc, engineering units.
每个标签或流程变量都有数据和元数据。这可以包括变量、上次更新、标签的健康状况等,工程单位。
Finally
最后
I haven't actually programmed my own OPC Server, but the above might help. I am not sure how much that Java SDK above gives, but consider security in your application.
我实际上还没有对自己的 OPC 服务器进行编程,但以上内容可能会有所帮助。我不确定上面的 Java SDK 提供了多少,但请考虑应用程序的安全性。
Is it possible to run an OPC server, polling the data, then your application interfaces with the OPC Server? This could help with not having to mess (too much, at least) with OPC. You can get Java based OPC servers than run on any platform and I'm confident most have an API, or at least a way to communicate the data.
是否可以运行 OPC 服务器,轮询数据,然后您的应用程序与 OPC 服务器接口?这可能有助于不必弄乱(至少太多)与 OPC。您可以获得基于 Java 的 OPC 服务器,而不是在任何平台上运行,我相信大多数都有 API,或者至少有一种通信数据的方式。
Edit
编辑
Check out the software package Pi (http://www.osisoft.com/). That might help with this.
查看软件包 Pi ( http://www.osisoft.com/)。这可能有助于解决这个问题。
回答by vonGohren
You may try to use a evaluation verison of the ProSys OPC Java SDK. This is a pretty simple SDK and it provides you with a tutorial on how to create a server and a client from scratch. Following is a code on how simple it is to create a server with no addresse space in it:
您可以尝试使用ProSys OPC Java SDK的评估版。这是一个非常简单的 SDK,它为您提供了一个关于如何从头开始创建服务器和客户端的教程。以下是关于创建一个没有地址空间的服务器是多么简单的代码:
private String APPNAME = "yourappname";
private String SERVER = "nameoftheserver";
private int PORT = 52000; #choose whatever
public void CreateOPCUAserver() {
server = new UaServer();
final PkiFileBasedCertificateValidator validator = new PkiFileBasedCertificateValidator();
server.setCertificateValidator(validator);
validator.setValidationListener(validationListener);
ApplicationDescription appDescription = new ApplicationDescription();
appDescription.setApplicationName(new LocalizedText(APPNAME, Locale.ENGLISH));
appDescription.setApplicationUri("urn:localhost:UA:"+SERVER);
appDescription.setProductUri("urn:snorre.com:UA:"+SERVER);
appDescription.setApplicationType(ApplicationType.Server);
try {
server.setPort(PORT);
server.setUseLocalhost(true);
server.setUseAllIpAddresses(true);
server.setServerName("OPCUA/"+APPNAME);
final ApplicationIdentity identity = ApplicationIdentity.loadOrCreateCertificate(appDescription, "NTNU",
/* Private Key Password */"opcua",
/* Key File Path */new File(validator.getBaseDir(), "private"),
/* Enable renewing the certificate */true,
/* Additional host names for the certificate */server.getHostNames());
server.setApplicationIdentity(identity);
server.setSecurityModes(SecurityMode.ALL);
server.addUserTokenPolicy(UserTokenPolicy.ANONYMOUS);
server.addUserTokenPolicy(UserTokenPolicy.SECURE_USERNAME_PASSWORD);
server.addUserTokenPolicy(UserTokenPolicy.SECURE_CERTIFICATE);
server.init();
String endpoint = String.format("opc.tcp://%s:%s/OPCUA/%s","localhost", PORT, APPNAME);
server.addEndpoint(endpoint, SecurityMode.NONE, UserTokenPolicy.ANONYMOUS);
server.start();
logger.info("*********important parameters for event transmission**********");
logger.info("Servername "+endpoint);
logger.info("Publishing interval: " + server.getSubscriptionManager().getMaxPublishingInterval());
logger.info("Retransmition queue size: " + server.getSubscriptionManager().getMaxRetransmissionQueueSize());
logger.info("Session timeout: " + server.getSessionManager().getMaxSessionTimeout());
logger.info(logger.getClass().getCanonicalName());
logger.info("****************************end ********************");
} catch (UaServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecureIdentityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You also have to create a validation listener which is pretty basic. Just to get it to run
您还必须创建一个非常基本的验证侦听器。只是为了让它运行
public class MyCertificateValidationListener implements CertificateValidationListener {
@Override
public ValidationResult onValidate(Cert certificate, ApplicationDescription applicationDescription, EnumSet<CertificateCheck> passedChecks) {
// Do not mind about URI...
if (passedChecks.containsAll(EnumSet.of(CertificateCheck.Trusted, CertificateCheck.Validity, CertificateCheck.Signature))) {
if (!passedChecks.contains(CertificateCheck.Uri)) {
try {
System.out.println("Client's ApplicationURI (" + applicationDescription.getApplicationUri() + ") does not match the one in certificate: " + PkiFileBasedCertificateValidator.getApplicationUriOfCertificate(certificate));
} catch (CertificateParsingException e) {
throw new RuntimeException(e);
}
}
return ValidationResult.AcceptPermanently;
}
return ValidationResult.Reject;
}
}
This does not have any authentication, but it helps you get on the way. Now you can use some free client program to connect to your server and browse. You will see that ther are some folders there, but that is because the server comes with those folders intially.
这没有任何身份验证,但它可以帮助您继续前进。现在您可以使用一些免费的客户端程序连接到您的服务器并进行浏览。您会看到那里有一些文件夹,但那是因为服务器最初附带了这些文件夹。
Now the server is up and running, you have to create an AddresseSpace, then a data model of the PLC to add to your current server. Then this data model has a connection to your PLC and its possible methods, giving OPC-UA clients to connect to your server basend on a simple uri like this "opc.tcp://localhost:52000/OPCUA/servername" or http. This is you run it locally. Else it has to be an IP on this.
现在服务器已启动并运行,您必须创建一个 AddresseSpace,然后将 PLC 的数据模型添加到您当前的服务器中。然后这个数据模型连接到您的 PLC 及其可能的方法,让 OPC-UA 客户端基于一个简单的 uri 连接到您的服务器,例如“opc.tcp://localhost:52000/OPCUA/servername”或 http。这是你在本地运行它。否则它必须是一个IP。
When clients then browse this they can see the variables, methods events alarms and so on that you have created within your datamodel. And the can use the methods and alot of different cool stuff.
当客户浏览它时,他们可以看到您在数据模型中创建的变量、方法事件警报等。并且可以使用方法和很多不同的很酷的东西。