//Check buffer with a particular int WiFlyRNXV::compareBufferResponse(char* compareFirst,char* compareSecond){ //Variables char* responseBuffer; //Buffer for response boolean bufRead = true; //Finish Reading int bufpos = 0; //Buffer position char chResponse = 'A'; //Initial character response int bufsize = RESPONSE_BUFFER_SIZE -1; //terminating null for bufsize boolean compareSuccess=false; //Compare Success int compareCase=0; //Compare Case //Reset the buffer responseBuffer = (char*) malloc(RESPONSE_BUFFER_SIZE); memset (responseBuffer, '\0', bufsize); //Fill the buffer unsigned long startTime = millis(); while(bufRead){ //Start getting values if(uart.available()){ chResponse = uart.read(); //Serial.print(chResponse); //Check this, buffer overflow if(bufpos<bufsize){ responseBuffer[bufpos]=chResponse; bufpos++; }else{ Serial.println("Buffer overflow"); bufpos=0; } } //Check for existence of the comparison string, or if timeout stop if(checkForString(responseBuffer,compareFirst)){ compareSuccess=true; compareCase=1; bufRead=false; }if(checkForString(responseBuffer,compareSecond)){ compareCase=2; compareSuccess=true; bufRead=false; }else if((millis()-startTime)>1000){ compareCase=0; compareSuccess=false; bufRead=false; } } uart.flush(); return compareCase; }
//Check buffer with a particular. always stops at $ boolean WiFlyRNXV::checkBufferResponse(String compareValue,int timeout){ //NULL case boolean noCase=false; if(compareValue==NULL) noCase=true; initializeString(responseBuffer,LONG_STRING); //Variables //char* responseBuffer; //Buffer for response boolean bufRead = true; //Finish Reading int bufpos = 0; //Buffer position char chResponse = 'A'; //Initial character response boolean compareSuccess=false; //Compare Success //Fill the buffer unsigned long startTime = millis(); while(bufRead){ //Start getting values if(uart.available()){ chResponse = uart.read(); //Serial.print(chResponse); //Keep reading until $ seen if(noCase){ if(chResponse=='$'){ compareSuccess=true; break; } } //Place into String if(bufpos<100){ responseBuffer[bufpos]=chResponse; bufpos++; }else{ Serial.println("Overflow"); bufpos=0; } } //Check for existence of the comparison string, or if timeout stop if(checkForString(compareValue,responseBuffer) && noCase==false){ compareSuccess=true; bufRead=false; }else if((millis()-startTime)>timeout){ compareSuccess=false; bufRead=false; } } uart.flush(); //if(compareSuccess)Serial.println("Found: "+compareValue); //else Serial.println("Not found: "+compareValue); return compareSuccess; }
int WiFlyRNXV::ProcessResponse(char* buffer) { Serial.println("Processing following response:"); Serial.println(buffer); // Check if sync command issued if(checkForString(buffer,KEYWORD_SYNC)) { Serial.println("Sending UDP sync request."); SendUDP("<Prototype:pw9999:2:0000>"); return -1; } // Check if switches should be toggled else { int i; int switch_status = 0; bool found_command = false; // Check for delimiting symbols. if(buffer[0] == KEYWORD_FRONT_DELIMITER && buffer[MAX_SWITCHES+1] == KEYWORD_END_DELIMITER) { Serial.println("Response matches switch update request."); found_command = true; for(i=0; i<MAX_SWITCHES; i++) { if(buffer[i+1] != '1' && buffer[i+1] != '0') found_command = false; } } if(found_command == true) { Serial.println("Found switch data!"); for(i=0; i<MAX_SWITCHES; i++) { // Arrange flags if (buffer[i+1] == '1') switch_status += 1; // If not 1 or 0 then read in current port and don't adjust switch. nyi. switch_status = switch_status << 1; } return switch_status; } else { Serial.println("Could not find valid data."); return -1; } } }
boolean WiFlyRNXV::getDataType(String& fillBuff,int action){ initializeString(fillBuff,SHORT_STRING); if(!inCommandMode) EnterCommandMode(); String delimit; if(action==GET_IP){ delimit="IP="; uart.println("get ip"); }else if(action==GET_DEVID){ delimit="d="; uart.println("show deviceid"); } boolean bufRead = true; //Finish Reading boolean ipReadMode = false; //Ready to write in IP boolean ipReadOver = false; //Finished writing IP int bufpos = 0; //Buffer position char chResponse = 'A'; //Initial character response boolean compareSuccess=false; //Compare Success int timeout=5000; //Timeout value fixed //Fill the buffer unsigned long startTime = millis(); while(bufRead){ //Start getting values if(uart.available()){ chResponse = uart.read(); //Serial.print(chResponse); //Stop at character : if(chResponse==':'){ ipReadOver=true; break; } if(ipReadMode==false){ responseBuffer[bufpos]=chResponse; }else{ fillBuff[bufpos]=chResponse; } bufpos++; } //Check for existence of the comparison string, or if timeout stop if(checkForString(delimit,responseBuffer) && ipReadMode==false){ ipReadMode=true; bufpos=0; }else if((millis()-startTime)>timeout){ compareSuccess=false; bufRead=false; } } if(ipReadOver==true){ //ipValue.trim(); ipValue.replace(" ",""); Serial.print("IPVAL:"); Serial.println(fillBuff); compareSuccess=true; delay(200); ExitCommandMode(); } uart.flush(); return compareSuccess; }