Skip to content

Commit

Permalink
Update EasyNextionLibrary.cpp
Browse files Browse the repository at this point in the history
Updated function readNumberFromSerial() Returns 7777 if reading fails
  • Loading branch information
Seithan authored Apr 13, 2020
1 parent 5e6fca5 commit f90d138
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/EasyNextionLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,15 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
// You must have a timer to break the while, as the code could get stuck in the while()

_tmr1 = millis();
while(_serial->available() < 2){ // Waiting for bytes to come to Serial

while(_serial->available() < 4){ // Waiting for bytes to come to Serial
if((millis() - _tmr1) > 500){ // Waiting... But not forever...
break;
uint16_t _tempUint = _serial->available();
for(int i = 0; i < _tempUint; i++){
_serial->read();
}
_waitingForNumber = true;
return _numberValue;
}
}

Expand All @@ -152,7 +158,12 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
while(_start_char != '#'){ // Read the Serial until start_char is found
_start_char = _serial->read(); // Just in case that "garbage" data lying on Serial
if((millis() - _tmr1) > 400){ // Reading... Waiting... But not forever......
break;
uint16_t _tempUint = _serial->available();
for(int i = 0; i < _tempUint; i++){
_serial->read();
}
_waitingForNumber = true;
return _numberValue;
}
}
if(_start_char == '#'){ // And when we find the character #
Expand All @@ -164,6 +175,10 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
while(_serial->available() < _len){ // Waiting for all the bytes that we declare with <len> to arrive
if((millis() - _tmr1) > 400){ // Waiting... But not forever......
_cmdFound = false; // tmr_1 a timer to avoid the stack in the while loop if there is not any bytes on _serial
_len = _serial->available();
for(int i = 0; i < _len; i++){
_serial->read();
}
break;
}
}
Expand Down Expand Up @@ -207,7 +222,7 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if


return _numberValue;
}
}


/*
Expand Down Expand Up @@ -319,4 +334,4 @@ void EasyNex::readCommand(){

break;
}
}
}

0 comments on commit f90d138

Please sign in to comment.