Sending Carriage Return [ENTER] to the Serial Port

Hello,
I need to send a couple of characters to my device to get it going. I'm able to get the characters sent but I'm having trouble getting the 'carriage return' to be sent.

Referring to http://arduino.cc/en/Reference/ASCIIchart

     int poweron = 111;
     int cr = 13;
     int continuous = 99;
     Serial.write(poweron); // Turn  on 'o'
     delay(1000);
     Serial.write(cr);  // Carriage Return
     delay(1000);
     Serial.write(continuous);  // Activate Continuous Function 'c'
     delay(1000);
     Serial.write(cr); //  Carriage Return
     delay(1000);

If I press ENTER on my keyboard the two characters are sent to the serial terminal.

:roll_eyes:

Thanks for any help

     Serial.write( 'o' ); // Turn  on 'o'
     delay(1000);
     Serial.write( '\r' );  // Carriage Return
     delay(1000);
     Serial.write( 'c' );  // Activate Continuous Function 'c'
     delay(1000);
     Serial.write( '\r' ); //  Carriage Return
     delay(1000);

If I press ENTER on my keyboard the two characters are sent to the serial terminal.

The normal line terminator is CR LF (it's a left over from the dark ages).

Mark

Hmm, still hanging unless I press enter on the keyboard.

delay(1000);
     Serial.write( 'o' ); // Turn on 'o'
     delay(1000);
     Serial.write( '\r' );  // Carriage Return
     delay(1000);
     Serial.write( 'c' );  // Activate Continuous Function 'c'
     delay(1000);
     Serial.write( '\r' ); //  Carriage Return
     delay(1000);

carriage_return_not_working.png

chiques:
Hmm, still hanging unless I press enter on the keyboard.

In order to work correctly, which does it need?

  • \n
  • \r
  • \r\n

They're all plausible. From here we can't guess which it needs. If you don't know which it needs, try them all until you find what works.

Hi all

I want to send character 'R' and carriage return in my program, what is the command to do that?

I want to send character 'R' and carriage return

xxx.print("R\r");

But enter in serial monitor by default does CR LF, not just CR.

septillion:
But enter in serial monitor by default does CR LF, not just CR.

What are you referring to? The original 2013 part of the thread or the 2018 part of the thread :wink:

Damn thread diggers.... Never mind!

The execution of the above codes put o and c on the same line one after another, and the presentation is like: oc. Then --

What does Serial.write('\r') do after the Serial.write('o') command. My understanding is this: the \r (CR = 0x0D) brings the cursor at the beginning of the line; if so, the character c should be written over o; but, this is not.

If we look at the case of dot printer/type writer, we observe that the print-head reaches to the end and then it returns to the beginning position; this is known as Carriage Return. Is this carriage return is similar to the carriage return (CR) represented by 0x0D? After that, the paper goes up one line (auto/manual); this is known as Line Feed. Is this line feed is similar to the line feed (LF) represented by the code 0x0A? Practically, we observe that: \n, ln, and 0x0A do the same job; but, the role of \r (0x0D) is not understood.

Would appreciate to have some better views on the role of:

\r(0x0D)

Would appreciate to have some better views on the role of:

\r(0x0D)

To my knowledge, that depends on the implementation in the terminal program.

What does Serial.write('\r') do after the Serial.write('o') command.

That depends on how the system receiving the data is written. For instance, the Serial monitor does not return the print position to the start of the line on receipt of a carriage return. It is ignored.

Try

void setup() 
{
 Serial.begin(115200);
 Serial.print("Hello");
  Serial.write('\r');   //carriage return
  Serial.print("World");
  Serial.println();   //newline
  Serial.print("Hello");
  Serial.write('\n');   //newline
  Serial.print("World");
}

void loop() 
{
}

More fully featured terminal emulators may (should) behave differently

UKHeliBob:
That depends on how the system receiving the data is written. For instance, the Serial monitor does not return the print position to the start of the line on receipt of a carriage return. It is ignored.

Your codes are executed; yes, the CR (\r) is ignored by the Serial Monitor of IDE. However, it (\r = CR) is not ignored by the LCD Monitor. It prints a non-friendly character between a and b while printing the message char s[4] = {'a', '\r', 'b', '\0'};. This indicates that the Reception Program of the Serial Monitor has been designed to filter out the CR (\r) character.

#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() 
{
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  char s[4] = {'a', '\r', 'b', '\0'};
  lcd.print(s);   //shows: aXb 
  
}

void loop() 
{
  
}