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