Serial1.available() not working

I tried the following program and find that the loop does not enter after Serial1.available() . The Serial1.available is not working , please help!! , Whereas if I try SerialUSB.available() The program works fine. I shorted the Tx(6) and Rx(7) pins of xiao

byte c;
byte d;
void setup() {
    SerialUSB.begin(9600);
    Serial1.begin(9600);
}
 
void loop() {

      
      if(Serial1.available()>0){
      c = SerialUSB.read();
      Serial1.write(c);
      d = Serial1.read();
      SerialUSB.write(d);
      delay(1000);
      }
}

Can anyone please reply? I think its problem with the serial library?

This is a bit of Confusing.

Have yo tested whether you get the correct string on the Machine when you just do Serial.println (“This will test the functionality of the serial”); After your time in setup()? the next thing to test could be if the port of Serial 1 really fit for your data source.

Yes SerialUSB works perfectly fine. The problem is with Serial1

Hi Sundar_19,
Try below code. You can check Serial1 Read/Write function.


byte c;
byte d;
void setup() 
{
    SerialUSB.begin(9600);
    while(!SerialUSB){};
    Serial1.begin(9600);
    while(!Serial1){};
}
 
void loop() 
{
  if(SerialUSB.available()>0)
  {
    c = SerialUSB.read();
    Serial1.write(c);
    if(Serial1.available()>0)
    {
      d = Serial1.read();
      SerialUSB.write(d);
    }   
  } 
}

Thank you so much but the previous values are pushed only when I send the next value , that is when I enter 1 in serial monitor first and then 2 , the 1 is reflected in serial monitor only after I enter 2 .

Sundar_19,
Set the serial monitor setting to “Newline”.
Add delay (1); to the last line of code.

Thank you so much , our team checked it and it works fine , thank you again!!!

1 Like