You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cg...@apache.org on 2024/10/18 18:28:06 UTC
(plc4x) 01/01: Fix coil read problem.
This is an automated email from the ASF dual-hosted git repository.
cgarcia pushed a commit to branch fix/modbus
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 0e72fb00b2288983ea1969d6f0d785d30c60e717
Author: César García <ce...@ceos.com.ve>
AuthorDate: Fri Oct 18 14:26:35 2024 -0400
Fix coil read problem.
---
.../modbus/base/optimizer/ModbusOptimizer.java | 58 +++++++++++-----------
1 file changed, 30 insertions(+), 28 deletions(-)
diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/base/optimizer/ModbusOptimizer.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/base/optimizer/ModbusOptimizer.java
index db838c6a0b..76daa18386 100644
--- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/base/optimizer/ModbusOptimizer.java
+++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/base/optimizer/ModbusOptimizer.java
@@ -19,6 +19,8 @@
package org.apache.plc4x.java.modbus.base.optimizer;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.model.PlcTag;
@@ -192,33 +194,7 @@ public class ModbusOptimizer extends SingleTagOptimizer {
}
// Go through all responses till we find one where that contains the current tag's data.
for (Response response : responses.get(tagType)) {
- if(modbusTag instanceof ModbusTagCoil) {
- if(response.matchesCoil(modbusTag)) {
- // If this response was invalid, return all associated addresses as equally invalid.
- // TODO: Possibly it would be worth doing a single item request for each of these
- // tags in order to find out which ones are actually invalid as if one item in the
- // current request exceeds the address range, all items in this chunk will fail, even
- // if only one element was invalid.
- if(response.getResponseCode() != PlcResponseCode.OK) {
- values.put(tagName, new DefaultPlcResponseItem<>(response.getResponseCode(), null));
- break;
- }
-
- // Coils are read completely different from registers.
- ModbusTagCoil coilTag = (ModbusTagCoil) modbusTag;
-
- // Calculate the byte that contains the response for this Coil
- byte[] responseData = response.getResponseData();
- int bitPosition = coilTag.getAddress() - response.startingAddress;
- int bytePosition = bitPosition / 8;
- int bitPositionInByte = bitPosition % 8;
- boolean isBitSet = (responseData[bytePosition] & (1 << bitPositionInByte)) != 0;
- values.put(tagName, new DefaultPlcResponseItem<>(PlcResponseCode.OK, new PlcBOOL(isBitSet)));
- break;
- }
- }
- // Read a normal register.
- else if (response.matchesRegister(modbusTag)) {
+ if (response.matchesRegister(modbusTag)) {
// If this response was invalid, return all associated addresses as equally invalid.
// TODO: Possibly it would be worth doing a single item request for each of these
// tags in order to find out which ones are actually invalid as if one item in the
@@ -407,9 +383,35 @@ public class ModbusOptimizer extends SingleTagOptimizer {
public byte[] getResponseDataForTag(ModbusTag modbusTag) {
byte[] itemData = new byte[modbusTag.getLengthBytes()];
- System.arraycopy(responseData, (modbusTag.getAddress() - startingAddress) * 2, itemData, 0, modbusTag.getLengthBytes());
+ int value = 0;
+ switch(modbusTag.getDataType()) {
+ case BOOL: {
+ itemData = new byte[responseData.length];
+ for (int i= 0; i < responseData.length; i++){
+ itemData[i] = byteReverse(responseData[i]);
+ }
+ }
+ break;
+ default:
+ System.arraycopy(responseData,
+ (modbusTag.getAddress() - startingAddress) * 2,
+ itemData, 0, modbusTag.getLengthBytes());
+ }
+
return itemData;
}
+
+ public static byte byteReverse(byte x) {
+ byte b = 0;
+ for (int i = 0; i < 8; ++i) {
+ b<<=1;
+ b|=( x &1);
+ x>>=1;
+ }
+ return b;
+ }
+
+
}
protected interface TagFactory {