Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: HWSERIAL port #define definition doesn't work

  1. #1

    HWSERIAL port #define definition doesn't work

    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 KurtE; 09-03-2020 at 08:09 PM. Reason: Code tags

  2. #2
    Senior Member brtaylor's Avatar
    Join Date
    Mar 2016
    Location
    Portland, OR
    Posts
    580
    Quote Originally Posted by Fluxanode View Post
    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;

  3. #3
    So then "HardwareSerial" can be used instead of Serial3?

  4. #4
    Senior Member brtaylor's Avatar
    Join Date
    Mar 2016
    Location
    Portland, OR
    Posts
    580
    Quote Originally Posted by Fluxanode View Post
    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...

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,671
    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

  6. #6
    Kurt, I don't think so. I just copied and pasted form the web page. Would having another Arduino sketch window open cause problems?

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,671
    No should make no difference. You might double check and try it again to see if there was not something slightly off?

  8. #8
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,454
    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

  9. #9
    Basically just that. It's as if the serial1 port isn't there.

  10. #10
    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.

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,752
    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.

  12. #12
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •