Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout for reading data from stream should abort read operation after first timeout (?) #316

Closed
sabbay opened this issue Aug 9, 2019 · 2 comments · May be fixed by #317
Closed

Timeout for reading data from stream should abort read operation after first timeout (?) #316

sabbay opened this issue Aug 9, 2019 · 2 comments · May be fixed by #317

Comments

@sabbay
Copy link

sabbay commented Aug 9, 2019

Hello,
While testing my solution I came across weird situation. While interrupting SIM800L <-> ESP32 communication I noticed that stream`s _timeout only skips the character that is being currently read, then tries to read another character in function modemRead.

My quickfix is:

  size_t modemRead(size_t size, uint8_t mux) {
#ifdef TINY_GSM_USE_HEX
    sendAT(GF("+CIPRXGET=3,"), mux, ',', size);
    if (waitResponse(GF("+CIPRXGET:")) != 1) {
      return 0;
    }
#else
    sendAT(GF("+CIPRXGET=2,"), mux, ',', size);
    if (waitResponse(GF("+CIPRXGET:")) != 1) {
      return 0;
    }
#endif
    streamSkipUntil(','); // Skip Rx mode 2/normal or 3/HEX
    streamSkipUntil(','); // Skip mux
    size_t len_requested = stream.readStringUntil(',').toInt();
    //  ^^ Requested number of data bytes (1-1460 bytes)to be read
    size_t len_confirmed = stream.readStringUntil('\n').toInt();
    // ^^ Confirmed number of data bytes to be read, which may be less than requested.
    // 0 indicates that no data can be read.
    // This is actually be the number of bytes that will be remaining after the read
    for (size_t i=0; i<len_requested; i++) {
      uint32_t startMillis = millis();
#ifdef TINY_GSM_USE_HEX
      while (stream.available() < 2 && (millis() - startMillis < sockets[mux]->_timeout)) {
        TINY_GSM_YIELD();
      }
      char buf[4] = { 0, };
      buf[0] = stream.read();
      buf[1] = stream.read();
      char c = strtol(buf, NULL, 16);
#else
      while (!stream.available() && (millis() - startMillis < sockets[mux]->_timeout)) {
        TINY_GSM_YIELD();
      }
      if(millis() - startMillis > sockets[mux]->_timeout){
        DBG("TIMEOUT, ABORTING READ.");
        break;
      }
      char c = stream.read();
#endif
      sockets[mux]->rx.put(c);
    }
    // sockets[mux]->sock_available = modemGetAvailable(mux);
    sockets[mux]->sock_available = len_confirmed;
    waitResponse();
    return len_requested;
  }

Was skipping byte by byte intended? It seems weird to me.

@SRGDamia1
Copy link
Collaborator

The stream _timeout value usually is per-character.

@SRGDamia1
Copy link
Collaborator

I'm going to close this issue. Please re-open it if you're still having issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants