HWSERIAL port #define definition doesn't work

Status
Not open for further replies.

Fluxanode

Well-known member
Ok here's a head scratcher...

Teensy 3.2 connected to a computer using onboard USB serial, and FTDI USB to RS232 to a max232 connected to serial1 (pins 0 and 1).

Why doesn't this example code work:

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);
	}
}

While the version below does work when I removed the #define HWSERIAL Serial1 and explicitly used Serial1 instead of the HWSERIAL????

The version that doesn't work is copied directly from the https://www.pjrc.com/teensy/td_uart.html page. The only part that works is the USB received on the Arduino Monitor.




Code:
void setup() {
	Serial.begin(9600);
	Serial1.begin(9600);
}

void loop() {
        int incomingByte;
        
	if (Serial.available() > 0) {
		incomingByte = Serial.read();
		Serial.print("USB received: ");
		Serial.println(incomingByte, DEC);
		Serial1.print("USB received:");
		Serial1.println(incomingByte, DEC);
	}
	if (Serial1.available() > 0) {
		incomingByte = Serial1.read();
		Serial.print("UART received: ");
		Serial.println(incomingByte, DEC);
		Serial1.print("UART received:");
		Serial1.println(incomingByte, DEC);
	}
}

Is there a different way to define HWSERIAL that will work?
 
Last edited by a moderator:
Ok here's a head scratcher...

Teensy 3.2 connected to a computer using onboard USB serial, and FTDI USB to RS232 to a max232 connected to serial1 (pins 0 and 1).

Why doesn't this example code work:

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);
	}
}

While the version below does work when I removed the #define HWSERIAL Serial1 and explicitly used Serial1 instead of the HWSERIAL????

The version that doesn't work is copied directly from the https://www.pjrc.com/teensy/td_uart.html page. The only part that works is the USB received on the Arduino Monitor.




Code:
void setup() {
	Serial.begin(9600);
	Serial1.begin(9600);
}

void loop() {
        int incomingByte;
        
	if (Serial.available() > 0) {
		incomingByte = Serial.read();
		Serial.print("USB received: ");
		Serial.println(incomingByte, DEC);
		Serial1.print("USB received:");
		Serial1.println(incomingByte, DEC);
	}
	if (Serial1.available() > 0) {
		incomingByte = Serial1.read();
		Serial.print("UART received: ");
		Serial.println(incomingByte, DEC);
		Serial1.print("UART received:");
		Serial1.println(incomingByte, DEC);
	}
}

Is there a different way to define HWSERIAL that will work?

I use this all the time in my code:
Code:
static constexpr HardwareSerial &GNSS_UART = Serial3;
 
So then HardwareSerial can be used instead of Serial3?

When I use this:
Code:
static constexpr HardwareSerial &GNSS_UART = Serial3;

It creates a reference to Serial3 called GNSS_UART. So I can use GNSS_UART in my code instead of Serial3. The advantage of this approach is that it maintains type safety, unlike a define, and doesn't take up resources, since it's a reference. I typically have a header file of references like this for all of the hardware definitions for a project. Makes it pretty quick to port to a different hardware platform if, for example, the serial port changes number.

I'm not sure why your define isn't working...
 
Note: I use the #define all of the time, and I have not had an issue. The only case that I have seen is if you have some other place that defines it differently.

So you might simply try a different #define. Example sometimes I will have XBeeSerial or DBGSerial or DXLSerial defined
 
Kurt, I don't think so. I just copied and pasted form the web page. Would having another Arduino sketch window open cause problems?
 
No should make no difference. You might double check and try it again to see if there was not something slightly off?
 
Why doesn't this example code work
What exactly do you mean by doesn't work?
That code (with the #define) runs fine for me on a T3.2 and a T4.0 (on Win 10 with Arduino 1.8.13 and TD 1.53).

Pete
 
KurtE:

Ok i cleared everything and started fresh, now it works. Strange. The first time around all I did to fix was change HWSERIAL Serial1

Thanks everyone for the help.
 
Any chance you're using an old pre-10 version of Windows?

Windows 7 has a really confusing driver bug which could explain why it didn't work one time, but then magically started working the next.
 
Thanks Paul, maybe Windows 10 also. It was really weird, it wouldn't work all day but after sleeping on it and completely starting fresh with a clean file, bingo it works...
 
Status
Not open for further replies.
Back
Top