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;
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;
}

View File

@@ -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;

View File

@@ -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 {
if (!ClearCommBreak(PRIVATE->hCom)) return false;
return true;
}
} else {
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 {