Skip to content

Commit

Permalink
Added station/address parameter to AB-ETH driver
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFeinauer committed Dec 2, 2019
1 parent 9d141e1 commit 7d73e38
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 19 deletions.
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 AbEthDriver implements PlcDriver {

private static final Pattern ABETH_URI_PATTERN = Pattern.compile("^ab-eth://(?<host>.*)(?<params>\\?.*)?");
private static final Pattern ABETH_URI_PATTERN = Pattern.compile("^ab-eth://(?<host>.*)/(?<station>\\d{1,2})(?<params>\\?.*)?");

@Override
public String getProtocolCode() {
Expand All @@ -48,15 +48,15 @@ public PlcConnection connect(String url) throws PlcConnectionException {
Matcher matcher = ABETH_URI_PATTERN.matcher(url);
if (!matcher.matches()) {
throw new PlcConnectionException(
"Connection url doesn't match the format 'ab-eth://{host|ip}'");
"Connection url doesn't match the format 'ab-eth://{host|ip}/{station}'");
}
int station = Integer.parseInt(matcher.group("station"));
String host = matcher.group("host");

String params = matcher.group("params") != null ? matcher.group("params").substring(1) : null;

try {
InetAddress serverInetAddress = InetAddress.getByName(host);
return new AbEthPlcConnection(serverInetAddress, params);
return new AbEthPlcConnection(serverInetAddress, station, params);
} catch (UnknownHostException e) {
throw new PlcConnectionException("Error parsing address", e);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.base.connection.ChannelFactory;
import org.apache.plc4x.java.base.connection.NettyPlcConnection;
import org.apache.plc4x.java.tcp.connection.TcpSocketChannelFactory;
import org.apache.plc4x.java.base.events.ConnectEvent;
import org.apache.plc4x.java.base.events.ConnectedEvent;
import org.apache.plc4x.java.base.messages.*;
Expand All @@ -46,13 +45,17 @@ public class AbEthPlcConnection extends NettyPlcConnection implements PlcReader
private static final int AB_ETH_PORT = 2222;
private static final Logger logger = LoggerFactory.getLogger(AbEthPlcConnection.class);

public AbEthPlcConnection(InetAddress address, String params) {
this(new TcpSocketChannelFactory(address, AB_ETH_PORT), params);
private final int station;


public AbEthPlcConnection(InetAddress address, int station, String params) {
this(new TcpSocketChannelFactory(address, AB_ETH_PORT), station, params);
logger.info("Setting up AB-ETH Connection with: host-name {}", address.getHostAddress());
}

public AbEthPlcConnection(ChannelFactory channelFactory, String params) {
public AbEthPlcConnection(ChannelFactory channelFactory, int station, String params) {
super(channelFactory, true);
this.station = station;

if (!StringUtils.isEmpty(params)) {
for (String param : params.split("&")) {
Expand Down Expand Up @@ -101,7 +104,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
}
});
pipeline.addLast(new AbEthProtocol());
pipeline.addLast(new Plc4xAbEthProtocol());
pipeline.addLast(new Plc4xAbEthProtocol(station));
}
};
}
Expand Down Expand Up @@ -153,5 +156,4 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
return future
.thenApply(PlcReadResponse.class::cast);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,22 @@ public static AbEthField of(String fieldString) {
short elementNumber = Short.parseShort(matcher.group(ELEMENT_NUMBER));
short bitNumber = (matcher.group(BIT_NUMBER) != null) ? Short.parseShort(matcher.group(BIT_NUMBER)) : 0; //Short.parseShort(matcher.group(BIT_NUMBER));
FileType fileType = FileType.valueOf(matcher.group(DATA_TYPE).toUpperCase());

short byteSize;
switch (fileType) {
case WORD:
case SINGLEBIT:
byteSize = 2;
break;
case DWORD:
byteSize = 4;
break;
case SINGLEBIT:
byteSize = 2;
break;
default:
byteSize = Short.parseShort(matcher.group(SIZE));
}
return new AbEthField(byteSize, fileNumber, fileType, elementNumber, bitNumber);
}
throw new PlcInvalidFieldException("Unable to parse address: " + fieldString);
throw new PlcInvalidFieldException("Unable to parse field address: " + fieldString);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public class Plc4xAbEthProtocol extends PlcMessageToMessageCodec<CIPEncapsulatio

private long sessionHandle;
private Map<Integer, PlcRequestContainer> requests;
private int station;

public Plc4xAbEthProtocol() {
public Plc4xAbEthProtocol(int station) {
logger.trace("Created new instance of PLC4X-AB-ETH Protocol");
this.requests = new HashMap<>();
this.station = station;
}

@Override
Expand Down Expand Up @@ -103,9 +105,9 @@ protected void encode(ChannelHandlerContext ctx, PlcRequestContainer msg, List<O
DF1RequestProtectedTypedLogicalRead logicalRead = new DF1RequestProtectedTypedLogicalRead(
abEthField.getByteSize(), abEthField.getFileNumber(), abEthField.getFileType().getTypeCode(),
abEthField.getElementNumber(), (short) 0); // Subelementnumber default to zero
// TODO: make target and origin address changeable
// origin/sender: constant = 5
DF1RequestMessage requestMessage = new DF1CommandRequestMessage(
(short) 8, (short) 5, (short) 0, transactionCounterGenerator.incrementAndGet(), logicalRead);
(short) station, (short) 5, (short) 0, transactionCounterGenerator.incrementAndGet(), logicalRead);
CIPEncapsulationReadRequest read = new CIPEncapsulationReadRequest(
sessionHandle, 0, emptySenderContext, 0, requestMessage);

Expand Down
7 changes: 7 additions & 0 deletions plc4j/examples/hello-world-plc4x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
</dependency>

<!-- Required driver implementation -->
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-driver-ab-eth</artifactId>
<version>0.6.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-driver-ads</artifactId>
Expand Down Expand Up @@ -107,6 +113,7 @@
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<usedDependencies combine.children="append">
<usedDependency>org.apache.plc4x:plc4j-driver-ab-eth</usedDependency>
<usedDependency>org.apache.plc4x:plc4j-driver-ads</usedDependency>
<usedDependency>org.apache.plc4x:plc4j-driver-ethernet-ip</usedDependency>
<usedDependency>org.apache.plc4x:plc4j-driver-modbus</usedDependency>
Expand Down
2 changes: 1 addition & 1 deletion src/site/asciidoc/protocols/ab-eth/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ supports the protected typed logical read with a limited number of data types.

=== Connection

The connection string looks as follows: `ab-eth://<ip-address>`
The connection string looks as follows: `ab-eth://<ip-address>/<station>`

The field address: `N<file>:<offset></bitnumber>:<datatype>[<numberofbytes>]`. The following data types are available
at the moment: SINBLEBIT (requires bitnumber to be set), WORD (2 byte integer), DWORD (4 byte integer), INTEGER (returns
Expand Down
8 changes: 8 additions & 0 deletions src/site/asciidoc/protocols/features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ The following table contains a list of operations and the protocols that support
|===
|Protocol |Read Single Address Value |Read Multiple Address Values |Write Single Address Value |Write Multiple Address Value|Subscribe to Value changes |Subscribe to PLC Events/Alarms

|AB-Ethernet
|icon:check[role="green"]
|icon:check[role="red"]
|icon:check[role="red"]
|icon:check[role="red"]
|icon:check[role="red"]
|icon:question[role="red"]

|ADS
|icon:check[role="green"]
|icon:check[role="green"]
Expand Down
1 change: 1 addition & 0 deletions src/site/site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
</menu>
<menu name="Protocols">
<item name="Features" href="protocols/features.html"/>
<item name="AB-Ethernet" href="protocols/ab-eth/index.html"/>
<item name="ADS" href="protocols/ads/index.html"/>
<item name="DeltaV" href="protocols/delta-v/index.html"/>
<item name="EtherNet/IP" href="protocols/ethernet-ip/index.html"/>
Expand Down

0 comments on commit 7d73e38

Please sign in to comment.