Go Down

Topic: SerialEvent question (Read 664 times) previous topic - next topic

pak

Apr 19, 2013, 10:48 am Last Edit: Apr 19, 2013, 10:55 am by pak Reason: 1
Hello Dude,
     I'm trying Arduino with SerialEvent() function, but I don't understand the some part of following function written in HardwareSerial.cpp.
Quote
#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,

Quote
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


Nick Gammon

Code: [Select]
[color=red]char inChar

Doesn't make sense.

pak

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

So I have no choice but to use this
Quote
Quote here.







UKHeliBob

Quote
<-- 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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

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

pak

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

Quote
#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

PaulS

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

Yes.

pak

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

Code: [Select]
#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:

Code: [Select]
ISR(USART1_RX_vect)

  unsigned char c = UDR1; // read incoming byte to clear interrupt flag
  UDR1 = c;
  UDR0 = c;
}



Nick Gammon


PaulS

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.

Go Up