From 44d27228c5f6c57c8471e79b0297df5d0a6695a3 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 15 Jun 2016 12:35:33 +0200 Subject: [PATCH] Added Serial.baudRate() to get current baud rate (#2079) * Changed WifInfo settings and WeMos board name * Added board name to have in sketch and MDNS/OTA * board naming convention https://github.com/esp8266/Arduino/pull/2054 * Added Serial.baudRate() to get current baud rate * Added more description - Added note about Software Serial Implementation - Indicate this will works on ESP8266 boards only --- cores/esp8266/HardwareSerial.cpp | 7 +++++++ cores/esp8266/HardwareSerial.h | 1 + doc/reference.md | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 18a581dde..b69729ae4 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -180,6 +180,13 @@ size_t HardwareSerial::write(uint8_t c) return 1; } +int HardwareSerial::baudRate(void) +{ + // Null pointer on _uart is checked by SDK + return uart_get_baudrate(_uart); +} + + HardwareSerial::operator bool() const { return _uart != 0; diff --git a/cores/esp8266/HardwareSerial.h b/cores/esp8266/HardwareSerial.h index bf3dc68cb..a05cb5d30 100644 --- a/cores/esp8266/HardwareSerial.h +++ b/cores/esp8266/HardwareSerial.h @@ -133,6 +133,7 @@ public: void setDebugOutput(bool); bool isTxEnabled(void); bool isRxEnabled(void); + int baudRate(void); protected: int _uart_nr; diff --git a/doc/reference.md b/doc/reference.md index 9379ecf63..ebf9ce0e6 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -84,6 +84,21 @@ You also need to use `Serial.setDebugOutput(true)` to enable output from `printf Both `Serial` and `Serial1` objects support 5, 6, 7, 8 data bits, odd (O), even (E), and no (N) parity, and 1 or 2 stop bits. To set the desired mode, call `Serial.begin(baudrate, SERIAL_8N1)`, `Serial.begin(baudrate, SERIAL_6E2)`, etc. +A new method has been implemented on both `Serial` and `Serial1` to get current baud rate setting. To get the current baud rate, call `Serial.baudRate()`, `Serial1.baudRate()`. Return a `int` of current speed. For example +```cpp +// Set Baud rate to 57600 +Serial.begin(57600); + +// Get current baud rate +int br = Serial.baudRate(); + +// Will print "Serial is 57600 bps" +Serial.printf("Serial is %d bps", br); +``` + +I've done this also for official ESP8266 [Software Serial](https://github.com/esp8266/Arduino/blob/master/doc/libraries.md#softwareserial) library, see this [pull request](https://github.com/plerup/espsoftwareserial/pull/22). +Note that this implementation is **only for ESP8266 based boards**, and will not works with other Arduino boards. + ## Progmem The Program memory features work much the same way as on a regular Arduino; placing read only data and strings in read only memory and freeing heap for your application.