#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// wait for serial port to connect. Needed for native USB port only
while (!Serial) {
; }
Serial.println("Monitoring serial!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
In this code (similar code in the examples) the mySerial Object is created using the constructor in line 2. Nowhere do I see where the Serial object is created in this code and yet it is used in this code to use it's functions such as Serial.available() and Serial.begin(). So where is this Serial object created in this example so the user can use it's functions?
sydney
Arduino Core automatic creates it behind the scenes.
The Serial object is a hardware serial object created via the core.
Serial.begin(9600);
It looks like the Serial and SoftwareSerial objects have the same methods, pretty much clones, but the SoftwareSerial can connect to Rx and Tx pins, whereas the Serial object is made to be hard wired into the monitor window. And that starts to make more sense when considering code like
"if (mySerial.available()) {
Serial.write(mySerial.read());"
where Serial.write is writing to the monitor from the rx pin of the mySerial object.
"while (!Serial)"
I do find this code rather strange since Serial should be a boolean variable
sydney
The Serial object of the HardwareSerial class uses a hardware serial port controller (UART) on pins 0 (RX), 1 (TX).
The SoftwareSerial library allows you to implement another serial interface on any Arduino digital pins using software tools.
The HardwareSerial class has a method
operator bool () {return true; }
And the "Serial_" class (used for USB serial) has a similar "operator bool ();" method that returns 'true' only once the USB serial connection has been established.
As far as I know from the C++ syntax the following Serial.println("") calls the method println() which is a public member of Class Serial. While the following code snippet 'while (!Serial)' to me should be (!Serial.bool()) since the Serial class does have a bool method. Is bool() some sort of default operator? I find this piece of code very baffling from the C++ perspective.
To be more precise, Serial is an object of the HardwareSerial class.
extern HardwareSerial Serial;
Knowledgeable people say that
Member functions of the form
operator TypeName()are conversion operators. They allow objects of the class type to be used as if they were of type TypeName.
Here operator bool() allows an object Serial of the class type HardwareSerial to be used as if it were a bool.
HardwareSerial::operator bool()
The .println() function is a public member function of class Print which is a public base class of class Stream which is a public base class of class HardwareSerial. That is why lcd.println() and Serial.println() looks so much alike. They are the same function.
But "bool" is not the name of a function. It can't be the name of a function because it is a 'reserved word', like 'if' or 'while'. The member function is "operator bool ()". This is called "operator overloading" and allows things like class String using the '+' operator for string concatenation. In this case it defines the function to call when a HardwareSerial or Serial_ object gets used as a bool.
.bool() is not the same as operator bool(), which is conversion operator. You can find more about it here User-defined conversion function - cppreference.com
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.