Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLC4x-142 OPC-UA read variables #130

Merged
merged 3 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Bug Fixes
- PLC4X-163 Fixed Netty ByteBuf Leaks for S7 Driver
- PLC4X-158 Added Warning if no Pooled Driver is used for Scraper
- Fixed wrong connection address parameter handling which connects to a wrong address if there is no sub-address to a given IP + PORT combination
- PLC4X-142 OPC-UA read variables -- corrected RegEx

==============================================================
Apache PLC4X 0.5.0
Expand Down
7 changes: 7 additions & 0 deletions plc4j/drivers/opcua/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
<version>0.3.6</version>
</dependency>

<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>server-examples</artifactId>
<version>0.3.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
public class OpcuaField implements PlcField {

public static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>[\\w(.)-=]+))?");
public static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>.+))?");

private final OpcuaIdentifierType identifierType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.opcua.connection.OpcuaTcpPlcConnection;
import org.apache.plc4x.java.opcua.protocol.OpcuaField;
import org.apache.plc4x.java.opcua.protocol.OpcuaPlcFieldHandler;
import org.eclipse.milo.examples.server.ExampleServer;

import java.math.BigInteger;
import java.time.Duration;
Expand All @@ -45,35 +46,40 @@ Licensed to the Apache Software Foundation (ASF) under one
* The current version is tested against a public server, which is to be replaced later by a separate instance of the Milo framework.
* Afterwards the code represented here will be used as an example for the introduction page.
* <p>
* TODO: replace current public server with local Milo instance
*
* Created by Matthias Milan Strljic on 10.05.2019
*/
public class ManualPLC4XOpcua {
private static final String BOOL_IDENTIFIER = "ns=2;i=10844";
private static final String BYTE_STRING_IDENTIFIER = "ns=2;i=10858";
private static final String BYTE_IDENTIFIER = "ns=2;i=10846";
private static final String DOUBLE_IDENTIFIER = "ns=2;i=10854";
private static final String FLOAT_IDENTIFIER = "ns=2;i=10853";
private static final String INT16_IDENTIFIER = "ns=2;i=10847";
private static final String INT32_IDENTIFIER = "ns=2;i=10849";
private static final String INT64_IDENTIFIER = "ns=2;i=10851";
private static final String INTEGER_IDENTIFIER = "ns=2;i=10869";
private static final String SBYTE_IDENTIFIER = "ns=2;i=10845";
private static final String STRING_IDENTIFIER = "ns=2;i=10855";
private static final String UINT16_IDENTIFIER = "ns=2;i=10848";
private static final String UINT32_IDENTIFIER = "ns=2;i=10850";
private static final String UINT64_IDENTIFIER = "ns=2;i=10852";
private static final String UINTEGER_IDENTIFIER = "ns=2;i=10870";
private static final String BOOL_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Boolean";
private static final String BYTE_STRING_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/ByteString";
private static final String BYTE_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Byte";
private static final String DOUBLE_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Double";
private static final String FLOAT_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Float";
private static final String INT16_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Int16";
private static final String INT32_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Int32";
private static final String INT64_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Int64";
private static final String INTEGER_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/Integer";
private static final String SBYTE_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/SByte";
private static final String STRING_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/String";
private static final String UINT16_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/UInt16";
private static final String UINT32_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/UInt32";
private static final String UINT64_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/UInt64";
private static final String UINTEGER_IDENTIFIER = "ns=2;s=HelloWorld/ScalarTypes/UInteger";
private static final String DOES_NOT_EXIST_IDENTIFIER = "ns=2;i=12512623";

public static void main(String args[]) {
try {
ExampleServer testServer = new ExampleServer();
testServer.startup().get();

} catch (Exception e) {
e.printStackTrace();
}
OpcuaTcpPlcConnection opcuaConnection = null;
OpcuaPlcFieldHandler fieldH = new OpcuaPlcFieldHandler();
PlcField field = fieldH.createField("ns=2;i=10855");
PlcField field = fieldH.createField(BOOL_IDENTIFIER);
try {
opcuaConnection = (OpcuaTcpPlcConnection)
new PlcDriverManager().getConnection("opcua:tcp://localhost:4843?discovery=true");
new PlcDriverManager().getConnection("opcua:tcp://127.0.0.1:12686/milo?discovery=false");

} catch (PlcConnectionException e) {
e.printStackTrace();
Expand Down Expand Up @@ -104,19 +110,20 @@ public static void main(String args[]) {

PlcReadRequest request = builder.build();
PlcReadResponse response = opcuaConnection.read(request).get();
Collection coll = response.getAllStrings("String");

//Collection coll = response.getAllStrings("String");

PlcWriteRequest.Builder wBuilder = opcuaConnection.writeRequestBuilder();
wBuilder.addItem("w-Bool", BOOL_IDENTIFIER, true);
wBuilder.addItem("w-ByteString", BYTE_STRING_IDENTIFIER, "TEST".getBytes());
wBuilder.addItem("w-Byte", BYTE_IDENTIFIER, (byte) 1);
//wBuilder.addItem("w-ByteString", BYTE_STRING_IDENTIFIER, "TEST".getBytes());
wBuilder.addItem("w-Byte", BYTE_IDENTIFIER, (byte) 0x00);
wBuilder.addItem("w-Double", DOUBLE_IDENTIFIER, (double) 0.25);
wBuilder.addItem("w-Float", FLOAT_IDENTIFIER, (float) 0.25);
wBuilder.addItem("w-INT16", INT16_IDENTIFIER, (short) 12);
wBuilder.addItem("w-Int32", INT32_IDENTIFIER, (int) 314);
wBuilder.addItem("w-Int64", INT64_IDENTIFIER, (long) 123125);
wBuilder.addItem("w-Integer", INTEGER_IDENTIFIER, (int) 314);
wBuilder.addItem("w-SByte", SBYTE_IDENTIFIER, (short) 23);
wBuilder.addItem("w-SByte", SBYTE_IDENTIFIER, (byte) 23);
wBuilder.addItem("w-String", STRING_IDENTIFIER, "TEST");
wBuilder.addItem("w-UInt16", UINT16_IDENTIFIER, (int) 222);
wBuilder.addItem("w-UInt32", UINT32_IDENTIFIER, (long) 21412);
Expand Down Expand Up @@ -191,5 +198,3 @@ private long GetConnectionTime() {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ public void after() {
@Test
public void testOpcuaAddressPattern() {

//standard integer based param
assertMatching(ADDRESS_PATTERN, "ns=2;i=10846");
//string based address values
assertMatching(ADDRESS_PATTERN, "ns=2;s=test.variable.name.inspect");
assertMatching(ADDRESS_PATTERN, "ns=2;s=key param with some spaces");
assertMatching(ADDRESS_PATTERN, "ns=2;s=\"aweired\".\"siemens\".\"param\".\"submodule\".\"param");
assertMatching(ADDRESS_PATTERN, "ns=2;s=Weee314Waannaaa\\somenice=ext=a234a*#+1455!§$%&/()tttraaaaSymbols-.,,");
// GUID address tests
assertMatching(ADDRESS_PATTERN, "ns=2;g=09087e75-8e5e-499b-954f-f2a8624db28a");
// binary encoded addresses
assertMatching(ADDRESS_PATTERN, "ns=2;b=asvaewavarahreb==");

}
Expand Down