diff --git a/.gitignore b/.gitignore index 259148f..2cbc7ae 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ *.exe *.out *.app + +.vscode/ \ No newline at end of file diff --git a/src/EasyNextionLibrary.cpp b/src/EasyNextionLibrary.cpp index 469598f..5eb6214 100644 --- a/src/EasyNextionLibrary.cpp +++ b/src/EasyNextionLibrary.cpp @@ -82,12 +82,12 @@ String EasyNex::readStr(String TextComponent){ String _Textcomp = TextComponent; bool _endOfCommandFound = false; - char _tempChar; + uint8_t _tempChar; _tmr1 = millis(); while(_serial->available()){ // Waiting for NO bytes on Serial, // as other commands could be sent in that time. - if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever...after the timeout + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout _readString = "ERROR"; break; // Exit from the loop due to timeout. Return ERROR }else{ @@ -110,7 +110,7 @@ String EasyNex::readStr(String TextComponent){ _tmr1 = millis(); while(_serial->available() < 4){ // Waiting for bytes to come to Serial, an empty Textbox will send 4 bytes // and this the minimmum number that we are waiting for (70 FF FF FF) - if((millis() - _tmr1) > 400UL){ // Waiting... But not forever...after the timeout + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout _readString = "ERROR"; break; // Exit the loop due to timeout. Return ERROR } @@ -125,7 +125,7 @@ String EasyNex::readStr(String TextComponent){ _start_char = _serial->read(); } - if((millis() - _tmr1) > 100UL){ // Waiting... But not forever...... + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...... _readString = "ERROR"; // If the 0x70 is not found within the given time, break the while() // to avoid being stuck inside the while() loop break; @@ -151,7 +151,7 @@ String EasyNex::readStr(String TextComponent){ } } - if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever...... + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...... _readString = "ERROR"; // If the end of the command is NOT found in the time given, // break the while break; @@ -175,14 +175,14 @@ uint32_t EasyNex::readNumber(String component){ _comp = component; bool _endOfCommandFound = false; - char _tempChar; + uint8_t _tempChar; _numberValue = 777777; // The function will return this number in case it fails to read the new number _tmr1 = millis(); while(_serial->available()){ // Waiting for NO bytes on Serial, // as other commands could be sent in that time. - if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever...after the timeout + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout _numberValue = 777777; break; // Exit from the loop due to timeout. Return 777777 }else{ @@ -205,7 +205,7 @@ uint32_t EasyNex::readNumber(String component){ _tmr1 = millis(); while(_serial->available() < 8){ // Waiting for bytes to come to Serial, // we are waiting for 8 bytes - if((millis() - _tmr1) > 400UL){ // Waiting... But not forever...after the timeout + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...after the timeout _numberValue = 777777; break; // Exit the loop due to timeout. Return 777777 } @@ -220,7 +220,7 @@ uint32_t EasyNex::readNumber(String component){ _start_char = _serial->read(); } - if((millis() - _tmr1) > 100UL){ // Waiting... But not forever...... + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...... _numberValue = 777777; // If the 0x71 is not found within the given time, break the while() // to avoid being stuck inside the while() loop break; @@ -240,19 +240,27 @@ uint32_t EasyNex::readNumber(String component){ while(_endOfCommandFound == false){ // As long as the three 0xFF bytes have NOT been found, run the commands inside the loop - _tempChar = _serial->read(); // Read the next byte of the Serial - - if(_tempChar == 0xFF || _tempChar == 0xFFFFFFFF){ // If the read byte is the end command byte, - _endBytes++ ; // Add one to the _endBytes counter - if(_endBytes == 3){ - _endOfCommandFound = true; // If the counter is equal to 3, we have the end command - } - }else{ // If the read byte is NOT the end command byte, + _tempChar = _serial->read(); // Read the next byte of the Serial + + if (_tempChar == 0xFFFFFFFF) + { // If the read byte is the end command byte, + _endOfCommandFound = true; // If the counter is equal to 3, we have the end command + break; + } + if (_tempChar == 0xFF) + { // If the read byte is the end command byte, + _endBytes++; // Add one to the _endBytes counter + if (_endBytes == 3) + { + _endOfCommandFound = true; // If the counter is equal to 3, we have the end command + } + } + else{ // If the read byte is NOT the end command byte, _numberValue = 777777; break; } - if((millis() - _tmr1) > 1000UL){ // Waiting... But not forever...... + if((millis() - _tmr1) > _readTimeout){ // Waiting... But not forever...... _numberValue = 777777; // If the end of the command is NOT found in the time given, // break the while break; diff --git a/src/EasyNextionLibrary.h b/src/EasyNextionLibrary.h index 48bf41b..7b16404 100644 --- a/src/EasyNextionLibrary.h +++ b/src/EasyNextionLibrary.h @@ -86,6 +86,7 @@ class EasyNex { uint32_t readNumber(String); String readStr(String); int readByte(); + void setReadTimeout(uint32_t timeout) { _readTimeout = timeout; } //--------------------------------------- // public variables @@ -118,7 +119,7 @@ class EasyNex { HardwareSerial* _serial; void readCommand(void); void callTriggerFunction(void); - + //---------------------------------------------- // for function writeNum() (write to numeric attribute) //------------------------------------------------ @@ -136,6 +137,7 @@ class EasyNex { String _comp; uint8_t _numericBuffer[4]; uint32_t _numberValue; + uint32_t _readTimeout = 1000; //--------------------------------------- // for General functions