File tree 1 file changed +27
-1
lines changed
1 file changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -257,14 +257,40 @@ class P1Reader : public Component, public UARTDevice {
257
257
}
258
258
259
259
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
+
260
286
void readP1Message () {
261
287
if (available ()) {
262
288
uint16_t crc = 0x0000 ;
263
289
ParsedMessage parsed = ParsedMessage ();
264
290
bool telegramEnded = false ;
265
291
266
292
while (available ()) {
267
- int len = Serial. readBytesUntil (' \n ' , buffer, BUF_SIZE);
293
+ int len = readBytesUntil (' \n ' , buffer, BUF_SIZE- 1 );
268
294
269
295
if (len > 0 ) {
270
296
ESP_LOGD (" data" , " %s" , buffer);
You can’t perform that action at this time.
0 commit comments