Go Down

Topic: Sending Carriage Return [ENTER] to the Serial Port (Read 45619 times) previous topic - next topic

chiques

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

Code: [Select]


     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.

:smiley-roll-blue:

Thanks for any help

Coding Badly

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

holmes4

Quote
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

chiques


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



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

Code: [Select]

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);

PeterH


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.

sal_murd

Hi all

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

westfw

Quote
I want to send character 'R' and carriage return
Code: [Select]
xxx.print("R\r");

septillion

But enter in serial monitor by default does CR LF, not just CR.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

sterretje

#8
Jun 13, 2018, 10:38 am Last Edit: Jun 13, 2018, 10:38 am by sterretje
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 ;)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

septillion

Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

GolamMostafa

#10
Jun 13, 2018, 12:49 pm Last Edit: Jun 13, 2018, 02:37 pm by GolamMostafa
Code: [Select]
    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);

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)

sterretje

Quote
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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

UKHeliBob

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


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

GolamMostafa

#13
Jun 13, 2018, 03:29 pm Last Edit: Jun 14, 2018, 01:42 am by GolamMostafa
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.
Code: [Select]
#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()
{
 
}

Go Up