Skip to content

Commit

Permalink
fix: readBytesUntil from configured UARTDevice instead of Serial
Browse files Browse the repository at this point in the history
  • Loading branch information
andreanielsen83 committed Aug 31, 2024
1 parent 4de0e97 commit 2932e9f
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion p1reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,40 @@ class P1Reader : public Component, public UARTDevice {
}

private:
int timedRead() {
const unsigned long _startMillis = millis();
int c;
do {
if (available()) {
c = read();
if (c >= 0) return c;
}
else {
delay(1);
}
} while(millis() - _startMillis < 1000); // default timeout is 1000ms
return -1; // indicates timeout
}

int readBytesUntil(const char terminator, char *data, const size_t len) {
size_t count = 0;
while (count < len) {
int c = timedRead();
if (c < 0 || terminator == (char) c) break;
data[count] = (char) c;
count++;
}
return count;
}

void readP1Message() {
if (available()) {
uint16_t crc = 0x0000;
ParsedMessage parsed = ParsedMessage();
bool telegramEnded = false;

while (available()) {
int len = Serial.readBytesUntil('\n', buffer, BUF_SIZE);
int len = readBytesUntil('\n', buffer, BUF_SIZE-1);

if (len > 0) {
ESP_LOGD("data", "%s", buffer);
Expand Down

0 comments on commit 2932e9f

Please sign in to comment.