0

I want to use two serial ports

Serial1 - for debugging - No RX needed

Serial - to read from a different device

I receive garbled output instead of valid characters

Here is code for NodeMCU

#define LED D0


void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.flush();
  Serial.swap();
  Serial1.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()>0)
  {
//    digitalWrite(LED, HIGH);
//    delay(500);
//    digitalWrite(LED, LOW);
//    delay(500);    
//    digitalWrite(LED, HIGH);
//    delay(500);
//    digitalWrite(LED, LOW);
//    delay(500);   
    Serial1.print(Serial.read());
  }
}

and here is the code for Arduino mega which is emulating a send and receive

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial1.begin(9600);    //Transmit
  Serial2.begin(9600);    //Recieve
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()>0)
    Serial1.print(Serial.read());

  if(Serial2.available()>0)
    Serial.print(Serial2.read());
}

When I Send an Charater(Single) I receive something like this for "s" - 5257525753515257534952575256 for "d" - 5257525652565257534952575256

Kindly help

  • use Serial.write to write a character returned by read – Juraj Dec 1 '19 at 13:38
  • That does work. Thanks @Juraj – siddharth taunk Dec 1 '19 at 14:00
  • Serial.swap() function swaps the Serial to pins 15 and 13 – Juraj Dec 1 '19 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.