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
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.
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.