ESP8266 crash reading from serial

Hello, I have this portion of code from this library GitHub - MHassanNadeem/WatchPower: Arduino compatible driver for WatchPower (OptiSolar, VoltronicPower etc) Solar Intervers

		HardwareSerial* refSer;
		refSer->begin(2400);
		uint16_t bytesRead;
		char inputBuffer[256];
		bytesRead =  refSer->readBytesUntil('\r', inputBuffer, sizeof(inputBuffer)-1);

but when executed the ESP8266 keep crashing, can somebody help me?

thank you
Alberto

Did you ask the library author?

Also please read

yes, he didn't answer yet

think you need to create the HardwareSerial object before calling member functions

    HardwareSerial* refSer;
    refSer = new HardwareSerial(2);
    refSer->begin(2400);

tried, no luck

What is the error? Does it crash with a watchdog timer timeout? If so you may need a shorter timeout on the serial read. The readBytesUntil() will wait until there is data or the serial timeout.

Any error message?
Does it work on other controllers (i.e. UNO)?

Increase your baudrate on the serial to avoid esp8266 watchdog timer issues might be something to try.

this works on a ESP32 NodeMCU

// ESP32 example using HardwareSerial library to access Serial port 2 (pins 16 and 17)

#define SERIAL_BAUD 115200

HardwareSerial* refSer;

void setup() {
  //Initialize serial port 0
  Serial.begin(SERIAL_BAUD);
  delay(2000);
  Serial.println("ESP32 HardwareSerial test port 2 pins 16 and 17");
  //Initialize serial port 2
  refSer = new HardwareSerial(2);
  refSer->begin(SERIAL_BAUD, SERIAL_8N1);
}

void loop() {
  while (refSer->available() > 0) {
    uint8_t byteFromSerial = refSer->read();
    Serial.write(byteFromSerial);
  }
  while (Serial.available() > 0) {
    uint8_t byteFromSerial1 = Serial.read();
    refSer->write(byteFromSerial1);
  }
}

serial monitor displays

08:46:35.119 -> ESP32 HardwareSerial test port 2 pins 16 and 17
08:46:43.696 -> test1
08:46:48.940 -> rest 2
08:46:53.331 -> 1234567890
08:46:58.033 -> abcdefgh

test using ESP8266 ESP12E transmit using serial 1 pin 17 (D4) TXD1
note: no RXD1 pin available

// ESP8266 example using HardwareSerial library to transmit using Serial port 1 pins TXD1 17 (no RXD1)

#define SERIAL_BAUD 115200

HardwareSerial* refSer;

void setup() {
  //Initialize serial port 0
  Serial.begin(SERIAL_BAUD);
  delay(2000);
  Serial.println("\nESP8266 HardwareSerial transmit test port 1 pin TXD1 17");
  //Initialize serial port 1
  refSer = new HardwareSerial(1);
  refSer->begin(SERIAL_BAUD, SERIAL_8N1);
}

void loop() {
  while (refSer->available() > 0) {
    uint8_t byteFromSerial = refSer->read();
    Serial.write(byteFromSerial);
  }
  while (Serial.available() > 0) {
    uint8_t byteFromSerial1 = Serial.read();
    Serial.write(byteFromSerial1);
    refSer->write(byteFromSerial1);
  }
}

ESP8266 serial monitor

09:16:19.888 -> ESP8266 HardwareSerial transmit test port 1 pin TXD1 17
09:19:20.191 -> esp8266 test 1
09:19:26.044 -> test2
09:19:30.640 -> 1234567890
09:19:35.582 -> abcdefghijk

connected to ESP32 for test output

ESP32 HardwareSerial test port 2 pins 16 and 17
esp8266 test 1
test2
1234567890
abcdefghijk

I think I have solved it, had to change a function:

before:

uint16_t WatchPower::readLine(char *buffer, uint16_t length){
    if(length < 1) return 0;

    uint16_t bytesRead = refSer->readBytesUntil('\r', buffer, length - 1);
    buffer[bytesRead] = 0;

    return bytesRead;
}

after:

uint16_t WatchPower::readLine(char *buffer, uint16_t length){
    if(length < 1) return 0;
    uint16_t bytesRead;
    buffer[bytesRead] = 0;
    while (refSer->available() > 0) {
        bytesRead = refSer->readBytesUntil('\x0D', buffer, length - 1);
  	}
    return bytesRead;
}

thank you for the suggestions..