Skip to content

Commit f90d138

Browse files
authored
Update EasyNextionLibrary.cpp
Updated function readNumberFromSerial() Returns 7777 if reading fails
1 parent 5e6fca5 commit f90d138

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/EasyNextionLibrary.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,15 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
140140
// You must have a timer to break the while, as the code could get stuck in the while()
141141

142142
_tmr1 = millis();
143-
while(_serial->available() < 2){ // Waiting for bytes to come to Serial
143+
144+
while(_serial->available() < 4){ // Waiting for bytes to come to Serial
144145
if((millis() - _tmr1) > 500){ // Waiting... But not forever...
145-
break;
146+
uint16_t _tempUint = _serial->available();
147+
for(int i = 0; i < _tempUint; i++){
148+
_serial->read();
149+
}
150+
_waitingForNumber = true;
151+
return _numberValue;
146152
}
147153
}
148154

@@ -152,7 +158,12 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
152158
while(_start_char != '#'){ // Read the Serial until start_char is found
153159
_start_char = _serial->read(); // Just in case that "garbage" data lying on Serial
154160
if((millis() - _tmr1) > 400){ // Reading... Waiting... But not forever......
155-
break;
161+
uint16_t _tempUint = _serial->available();
162+
for(int i = 0; i < _tempUint; i++){
163+
_serial->read();
164+
}
165+
_waitingForNumber = true;
166+
return _numberValue;
156167
}
157168
}
158169
if(_start_char == '#'){ // And when we find the character #
@@ -164,6 +175,10 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
164175
while(_serial->available() < _len){ // Waiting for all the bytes that we declare with <len> to arrive
165176
if((millis() - _tmr1) > 400){ // Waiting... But not forever......
166177
_cmdFound = false; // tmr_1 a timer to avoid the stack in the while loop if there is not any bytes on _serial
178+
_len = _serial->available();
179+
for(int i = 0; i < _len; i++){
180+
_serial->read();
181+
}
167182
break;
168183
}
169184
}
@@ -207,7 +222,7 @@ uint32_t EasyNex::readNumberFromSerial(){ // With this functions, we check if
207222

208223

209224
return _numberValue;
210-
}
225+
}
211226

212227

213228
/*
@@ -319,4 +334,4 @@ void EasyNex::readCommand(){
319334

320335
break;
321336
}
322-
}
337+
}

0 commit comments

Comments
 (0)