forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[modbus] gracefully handle unexpected slave responses (openhab#7160)
* [modbus] Handle various invalid/illegal slave responses gracefully - slave responds with too little data. Instead of crashing later when using the data, abort immediately. - slave responds with function code which does not match the request. This is illegal according to protocol Since we have seen both of the errors in the wild it is better to handle them gracefully, and print out good error message for the user. In addition, we now catch all Exceptions in the runnable that polls the data. This is to cover any unexpected / unknown errors that might arise. Now that we catch the error, the polling does not get halted (behaviour of the scheduler we use) but instead it will be retried again. Obviously copy-pasted from jamod (the underlying modbus library) Signed-off-by: Sami Salonen <ssalonen@gmail.com>
- Loading branch information
1 parent
a1a2731
commit 12f95d8
Showing
7 changed files
with
293 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...n/java/org/openhab/io/transport/modbus/ModbusUnexpectedResponseFunctionCodeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.io.transport.modbus; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* Exception representing situation where function code of the response does not match request | ||
* | ||
* @author Sami Salonen - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class ModbusUnexpectedResponseFunctionCodeException extends ModbusTransportException { | ||
|
||
private static final long serialVersionUID = 1109165449703638949L; | ||
private int requestFunctionCode; | ||
private int responseFunctionCode; | ||
|
||
public ModbusUnexpectedResponseFunctionCodeException(int requestFunctionCode, int responseFunctionCode) { | ||
this.requestFunctionCode = requestFunctionCode; | ||
this.responseFunctionCode = responseFunctionCode; | ||
|
||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return String.format("Function code of request (%d) does not equal response (%d)", requestFunctionCode, | ||
responseFunctionCode); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format( | ||
"ModbusUnexpectedResponseFunctionCodeException(requestFunctionCode=%d, responseFunctionCode=%d)", | ||
requestFunctionCode, responseFunctionCode); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
.../src/main/java/org/openhab/io/transport/modbus/ModbusUnexpectedResponseSizeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.io.transport.modbus; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* Exception representing situation where data length of the response does not match request | ||
* | ||
* @author Sami Salonen - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class ModbusUnexpectedResponseSizeException extends ModbusTransportException { | ||
|
||
private static final long serialVersionUID = 2460907938819984483L; | ||
private int requestSize; | ||
private int responseSize; | ||
|
||
public ModbusUnexpectedResponseSizeException(int requestSize, int responseSize) { | ||
this.requestSize = requestSize; | ||
this.responseSize = responseSize; | ||
|
||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return String.format("Data length of the request (%d) does not equal response (%d). Slave response is invalid.", | ||
requestSize, responseSize); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("ModbusUnexpectedResponseSizeException(requestFunctionCode=%d, responseFunctionCode=%d)", | ||
requestSize, responseSize); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.