1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

(re)introduce timeout in HardwareSerial::readBytes(buffer, size) (#5558)

(re)introduce timeout in HardwareSerial::readBytes(buffer, size), add HardwareSerial::read(buffer, size) + visual test
This commit is contained in:
david gauchard
2019-01-08 04:01:21 +01:00
committed by GitHub
parent 8f8628c681
commit dc03293d82
4 changed files with 55 additions and 7 deletions

View File

@ -16,12 +16,15 @@
#define BUFFER_SIZE 4096 // may be useless to use more than 2*SERIAL_SIZE_RX
#define SERIAL_SIZE_RX 1024 // Serial.setRxBufferSize()
#define FAKE_INCREASED_AVAILABLE 100 // test readBytes's timeout
#define TIMEOUT 5000
#define DEBUG(x...) //x
uint8_t buf [BUFFER_SIZE];
uint8_t temp [BUFFER_SIZE];
bool reading = true;
size_t testReadBytesTimeout = 0;
static size_t out_idx = 0, in_idx = 0;
static size_t local_receive_size = 0;
@ -83,6 +86,7 @@ void setup() {
size_for_1sec = baud / 10; // 8n1=10baudFor8bits
logger->printf("led changes state every %zd bytes (= 1 second)\n", size_for_1sec);
logger->printf("press 's' to stop reading, not writing (induces overrun)\n");
logger->printf("press 't' to toggle timeout testing on readBytes\n");
// prepare send/compare buffer
for (size_t i = 0; i < sizeof buf; i++) {
@ -136,7 +140,7 @@ void loop() {
if (reading) {
// receive data
maxlen = Serial.available();
maxlen = Serial.available() + testReadBytesTimeout;
if (maxlen > maxavail) {
maxavail = maxlen;
}
@ -180,8 +184,16 @@ void loop() {
timeout = (last_ms = now_ms) + TIMEOUT;
}
if (logger->read() == 's') {
logger->println("now stopping reading, keeping writing");
reading = false;
}
if (logger->available())
switch (logger->read()) {
case 's':
logger->println("now stopping reading, keeping writing");
reading = false;
break;
case 't':
testReadBytesTimeout ^= FAKE_INCREASED_AVAILABLE;
logger->printf("testing readBytes timeout: %d\n", !!testReadBytesTimeout);
break;
default:;
}
}