Serial read loop not reading \n


i new arduino , c. have writeen sketch check, whether year leap year.
in order allow user input have added function read serial monitor, so:

code: [select]
int readline(char str[]) {
  char c;
  int index = 0;
 
  while (true) {
   
    if (serial.available() > 0) {
      c = serial.read();
     
      serial.print("received char: ");
      serial.println(c, hex);
     
      if (c != '\n') {
        serial.print("debug (readline | if > char): ");
        serial.println(c);
        str[index++] = c;
      } else {
        serial.print("debug (readline | if > else): ");
        serial.println(c);
        str[index] = '\0';       // null termination character
        break;
      }
     
      serial.print("debug (readline | str): ");
      serial.println(str);
      serial.print("debug (readline | index): ");
      serial.println(index);
    }
  }
 
  return index;
}


the problem never reach 'else' part when checking new line character.
entering: 2012<enter>, expect see serial.println(c, hex) output as: 32 30 31 32 0d 0a, od being \n terminator.

i have not added input validation or other fancy stuff (and aware sketch loops forever read serial port), wanted understand serial.read build bigger projects.

what doing wrong in sketch?

any appreciated.

iirc ide never sends '\n' trigger know when send.

you try replace \n with  # instead , see if works...


Arduino Forum > Using Arduino > Programming Questions > Serial read loop not reading \n


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support