two or more hardware serial ports?

ralphhipps

Active member
Using Teensy 3.6, Arduino IDE 1.8.15, Teensyduino 1.54.

Trying to use two hw serial ports at the same time.

Is that possible? (please pardon my noobiness)

I found this page:

https://www.pjrc.com/teensy/td_uart.html

But it doesn't have much in the way of examples for this, only shows how to use one (1) hw serial port, see below.

How do I use serial1 and serial4, for example? or serial1, serial2, and serial3? etc.

I've searched the Forum and so far I can't find anything on this at all.


Example Code
This simple example shows how to use both the UART and USB Serial at the same time. Both are monitored for incoming bytes, and when either receives data, the results are printed to both.
// 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);
}
}
 
Just use Serial for USB and Serial1, Serial2, etc. for the others.

e.g.

Code:
void setup() {
  Serial.begin(9600); // USB
  Serial1.begin(9600); // Pins RX1/TX1
  Serial2.begin(9600); // Pins RX2/TX2
}

The "#define HWSERIAL Serial1" in the example code is just a convenience to replace HWSERIAL throughout your sketch with "Serial1". This means that if you later want the same code but using Serial3 instead of Serial1 you just have to update the HWSERIAL define statement, (and of course wire your other device to the RX3/TX3 pins instead of RX1/TX1).
 
if (Serial1.available() > 0)
if (Serial2.available() > 0)
if (Serial3.available() > 0)
etc

edit: Ninja'd by DaveAK
 
I was hoping it would be that simple, but so far I'm getting errors:

error: 'Serial1' was not declared in this scope
error: 'Serial4' was not declared in this scope

note: using serial for USB monitor connection


void setup() {

if (debug >= 1) {
Serial.begin(115200); // no line ending in terminal
delay(1250); // teensy needs time to get ready
}

Serial1.begin(9600); // Begin communication with Serial1 for OpenLCD
delay(1000);

LCD_clear(); // forces the cursor to the beginning of the display
delay(1000);
LCD_setCursor(0,0);
Serial1.print("LCD display test");

Serial4.begin(9600);

if (Serial4.available() > 0) {
headingCurrent = Serial4.read();
 
ok, fixed it, operator error.

too many projects at one time, forgot to switch the IDE back to the Teensy 3.6. The Arduino Uno doesn't support all this, of course. =)

compiles fine now without any #defines to HWSERIAL, etc., just serial1, serial4.

thanks for the confirmation, I think that made me look for something simple that I must have been overlooking, and sure enough... =)

Thanks again!
 
Hi, glad you got it working.
In future, when you post, could you enclose your code between code tags using the # key.
That's what @DakeAK did. As you can see it makes your post so much more readable and the code easier to understand.
 
In future, when you post, could you enclose your code between code tags using the # key.

@ralphhipps: For clarity, the "# key" that BriComp refers to is the "#" button located above the editor area (not the "#" key on the keyboard). You can use one of the following ways to make use of this button, which will make it easier to read your code, as well as making it easier for others to cut & paste the code for testing/troubleshooting:

a) paste your code into the editor area as part of your post
b) using your mouse, highlight all of your code
c) with your code highlighted, press the "#" button to enclose your code in "
Code:
" tags

- OR -

a) press the "#" button to add "
Code:
" tags
b) paste your code into the editor area so that it is enclosed between the two added tags

Hope that makes things clearer . . .

Mark J Culross
KD5RXT
 
Back
Top