From 3260c0795c15b31c6a1dc037274a2ab5213fb1e2 Mon Sep 17 00:00:00 2001 From: fdupont-epsilia <157042240+fdupont-epsilia@users.noreply.github.com> Date: Wed, 4 Dec 2024 06:08:01 -0500 Subject: [PATCH] feat(plc4j/ads): Add STRING and WSTRING to getDataTypeTableEntry. (#1902) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add STRING and WSTRING to getDataTypeTableEntry. serializePlcValue, remove check directly in dataTypeTable, only use getDataTypeTableEntry. * fix(plc4j/ads): Remove obsolete comment #1902 --------- Co-authored-by: François Dupont --- .../java/ads/protocol/AdsProtocolLogic.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java index 114a46c8ff..9d8e59afe1 100644 --- a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java +++ b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java @@ -1198,11 +1198,6 @@ protected CompletableFuture multiWrite(PlcWriteRequest writeRe } protected byte[] serializePlcValue(PlcValue plcValue, String datatypeName) throws SerializationException { - // First check, if we have type information available. - if (!dataTypeTable.containsKey(datatypeName)) { - throw new SerializationException("Could not find data type: " + datatypeName); - } - // Get the data type, allocate enough memory and serialize the value based on the // structure defined by the data type. Optional dataTypeTableEntryOptional = getDataTypeTableEntry(datatypeName); @@ -1953,13 +1948,28 @@ protected Optional getDataTypeTableEntry(String name) { return Optional.of(dataTypeTable.get(name)); } try { - AdsDataType adsDataType = AdsDataType.valueOf(name); + AdsDataType adsDataType; + int numBytes; + + if (name.startsWith("STRING(")) { + adsDataType = AdsDataType.valueOf("CHAR"); + numBytes = Integer.parseInt(name.substring(7, name.length() - 1)) + 1; + } + else if (name.startsWith("WSTRING(")) { + adsDataType = AdsDataType.valueOf("WCHAR"); + numBytes = Integer.parseInt(name.substring(8, name.length() - 1)) * 2 + 2; + } + else { + adsDataType = AdsDataType.valueOf(name); + numBytes = adsDataType.getNumBytes(); + } + // !It seems that the dataType value differs from system to system, // !However, we never really seem to use that value, so I would say it doesn't matter. return Optional.of(new AdsDataTypeTableEntry( - 128, 1, 0, 0, adsDataType.getNumBytes(), 0, + 128, 1, 0, 0, numBytes, 0, adsDataType.getValue(), 0, 0, 0, - adsDataType.name(), "", "", + name, "", "", Collections.emptyList(), Collections.emptyList(), new byte[0])); } catch (IllegalArgumentException e) { return Optional.empty();