Pages: [1]   Go Down
Author Topic: SerialEvent question  (Read 611 times)
0 Members and 1 Guest are viewing this topic.
Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: April 19, 2013, 03:55:31 am by pak » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
[color=red]char inChar

Doesn't make sense.
Logged


Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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




 
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 49
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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:
ISR(USART1_RX_vect)

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

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/serial
Logged


Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 616
Posts: 49433
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: