-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SerialCommHub: several improvements (#731)
* SerialCommHub: make retry count on error configurable At the moment, an automatic retry up to 2 times is done in case the Modbus operation fails. With some devices or in same use-cases, this is not desired, so let us make the retry count configurable via manifest. So mimic the current behavior and to retain backwards compatibility, let's use a default value of 2. While at, we slightly change the debug messages and unify the loop counting code. Signed-off-by: Michael Heimpold <[email protected]> * SerialCommHub: fix fd usage on invalid values open returns -1 on error, any other value has to be considered a valid fd. So we have to initialize with -1 instead of zero (which is stdin usually). This must also be considered in the destructor to not close stdin by accident. Signed-off-by: Michael Heimpold <[email protected]> * SerialCommHub: refactor error handling to prevent log flooding When devices do not respond or some other problems occur, then the the log output might be flooded with messages. To have some more central control over the log output, let's refactor the module: - introduce various exceptions and use then in places where previously the log output was directly created - introduce a common helper function which performs the Modbus function which also increases code sharing between different high-level callbacks - emit only a warning in case the last trial of all configured retries fails finally (so in case the call can be completed successfully within the configure retries, only debug messages are created, but no usual user visible output) Signed-off-by: Michael Heimpold <[email protected]> * SerialCommHub: improve hexdump of received packet Signed-off-by: Michael Heimpold <[email protected]> * SerialCommHub: optimize memory pre-allocation for result For registers, the byte_cnt should always be even. But for coils/inputs, the byte_cnt could be odd, so let's cover this by ensuring that we round upwards. Signed-off-by: Michael Heimpold <[email protected]> * SerialCommHub: check for even byte count in response Since Modbus functions which operates on registers (which are defined as 16-bit wide) are expected to return full registers, we can and should check the received response for this. Signed-off-by: Michael Heimpold <[email protected]> Signed-off-by: Moritz Barsnick <[email protected]> * SerialCommHub: silence warning about unused return value of write Ignoring the return code of write results in a compiler warning as theoretically the write could return without having written the complete packet. So handle it accordingly. In case of error, we can throw an exception. Signed-off-by: Michael Heimpold <[email protected]> * SerialCommHub: catch system error for failure to write This is assumed to be fatal and non-recoverable, therefore logging is protected against flooding. A code path for possible recovery is implemented nevertheless. This should be replaced by a proper error reporting and handling solution, yet the error interfaces and handling for this are not in place yet. Signed-off-by: Moritz Barsnick <[email protected]> * SerialCommHub: fix reading beyond end of buffer When converting a reply with an odd number of bytes of data ("read coils" or "read discrete inputs"), the byte-to-word conversion was reading beyond the end of the response message buffer. This is fixed by checking for the number of remaining bytes, and copying only what is left. Signed-off-by: Moritz Barsnick <[email protected]> --------- Signed-off-by: Michael Heimpold <[email protected]> Signed-off-by: Moritz Barsnick <[email protected]> Co-authored-by: Michael Heimpold <[email protected]> Co-authored-by: Moritz Barsnick <[email protected]>
- Loading branch information
Showing
5 changed files
with
243 additions
and
177 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
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.