RyanN
1
I've got three serial devices on Serial1, Serial2 and Serial3 attached to a mega. I'm trying to write a console program to communicate with them.
I set up a HardwareSerial object called SensorSerial, but once I assign it, I can't change it. In this example it will always be Serial1
Is there a trick to this?
#include <HardwareSerial.h>
uint8_t term_mode = 0;
char term_char;
char sens_char;
HardwareSerial & SensorSerial = Serial1;
void setup() {
Serial.begin(9600);
Serial.println("Please select mode:");
Serial.println(" 1 - RGB sensor");
Serial.println(" 2 - DO sensor");
Serial.println(" 3 - Conductivity sensor");
while ( term_mode <= 0 or term_mode > 4) {
if ( Serial.available() ) {
term_char = Serial.read() - '0';
switch (term_char) {
case 1: { /* RGB */
HardwareSerial & SensorSerial = Serial1;
term_mode = 1;
break;
}
case 2: { /* DO */
HardwareSerial & SensorSerial = Serial2;
term_mode = 2;
break;
}
case 3: { /*Conductivity*/
HardwareSerial & SensorSerial = Serial3;
term_mode = 3;
break;
}
default:
Serial.print("Invalid selection: ");
Serial.println(term_char + '0');
}
}
}
SensorSerial.begin(38400);
}
void loop() {
while ( Serial.available() ) {
term_char = Serial.read();
if ( term_char != 10 ) {
SensorSerial.write(term_char);
if (term_char == 13 ) Serial.println("<CR>");
}
}
while ( SensorSerial.available() ) {
sens_char = SensorSerial.read();
Serial.write(sens_char);
if ( sens_char == 13 ) Serial.write(10);
}
}
Do you not have to initialize each Serial occurrence like...
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
...
Bill
system
3
There are 4 instances of the HardwareSerial class defined for you. You must not create more of them, because there are no more hardware serial ports.
What you can do is create a pointer to an instance of the HardwareSerial class, and make it point to one of the existing instances.
HardwareSerial *serialToUse;
Then, you can make it point to an instance:
serialToUse = &Serial3;
Then, use it:
serialToUse->print("Serial3 gets this...");
RyanN
4
I only need one of the three at a time and they all run at 38400 so I call begin() at the end of setup().
SensorSerial.begin(38400);
Based on PaulS's suggestion, It would be:
serialToUse->begin(38400);
Billdefish:
Do you not have to initialize each Serial occurrence like...
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
...
Bill
RyanN
5
Here's the code based on PaulS's suggestion:
#include <HardwareSerial.h>
uint8_t term_mode = 0;
char term_char;
char sens_char;
HardwareSerial *serialToUse;
void setup() {
Serial.begin(9600);
Serial.println("Please select mode:");
Serial.println(" 1 - RGB sensor");
Serial.println(" 2 - DO sensor");
Serial.println(" 3 - Conductivity sensor");
while ( term_mode <= 0 or term_mode > 4) {
if ( Serial.available() ) {
term_char = Serial.read() - '0';
switch (term_char) {
case 1: { /* RGB */
serialToUse = &Serial1;
term_mode = 1;
break;
}
case 2: { /* DO */
serialToUse = &Serial2;
term_mode = 2;
break;
}
case 3: { /*Conductivity*/
serialToUse = &Serial3;
term_mode = 3;
break;
}
default:
Serial.print("Invalid selection: ");
Serial.println(term_char + '0');
}
}
}
serialToUse->begin(38400);
}
void loop() {
while ( Serial.available() ) {
term_char = Serial.read();
if ( term_char != 10 ) {
serialToUse->write(term_char);
if (term_char == 13 ) Serial.println("<CR>");
}
}
while ( serialToUse->available() ) {
sens_char = serialToUse->read();
Serial.write(sens_char);
if ( sens_char == 13 ) Serial.write(10);
}
}