Merge pull request #386 from adrianca88/fixed_streamGetLength_functions
Fixed problem with streamGetFloatLength and streamGetIntLength functions
This commit is contained in:
@@ -262,16 +262,33 @@ class TinyGsmModem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline int16_t streamGetIntLength(int8_t numChars) {
|
inline bool streamGetLength(char* buf, int8_t numChars, const uint32_t timeout_ms = 1000L) {
|
||||||
char buf[6];
|
if (!buf) {
|
||||||
size_t bytesRead = thisModem().stream.readBytes(buf, numChars);
|
return false;
|
||||||
if (bytesRead) {
|
|
||||||
buf[numChars] = '\0';
|
|
||||||
int16_t res = atoi(buf);
|
|
||||||
return res;
|
|
||||||
} else {
|
|
||||||
return -9999;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t numCharsReady = -1;
|
||||||
|
uint32_t startMillis = millis();
|
||||||
|
while (millis() - startMillis < timeout_ms && (numCharsReady = thisModem().stream.available()) < numChars) {
|
||||||
|
TINY_GSM_YIELD();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numCharsReady >= numChars) {
|
||||||
|
thisModem().stream.readBytes(buf, numChars);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int16_t streamGetIntLength(int8_t numChars, const uint32_t timeout_ms = 1000L) {
|
||||||
|
char buf[numChars + 1];
|
||||||
|
if (streamGetLength(buf, numChars, timeout_ms)) {
|
||||||
|
buf[numChars] = '\0';
|
||||||
|
return atoi(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -9999;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int16_t streamGetIntBefore(char lastChar) {
|
inline int16_t streamGetIntBefore(char lastChar) {
|
||||||
@@ -283,34 +300,33 @@ class TinyGsmModem {
|
|||||||
buf[bytesRead] = '\0';
|
buf[bytesRead] = '\0';
|
||||||
int16_t res = atoi(buf);
|
int16_t res = atoi(buf);
|
||||||
return res;
|
return res;
|
||||||
} else {
|
|
||||||
return -9999;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return -9999;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float streamGetFloatLength(int8_t numChars) {
|
inline float streamGetFloatLength(int8_t numChars, const uint32_t timeout_ms = 1000L) {
|
||||||
char buf[16];
|
char buf[numChars + 1];
|
||||||
size_t bytesRead = thisModem().stream.readBytes(buf, numChars);
|
if (streamGetLength(buf, numChars, timeout_ms)) {
|
||||||
if (bytesRead) {
|
|
||||||
buf[numChars] = '\0';
|
buf[numChars] = '\0';
|
||||||
int16_t res = atof(buf);
|
return atof(buf);
|
||||||
return res;
|
|
||||||
} else {
|
|
||||||
return static_cast<float>(-9999);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return -9999.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float streamGetFloatBefore(char lastChar) {
|
inline float streamGetFloatBefore(char lastChar) {
|
||||||
char buf[16];
|
char buf[16];
|
||||||
size_t bytesRead = thisModem().stream.readBytesUntil(
|
size_t bytesRead = thisModem().stream.readBytesUntil(
|
||||||
lastChar, buf, static_cast<size_t>(16));
|
lastChar, buf, static_cast<size_t>(16));
|
||||||
if (bytesRead) {
|
// if we read 16 or more bytes, it's an overflow
|
||||||
|
if (bytesRead && bytesRead < 16) {
|
||||||
buf[bytesRead] = '\0';
|
buf[bytesRead] = '\0';
|
||||||
float res = atof(buf);
|
float res = atof(buf);
|
||||||
return res;
|
return res;
|
||||||
} else {
|
|
||||||
return static_cast<float>(-9999);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return -9999.0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool streamSkipUntil(const char c, const uint32_t timeout_ms = 1000L) {
|
inline bool streamSkipUntil(const char c, const uint32_t timeout_ms = 1000L) {
|
||||||
|
Reference in New Issue
Block a user