Skip to content

Commit 5271213

Browse files
readBytesUntil from configured UARTDevice instead of Serial
1 parent 4de0e97 commit 5271213

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

p1reader.h

+27-1
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,40 @@ class P1Reader : public Component, public UARTDevice {
257257
}
258258

259259
private:
260+
int timedRead() {
261+
const unsigned long _startMillis = millis();
262+
int c;
263+
do {
264+
if (available()) {
265+
c = read();
266+
if (c >= 0) return c;
267+
}
268+
else {
269+
delay(1);
270+
}
271+
} while(millis() - _startMillis < 1000); // default timeout is 1000ms
272+
return -1; // indicates timeout
273+
}
274+
275+
int readBytesUntil(const char terminator, char *data, const size_t len) {
276+
size_t count = 0;
277+
while (count < len) {
278+
int c = timedRead();
279+
if (c < 0 || terminator == (char) c) break;
280+
data[count] = (char) c;
281+
count++;
282+
}
283+
return count;
284+
}
285+
260286
void readP1Message() {
261287
if (available()) {
262288
uint16_t crc = 0x0000;
263289
ParsedMessage parsed = ParsedMessage();
264290
bool telegramEnded = false;
265291

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

269295
if (len > 0) {
270296
ESP_LOGD("data", "%s", buffer);

0 commit comments

Comments
 (0)