Skip to content
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apache-commons-compress = "1.28.0"
apache-commons-io = "2.20.0"
apache-commons-lang = "3.19.0"
apache-plc4x = "0.11.0"
apache-plc4x = "0.13.1"
assertj = "3.27.6"
awaitility = "4.3.0"
bouncycastle = "1.80"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum DATA_TYPE {
LTIME_OF_DAY((short) 0x56, LocalTime.class),
DATE_AND_TIME((short) 0x57, LocalDateTime.class),
LDATE_AND_TIME((short) 0x58, LocalDateTime.class),
DATE_AND_LTIME((short) 0x1F, LocalDateTime.class),
RAW_BYTE_ARRAY((short) 0x71, Byte.class);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public void start(
tempConnection.startConnection(input.moduleServices().eventService(), adapterId, getProtocolAdapterInformation().getProtocolId());
protocolAdapterState.setConnectionStatus(CONNECTED);
} catch (final Plc4xException e) {
try {
tempConnection.disconnect();
} catch (final Exception ex) {
log.debug("Tried disconnecting after connection error and caught exception", ex);
}
this.connection = null;
log.error("Plc4x connection failed to start", e);
protocolAdapterState.setConnectionStatus(ERROR);
}
Expand Down Expand Up @@ -276,6 +282,12 @@ public void stop(final @NotNull ProtocolAdapterStopInput input, final @NotNull P
protocolAdapterState.setConnectionStatus(ProtocolAdapterState.ConnectionStatus.CONNECTED);
}
return processPlcFieldData(Plc4xDataUtils.readDataFromReadResponse(event));
} else {
tags.stream()
.filter(tag -> event.getResponseCode(tag.getName()) != PlcResponseCode.OK)
.forEach(tag -> log.error("Unable to read tag {}. Error Code: {}",
tag.getName(),
event.getResponseCode(tag.getName())));;
}
}
return new Plc4xDataSample(adapterFactories.dataPointFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ void startConnection(final @NotNull EventService eventService, final @NotNull St
try {
plcConnection = CompletableFuture.supplyAsync(() -> {
try {
//This is not working in all cases. An exception is thrown if PLC4X actually catches
//the connection probllem. In many cases this call will simply get stuck. Afterwards
// This is not working in all cases. An exception is thrown if PLC4X actually catches
// the connection problem. In many cases this call will simply get stuck. Afterwards
// a new connection CANNOT be opened.
return Optional.of(plcDriverManager.getConnectionManager()
.getConnection(connectionString));
Expand Down Expand Up @@ -141,7 +141,8 @@ protected void lazyConnectionCheck() {
public void disconnect() throws Exception {
synchronized (lock) {
try {
if (plcConnection != null && plcConnection.isConnected()) {
final var plcConnection = this.plcConnection;
if (plcConnection != null) {
plcConnection.close();
}
} finally {
Expand All @@ -156,7 +157,7 @@ public boolean isConnected() {

public @NotNull CompletableFuture<? extends PlcReadResponse> read(final @NotNull List<Plc4xTag> tags) {
lazyConnectionCheck();
if (!plcConnection.getMetadata().canRead()) {
if (!plcConnection.getMetadata().isReadSupported()) {
return CompletableFuture.failedFuture(new Plc4xException("connection type read-blocking"));
}
if (log.isTraceEnabled()) {
Expand All @@ -176,7 +177,7 @@ public boolean isConnected() {
final @NotNull Plc4xTag tag,
final @NotNull Consumer<PlcSubscriptionEvent> consumer) {
lazyConnectionCheck();
if (!plcConnection.getMetadata().canSubscribe()) {
if (!plcConnection.getMetadata().isSubscribeSupported()) {
return CompletableFuture.failedFuture(new Plc4xException("connection type cannot subscribe"));
}
if (log.isTraceEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.plc4x.java.api.types.PlcValueType;
import org.apache.plc4x.java.api.value.PlcValue;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
Expand All @@ -45,6 +47,7 @@
* Some data utilies to manage the interaction with the PLC API
*/
public class Plc4xDataUtils {
private static final Logger log = LoggerFactory.getLogger(Plc4xDataUtils.class);
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private static final String AMP = "&";
private static final String EQUALS = "=";
Expand Down Expand Up @@ -258,6 +261,7 @@ public static Object convertObject(final PlcValue value) {

case DATE_AND_TIME: //64bit signed, milliseconds since 1990-1-1 (1990-01-01-0:0:0 - 2089-12-31-23:59:59.999)
case LDATE_AND_TIME: //64bit signed, nanoseconds since 1970-1-1 (1970-01-01-0:0:0.000000000 - 2262-04-11-23:47:16.854775807)
case DATE_AND_LTIME: //64bit signed, nanoseconds since 1970-1-1 (1970-01-01-0:0:0.000000000 - 2262-04-11-23:47:16.854775807)
return DATE_TIME_FORMATTER.format(value.getDateTime());

case RAW_BYTE_ARRAY:
Expand All @@ -267,6 +271,7 @@ public static Object convertObject(final PlcValue value) {
case List: //ArrayList
case NULL:
default:
log.error("Unable to convert PLC4X value of type {} and value {}", type, value.getDateTime());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
*/
public class ADSProtocolAdapter extends AbstractPlc4xAdapter<ADSSpecificAdapterConfig, Plc4xToMqttMapping> {

private static final @NotNull String SOURCE_AMS_NET_ID = "sourceAmsNetId";
private static final @NotNull String SOURCE_AMS_PORT = "sourceAmsPort";
private static final @NotNull String TARGET_AMS_PORT = "targetAmsPort";
private static final @NotNull String TARGET_AMS_NET_ID = "targetAmsNetId";
private static final @NotNull String SOURCE_AMS_NET_ID = "source-ams-net-id";
private static final @NotNull String SOURCE_AMS_PORT = "source-ams-port";
private static final @NotNull String TARGET_AMS_PORT = "target-ams-port";
private static final @NotNull String TARGET_AMS_NET_ID = "target-ams-net-id";

public ADSProtocolAdapter(
final @NotNull ProtocolAdapterInformation adapterInformation,
Expand Down
Loading