PIByteArray checksumPlain invert flag

fix PISerial setBreak
This commit is contained in:
Andrey
2021-09-16 16:18:20 +03:00
parent ece3fb1536
commit 19a8ca84e6
3 changed files with 21 additions and 11 deletions

View File

@@ -221,22 +221,22 @@ PIByteArray & PIByteArray::decompressRLE(uchar threshold) {
} }
uchar PIByteArray::checksumPlain8() const { uchar PIByteArray::checksumPlain8(bool inverse) const {
uchar c = 0; uchar c = 0;
int sz = size_s(); int sz = size_s();
for (int i = 0; i < sz; ++i) for (int i = 0; i < sz; ++i)
c += at(i); c += at(i);
c = ~(c + 1); if (inverse) c = ~(c + 1);
return c; return c;
} }
uint PIByteArray::checksumPlain32() const { uint PIByteArray::checksumPlain32(bool inverse) const {
uint c = 0; uint c = 0;
int sz = size_s(); int sz = size_s();
for (int i = 0; i < sz; ++i) for (int i = 0; i < sz; ++i)
c += at(i) * (i + 1); c += at(i) * (i + 1);
c = ~(c + 1); if (inverse) c = ~(c + 1);
return c; return c;
} }

View File

@@ -146,18 +146,18 @@ public:
PIByteArray & append(uchar t) {push_back(t); return *this;} PIByteArray & append(uchar t) {push_back(t); return *this;}
//! Returns 8-bit checksum //! Returns 8-bit checksum
//! sum all bytes, add 1, inverse //! sum all bytes, if inverse - add 1, inverse
//! Pseudocode: //! Pseudocode:
//! sum += at(i); //! sum += at(i);
//! return ~(sum + 1) //! return ~(sum + 1)
uchar checksumPlain8() const; uchar checksumPlain8(bool inverse = true) const;
//! Returns 32-bit checksum //! 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: //! Pseudocode:
//! sum += at(i) * (i + 1); //! sum += at(i) * (i + 1);
//! return ~(sum + 1) //! return ~(sum + 1)
uint checksumPlain32() const; uint checksumPlain32(bool inverse = true) const;
//! Returns hash //! Returns hash
uint hash() const; uint hash() const;

View File

@@ -296,12 +296,22 @@ bool PISerial::setBreak(bool enabled) {
} }
#ifdef WINDOWS #ifdef WINDOWS
if (enabled) { if (enabled) {
if (!SetCommBreak(PRIVATE->hCom)) return false; if (!SetCommBreak(PRIVATE->hCom)) {
piCoutObj << "setBreak error: " << errorString();
return false;
} else {
return true;
}
} else { } else {
if (!ClearCommBreak(PRIVATE->hCom)) return false; if (!ClearCommBreak(PRIVATE->hCom)) {
piCoutObj << "setBreak error: " << errorString();
return false;
} else {
return true;
}
} }
#else #else
if (::ioctl(descriptor, enabled ? TIOCSBRK : TIOCCBRK) < 0) { if (ioctl(descriptor, enabled ? TIOCSBRK : TIOCCBRK) < 0) {
piCoutObj << "setBreak error: " << errorString(); piCoutObj << "setBreak error: " << errorString();
return false; return false;
} else { } else {