Skip to content

Commit 44d2722

Browse files
hallardigrr
authored andcommitted
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 #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
1 parent cbe8f7c commit 44d2722

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

cores/esp8266/HardwareSerial.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ size_t HardwareSerial::write(uint8_t c)
180180
return 1;
181181
}
182182

183+
int HardwareSerial::baudRate(void)
184+
{
185+
// Null pointer on _uart is checked by SDK
186+
return uart_get_baudrate(_uart);
187+
}
188+
189+
183190
HardwareSerial::operator bool() const
184191
{
185192
return _uart != 0;

cores/esp8266/HardwareSerial.h

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class HardwareSerial: public Stream
133133
void setDebugOutput(bool);
134134
bool isTxEnabled(void);
135135
bool isRxEnabled(void);
136+
int baudRate(void);
136137

137138
protected:
138139
int _uart_nr;

doc/reference.md

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ You also need to use `Serial.setDebugOutput(true)` to enable output from `printf
8484
8585
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.
8686
87+
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
88+
```cpp
89+
// Set Baud rate to 57600
90+
Serial.begin(57600);
91+
92+
// Get current baud rate
93+
int br = Serial.baudRate();
94+
95+
// Will print "Serial is 57600 bps"
96+
Serial.printf("Serial is %d bps", br);
97+
```
98+
99+
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).
100+
Note that this implementation is **only for ESP8266 based boards**, and will not works with other Arduino boards.
101+
87102
## Progmem
88103

89104
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.

0 commit comments

Comments
 (0)