Mike Maurice
Active member
The code example at URL: https://www.pjrc.com/teensy/td_uart.html
See below.
Has a problem in that if you a add HWSERIAL.clear(); it will compile, as the HWSERIAL still has the context of hardware serial versus plain serial. A warning: plain serial has no: "clear();" Note that HWSERIAL is defined as Serial1 which is a HardwareSerial feature. It would seem wise to flag serial ports that are hardware rather than not, like in the example code.
However, if you pass "HWSERIAL" as a parameter, and accidentally declare the passed parameter as "Serial" the code will compile and appears to work until you add a function in the called code that uses a HardwareSerial function. That code will not compile and if you have never included HardwareSerial.h it could take a while before the mistake becomes apparent.
It seems to me that if you are going to use HardwareSerial functionallity, then the .h file should be included even if the resulting code will compile without it. Otherwise you may get bite later and have forgotten what the solution is.
Add: #include <HardwareSerial.h> this code example.
See below.
Has a problem in that if you a add HWSERIAL.clear(); it will compile, as the HWSERIAL still has the context of hardware serial versus plain serial. A warning: plain serial has no: "clear();" Note that HWSERIAL is defined as Serial1 which is a HardwareSerial feature. It would seem wise to flag serial ports that are hardware rather than not, like in the example code.
However, if you pass "HWSERIAL" as a parameter, and accidentally declare the passed parameter as "Serial" the code will compile and appears to work until you add a function in the called code that uses a HardwareSerial function. That code will not compile and if you have never included HardwareSerial.h it could take a while before the mistake becomes apparent.
It seems to me that if you are going to use HardwareSerial functionallity, then the .h file should be included even if the resulting code will compile without it. Otherwise you may get bite later and have forgotten what the solution is.
Add: #include <HardwareSerial.h> this code example.
Code:
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1
void setup() {
Serial.begin(9600);
HWSERIAL.begin(9600);
}
void loop() {
int incomingByte;
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("USB received: ");
Serial.println(incomingByte, DEC);
HWSERIAL.print("USB received:");
HWSERIAL.println(incomingByte, DEC);
}
if (HWSERIAL.available() > 0) {
incomingByte = HWSERIAL.read();
Serial.print("UART received: ");
Serial.println(incomingByte, DEC);
HWSERIAL.print("UART received:");
HWSERIAL.println(incomingByte, DEC);
}
}