You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/07/14 15:31:44 UTC
[plc4x] branch develop updated (594ed26 -> f716fc8)
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git.
from 594ed26 Added an Apache header to a new file
new 3ed3bdb PLC4X-215 - Drivers using the BaseOptimizer (SingleFieldOptimizer) don't handle error responses gracefully - Fixed the issue by only accessing the PlcValue if the return code is 'OK'
new f716fc8 PLC4X-213 - [Modbus] The Modbus driver doesn't handle error responses gracefully
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
RELEASE_NOTES | 5 ++
.../plc4x/java/api/types/PlcResponseCode.java | 4 ++
.../java/modbus/protocol/ModbusProtocolLogic.java | 56 ++++++++++++++++------
.../plc4x/java/spi/generation/ReadBuffer.java | 7 ++-
.../plc4x/java/spi/optimizer/BaseOptimizer.java | 7 +--
5 files changed, 58 insertions(+), 21 deletions(-)
[plc4x] 01/02: PLC4X-215 - Drivers using the BaseOptimizer
(SingleFieldOptimizer) don't handle error responses gracefully - Fixed the
issue by only accessing the PlcValue if the return code is 'OK'
Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 3ed3bdb674a7d0ffe71e554d031f31658ee812f3
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Jul 14 17:22:50 2020 +0200
PLC4X-215 - Drivers using the BaseOptimizer (SingleFieldOptimizer) don't handle error responses gracefully
- Fixed the issue by only accessing the PlcValue if the return code is 'OK'
---
.../java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java
index c031710..0933ec5 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/optimizer/BaseOptimizer.java
@@ -49,9 +49,10 @@ public abstract class BaseOptimizer {
for (String fieldName : curRequest.getFieldNames()) {
if (readResponse.isLeft()) {
PlcReadResponse subReadResponse = (PlcReadResponse) readResponse.getLeft();
- fields.put(fieldName,
- new ResponseItem<>(subReadResponse.getResponseCode(fieldName),
- subReadResponse.getAsPlcValue().getValue(fieldName)));
+ PlcResponseCode responseCode = subReadResponse.getResponseCode(fieldName);
+ PlcValue value = (responseCode == PlcResponseCode.OK) ?
+ subReadResponse.getAsPlcValue().getValue(fieldName) : null;
+ fields.put(fieldName, new ResponseItem<>(responseCode, value));
} else {
fields.put(fieldName, new ResponseItem<>(PlcResponseCode.INTERNAL_ERROR, null));
}
[plc4x] 02/02: PLC4X-213 - [Modbus] The Modbus driver doesn't
handle error responses gracefully
Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit f716fc87fdf1a1bad39e42095beb6034efd28c8f
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Jul 14 17:31:37 2020 +0200
PLC4X-213 - [Modbus] The Modbus driver doesn't handle error responses gracefully
- Implemented code for handling ModbusPDUError responses for requests
- Added some PlcResponseCode values which make sense in general
---
RELEASE_NOTES | 5 ++
.../plc4x/java/api/types/PlcResponseCode.java | 4 ++
.../java/modbus/protocol/ModbusProtocolLogic.java | 56 ++++++++++++++++------
.../plc4x/java/spi/generation/ReadBuffer.java | 7 ++-
4 files changed, 54 insertions(+), 18 deletions(-)
diff --git a/RELEASE_NOTES b/RELEASE_NOTES
index d805eb3..d39fcfc 100644
--- a/RELEASE_NOTES
+++ b/RELEASE_NOTES
@@ -28,6 +28,11 @@ PLC4X-211 PlcValues seem to always return "true" on the
isXYZ" checks.
PLC4X-212 When writing multiple values in one request the
item status is not correctly set
+PLC4X-215 Drivers using the BaseOptimizer
+ (SingleFieldOptimizer) don't handle error responses
+ gracefully
+PLC4X-213 [Modbus] The Modbus driver doesn't handle error
+ responses gracefully
==============================================================
diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/types/PlcResponseCode.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/types/PlcResponseCode.java
index d1b593d..e05e098 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/types/PlcResponseCode.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/types/PlcResponseCode.java
@@ -25,7 +25,11 @@ public enum PlcResponseCode {
ACCESS_DENIED,
INVALID_ADDRESS,
INVALID_DATATYPE,
+ INVALID_DATA,
INTERNAL_ERROR,
+ REMOTE_BUSY,
+ REMOTE_ERROR,
+ UNSUPPORTED,
RESPONSE_PENDING
}
diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
index 6ebe88e..4920a05 100644
--- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
+++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/protocol/ModbusProtocolLogic.java
@@ -19,10 +19,7 @@ under the License.
package org.apache.plc4x.java.modbus.protocol;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
-import org.apache.plc4x.java.api.messages.PlcReadRequest;
-import org.apache.plc4x.java.api.messages.PlcReadResponse;
-import org.apache.plc4x.java.api.messages.PlcWriteRequest;
-import org.apache.plc4x.java.api.messages.PlcWriteResponse;
+import org.apache.plc4x.java.api.messages.*;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.api.value.PlcBoolean;
@@ -108,12 +105,39 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
// Try to decode the response data based on the corresponding request.
PlcValue plcValue = null;
PlcResponseCode responseCode;
- try {
- plcValue = toPlcValue(requestPdu, responsePdu);
- responseCode = PlcResponseCode.OK;
- } catch (ParseException e) {
- // Add an error response code ...
- responseCode = PlcResponseCode.INTERNAL_ERROR;
+ // Check if the response was an error response.
+ if (responsePdu instanceof ModbusPDUError) {
+ ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
+ switch (errorResponse.getExceptionCode()) {
+ case 1:
+ // This implies the received function code is not supported.
+ responseCode = PlcResponseCode.UNSUPPORTED;
+ break;
+ case 2:
+ responseCode = PlcResponseCode.INVALID_ADDRESS;
+ break;
+ case 3:
+ responseCode = PlcResponseCode.INVALID_DATA;
+ break;
+ case 4:
+ responseCode = PlcResponseCode.REMOTE_ERROR;
+ break;
+ case 6:
+ responseCode = PlcResponseCode.REMOTE_BUSY;
+ break;
+ default:
+ // This generally implies that something wen't wrong which we didn't anticipate.
+ responseCode = PlcResponseCode.INTERNAL_ERROR;
+ break;
+ }
+ } else {
+ try {
+ plcValue = toPlcValue(requestPdu, responsePdu);
+ responseCode = PlcResponseCode.OK;
+ } catch (ParseException e) {
+ // Add an error response code ...
+ responseCode = PlcResponseCode.INTERNAL_ERROR;
+ }
}
// Prepare the response.
@@ -215,21 +239,24 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
private PlcValue toPlcValue(ModbusPDU request, ModbusPDU response) throws ParseException {
if (request instanceof ModbusPDUReadDiscreteInputsRequest) {
if (!(response instanceof ModbusPDUReadDiscreteInputsResponse)) {
- throw new PlcRuntimeException("Unexpected response type ModbusPDUReadDiscreteInputsResponse");
+ throw new PlcRuntimeException("Unexpected response type. " +
+ "Expected ModbusPDUReadDiscreteInputsResponse, but got " + response.getClass().getName());
}
ModbusPDUReadDiscreteInputsRequest req = (ModbusPDUReadDiscreteInputsRequest) request;
ModbusPDUReadDiscreteInputsResponse resp = (ModbusPDUReadDiscreteInputsResponse) response;
return readBooleanList(req.getQuantity(), resp.getValue());
} else if (request instanceof ModbusPDUReadCoilsRequest) {
if (!(response instanceof ModbusPDUReadCoilsResponse)) {
- throw new PlcRuntimeException("Unexpected response type ModbusPDUReadCoilsResponse");
+ throw new PlcRuntimeException("Unexpected response type. " +
+ "Expected ModbusPDUReadCoilsResponse, but got " + response.getClass().getName());
}
ModbusPDUReadCoilsRequest req = (ModbusPDUReadCoilsRequest) request;
ModbusPDUReadCoilsResponse resp = (ModbusPDUReadCoilsResponse) response;
return readBooleanList(req.getQuantity(), resp.getValue());
} else if (request instanceof ModbusPDUReadInputRegistersRequest) {
if (!(response instanceof ModbusPDUReadInputRegistersResponse)) {
- throw new PlcRuntimeException("Unexpected response type ModbusPDUReadInputRegistersResponse");
+ throw new PlcRuntimeException("Unexpected response type. " +
+ "Expected ModbusPDUReadInputRegistersResponse, but got " + response.getClass().getName());
}
ModbusPDUReadInputRegistersRequest req = (ModbusPDUReadInputRegistersRequest) request;
ModbusPDUReadInputRegistersResponse resp = (ModbusPDUReadInputRegistersResponse) response;
@@ -237,7 +264,8 @@ public class ModbusProtocolLogic extends Plc4xProtocolBase<ModbusTcpADU> impleme
return DataItemIO.staticParse(io, (short) 2, (short) req.getQuantity());
} else if (request instanceof ModbusPDUReadHoldingRegistersRequest) {
if (!(response instanceof ModbusPDUReadHoldingRegistersResponse)) {
- throw new PlcRuntimeException("Unexpected response type ModbusPDUReadHoldingRegistersResponse");
+ throw new PlcRuntimeException("Unexpected response type. " +
+ "Expected ModbusPDUReadHoldingRegistersResponse, but got " + response.getClass().getName());
}
ModbusPDUReadHoldingRegistersRequest req = (ModbusPDUReadHoldingRegistersRequest) request;
ModbusPDUReadHoldingRegistersResponse resp = (ModbusPDUReadHoldingRegistersResponse) response;
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
index a9087f9..3bce4ce 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/ReadBuffer.java
@@ -272,12 +272,11 @@ public class ReadBuffer {
}
public String readString(int bitLength, String encoding) {
- byte[] strBytes = new byte[bitLength/8];
- for(int i= 0; i<bitLength/8 && hasMore(8); i++){
+ byte[] strBytes = new byte[bitLength / 8];
+ for (int i = 0; (i < (bitLength / 8)) && hasMore(8); i++) {
try {
strBytes[i] = readByte(8);
- }
- catch (Exception e){
+ } catch (Exception e) {
throw new PlcRuntimeException(e);
}
}