From 19a8ca84e6a705969002f26f4c1ea387cf86c81a Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 16 Sep 2021 16:18:20 +0300 Subject: [PATCH] PIByteArray checksumPlain invert flag fix PISerial setBreak --- libs/main/core/pibytearray.cpp | 8 ++++---- libs/main/core/pibytearray.h | 8 ++++---- libs/main/io_devices/piserial.cpp | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/libs/main/core/pibytearray.cpp b/libs/main/core/pibytearray.cpp index ad05f6e1..9fc22fdc 100644 --- a/libs/main/core/pibytearray.cpp +++ b/libs/main/core/pibytearray.cpp @@ -221,22 +221,22 @@ PIByteArray & PIByteArray::decompressRLE(uchar threshold) { } -uchar PIByteArray::checksumPlain8() const { +uchar PIByteArray::checksumPlain8(bool inverse) const { uchar c = 0; int sz = size_s(); for (int i = 0; i < sz; ++i) c += at(i); - c = ~(c + 1); + if (inverse) c = ~(c + 1); return c; } -uint PIByteArray::checksumPlain32() const { +uint PIByteArray::checksumPlain32(bool inverse) const { uint c = 0; int sz = size_s(); for (int i = 0; i < sz; ++i) c += at(i) * (i + 1); - c = ~(c + 1); + if (inverse) c = ~(c + 1); return c; } diff --git a/libs/main/core/pibytearray.h b/libs/main/core/pibytearray.h index 31285742..d47740c8 100644 --- a/libs/main/core/pibytearray.h +++ b/libs/main/core/pibytearray.h @@ -146,18 +146,18 @@ public: PIByteArray & append(uchar t) {push_back(t); return *this;} //! Returns 8-bit checksum - //! sum all bytes, add 1, inverse + //! sum all bytes, if inverse - add 1, inverse //! Pseudocode: //! sum += at(i); //! return ~(sum + 1) - uchar checksumPlain8() const; + uchar checksumPlain8(bool inverse = true) const; //! Returns 32-bit checksum - //! sum all bytes multiplyed by index+1, add 1, inverse + //! sum all bytes multiplyed by index+1, if inverse - add 1, inverse //! Pseudocode: //! sum += at(i) * (i + 1); //! return ~(sum + 1) - uint checksumPlain32() const; + uint checksumPlain32(bool inverse = true) const; //! Returns hash uint hash() const; diff --git a/libs/main/io_devices/piserial.cpp b/libs/main/io_devices/piserial.cpp index 593b04cb..063250a5 100644 --- a/libs/main/io_devices/piserial.cpp +++ b/libs/main/io_devices/piserial.cpp @@ -296,12 +296,22 @@ bool PISerial::setBreak(bool enabled) { } #ifdef WINDOWS if (enabled) { - if (!SetCommBreak(PRIVATE->hCom)) return false; + if (!SetCommBreak(PRIVATE->hCom)) { + piCoutObj << "setBreak error: " << errorString(); + return false; + } else { + return true; + } } else { - if (!ClearCommBreak(PRIVATE->hCom)) return false; + if (!ClearCommBreak(PRIVATE->hCom)) { + piCoutObj << "setBreak error: " << errorString(); + return false; + } else { + return true; + } } #else - if (::ioctl(descriptor, enabled ? TIOCSBRK : TIOCCBRK) < 0) { + if (ioctl(descriptor, enabled ? TIOCSBRK : TIOCCBRK) < 0) { piCoutObj << "setBreak error: " << errorString(); return false; } else {