From eac8bc75a83b210550509a022c2bfdaa3ff19cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Dupont?= Date: Tue, 19 Nov 2024 14:33:44 -0500 Subject: [PATCH] Add STRING and WSTRING to getDataTypeTableEntry. serializePlcValue, remove check directly in dataTypeTable, only use getDataTypeTableEntry. --- .../java/ads/protocol/AdsProtocolLogic.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 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 114a46c8ff1..1fe443279d7 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 @@ -1199,10 +1199,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 +1949,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();