diff --git a/TinyGsmCommon.h b/TinyGsmCommon.h index 60a5e48..e0a37aa 100644 --- a/TinyGsmCommon.h +++ b/TinyGsmCommon.h @@ -71,4 +71,26 @@ const T& TinyGsmMax(const T& a, const T& b) return (b < a) ? a : b; } +template +uint32_t TinyGsmAutoBaud(T& SerialAT) +{ + static uint32_t rates[] = { 115200, 57600, 38400, 19200, 9600, 74400, 74880, 230400, 460800, 2400, 4800, 14400, 28800 }; + + for (unsigned i = 0; i < sizeof(rates)/sizeof(rates[0]); i++) { + uint32_t rate = rates[i]; + DBG("Trying baud rate", rate, "..."); + SerialAT.begin(rate); + delay(10); + for (int i=0; i<3; i++) { + SerialAT.print("AT\r\n"); + String input = SerialAT.readString(); + if (input.indexOf("OK") >= 0) { + DBG("Modem responded at rate", rate); + return rate; + } + } + } + return 0; +} + #endif