SerialEvent question

Hello Dude,
I'm trying Arduino with SerialEvent() function, but I don't understand the some part of following function written in HardwareSerial.cpp.

#if defined(USART_RX_vect)
SIGNAL(USART_RX_vect)
#elif defined(SIG_USART0_RECV)
SIGNAL(SIG_USART0_RECV)
#elif defined(SIG_UART0_RECV)
SIGNAL(SIG_UART0_RECV)
#elif defined(USART0_RX_vect)
SIGNAL(USART0_RX_vect)
#elif defined(SIG_UART_RECV)
SIGNAL(SIG_UART_RECV)
#endif
{
#if defined(UDR0)
if (bit_is_clear(UCSRA, PE)) {
unsigned char c = UDR;
store_char(c, &rx_buffer); //<-- what does this line mean?
} else {
unsigned char c = UDR;
};
#elif defined(UDR)
if (bit_is_clear(UCSRA, PE)) {
unsigned char c = UDR;
store_char(c, &rx_buffer);
} else {
unsigned char c = UDR;
};
#else
#error UDR not defined
#endif
}
#endif
#endif

But I understand that serial incoming data is stored in "c" when serial Rx interrupt occurs. In the tutorial of SerialEvent,

void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read(); //<-- why do we need to read again?
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}

Note: Arduino IDE v1.0.4 with Arduino UNO R3.
Thanks,
pak

[color=red]char inChar

Doesn't make sense.

Sorry dude, It happens like that when you use [color=red]Code here. [/color]

So I have no choice but to use this

Quote here.

<-- why do we need to read again?

You need to read each character, one at a time, until the buffer is empty or you get a newline character.

char inChar = (char)Serial.read(); //<-- why do we need to read again?

The HardwareSerial class is reading the data from the serial port and putting it in a buffer (or stack). The Serial.read() method is then popping a value off the stack/reading and removing it from the buffer. Two completely different operations.

Thank for reply, and how about this, does it mean Rx data is stored in rx_buffer?

#if defined(USART_RX_vect)
SIGNAL(USART_RX_vect)
#elif defined(SIG_USART0_RECV)
SIGNAL(SIG_USART0_RECV)
#elif defined(SIG_UART0_RECV)
SIGNAL(SIG_UART0_RECV)
#elif defined(USART0_RX_vect)
SIGNAL(USART0_RX_vect)
#elif defined(SIG_UART_RECV)
SIGNAL(SIG_UART_RECV)
#endif
{
#if defined(UDR0)
if (bit_is_clear(UCSRA, PE)) {
unsigned char c = UDR;
store_char(c, &rx_buffer); //<-- what does this line mean?
} else {
unsigned char c = UDR;
};
#elif defined(UDR)
if (bit_is_clear(UCSRA, PE)) {
unsigned char c = UDR;
store_char(c, &rx_buffer);
} else {
unsigned char c = UDR;
};
#else
#error UDR not defined
#endif
}
#endif
#endif

and how about this, does it mean Rx data is stored in rx_buffer?

Yes.

How to retrieve that rx_buffer? Any example? I'm using Mega1280 and how do I do with serialEvent1() function?

#if defined(USART1_RX_vect)
  void serialEvent1() __attribute__((weak));
  void serialEvent1() {}
  #define serialEvent1_implemented
  SIGNAL(USART1_RX_vect)
  {
    if (bit_is_clear(UCSR1A, UPE1)) {
      unsigned char c = UDR1;
      store_char(c, &rx_buffer1);
    } else {
      unsigned char c = UDR1;
    };
  }
#elif defined(SIG_USART1_RECV)
  #error SIG_USART1_RECV
#endif

Actually, I want to this way as follow:

ISR(USART1_RX_vect) 
{  
  unsigned char c = UDR1; // read incoming byte to clear interrupt flag
  UDR1 = c;
  UDR0 = c;
}

The serialEventN() functions are not interrupts. The snippets of code that you post from the HardwareSerial class have nothing to do with the code that YOU put inside the serialEvent() method.

Instead of telling us HOW you want to do something, tell us WHAT you want to do.