Reading from serial port

Hi all,
I have an Arduino mega reading serial data (six integer values, separated by a comma, new line at the end) into serial port 1 (18, 19). These values shall become converted to 6 variables. For control I want to print the values of the variables to the serial monitor:

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  if (Serial1.available()) {
    long vi = Serial1.parseInt();
    long bl = Serial1.parseInt();
    long gn = Serial1.parseInt();
    long yl = Serial1.parseInt();
    long og = Serial1.parseInt();
    long rd = Serial1.parseInt();
    if (Serial1.read() == '\n') {
    Serial.print(vi);Serial.print(",");Serial.print(bl);Serial.print(",");
    Serial.print(gn);Serial.print(",");Serial.print(yl);Serial.print(",");
    Serial.print(og);Serial.print(",");Serial.print(rd);Serial.println();
    }
  }

}

But nothing happens. The serial monitor remains empty.

What is wrong?

With the following code I have checked that data are received:

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

}

-richard

Where is the serial input coming from ?

The serial input basics tutorial may be of interest.

In your code, if one byte is available read 24 bytes. It would be better to read all the data in and then parse the data like in the tutorial.

This works:

long vi,bl,gn,yl,og,rd;

void setup() {
  
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
  
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    vi = Serial1.parseInt();Serial.print(vi);Serial.print(",");
    bl = Serial1.parseInt();Serial.print(bl);Serial.print(",");
    gn = Serial1.parseInt();Serial.print(gn);Serial.print(",");
    yl = Serial1.parseInt();Serial.print(yl);Serial.print(",");
    og = Serial1.parseInt();Serial.print(og);Serial.print(",");
    rd = Serial1.parseInt();Serial.print(rd);Serial.println();}
  }

You are still reading from Serial when all you are sure of is that there is at least one byte of data available. You are probably getting away with it because of the time taken to print to the Serial monitor but you should not rely on that.