int MG2639_Cell::readWaitForResponses(const char * goodRsp, const char * failRsp, unsigned int timeout) { unsigned long timeIn = millis(); // Timestamp coming into function unsigned int received = 0; // received keeps track of number of chars read clearBuffer(); // Clear the class receive buffer (rxBuffer) while (timeIn + timeout > millis()) // While we haven't timed out { if (dataAvailable()) // If data is available on UART RX { // Increment received count & read byte to buffer received += readByteToBuffer(); if (searchBuffer(goodRsp)) return received; // If we've received [goodRsp], return received if (searchBuffer(failRsp)) // If we've received [failRsp] return ERROR_FAIL_RESPONSE; // return FAIL response error code } } if (received > 0) // If we received any characters return ERROR_UNKNOWN_RESPONSE; // Return unkown response error code else // If we haven't received any characters return ERROR_TIMEOUT; // Return the timeout error code }
int16_t ESP8266Class::readForResponse(const char * rsp, unsigned int timeout) { unsigned long timeIn = millis(); // Timestamp coming into function unsigned int received = 0; // received keeps track of number of chars read clearBuffer(); // Clear the class receive buffer (esp8266RxBuffer) while (timeIn + timeout > millis()) // While we haven't timed out { if (_serial->available()) // If data is available on UART RX { received += readByteToBuffer(); if (searchBuffer(rsp)) // Search the buffer for goodRsp return received; // Return how number of chars read } } if (received > 0) // If we received any characters return ESP8266_RSP_UNKNOWN; // Return unkown response error code else // If we haven't received any characters return ESP8266_RSP_TIMEOUT; // Return the timeout error code }
int MG2639_Cell::changeBaud(unsigned long from, unsigned long to) { int iRetVal; char changeBaudCmd[14]; memset(changeBaudCmd, 0, 14); // Print something like "AT+IPR=9600" to changeBaudCmd string sprintf(changeBaudCmd, "%s=%lu", SET_BAUD_RATE, to); initializeUART(from); // Set UART baud to [from] baud sendATCommand((const char *)changeBaudCmd); // Send the baud change command // SoftwareSerial included with Arduino 1.6.1+ is drastically improved. // We can reliably send strings at 115200 #if (ARDUINO >= 10601) iRetVal = readWaitForResponse(RESPONSE_OK, COMMAND_RESPONSE_TIME); initializeUART(to); // Set SS UART to [to] baud rate return iRetVal; #else // Earlier versions of SS can't reliably read, but we should be able // to tell the difference between "ERROR" and "OK" due to the size // of the received string. unsigned long timeIn = millis(); unsigned long timeout = 10; // Set timeout to 10ms int received = 0; clearBuffer(); // Clear rxBuffer while (timeIn + timeout > millis()) // Check for a timeout { if (dataAvailable()) // If data available on UART { received += readByteToBuffer(); // Read it into rxBuffer, inc received } } initializeUART(to); // Set SS UART to [to] baud rate return received; // Return number of characters received #endif }