Problem in understanding the differences among Serial.write(0x0D), Serial.write(0x0A), Serial.println(), and Serial.print('\n') commands

1. Serial.write(0x0D) is said to take **Carriage Return" action. That means that the cursor will positioned at the beginning of the current line.

The execution of the following codes does not validate my above understanding. I have printed A and then I have executed Serial.write(0x0D) code and then I have printed B. I expected that the character A will be over-written by character B; instead, A and B have appeared along the same line.

void setup() 
{
  Serial.begin(9600);
  Serial.print('A');
  Serial.write(0x0D);
  Serial.print('B');
}

void loop() {}

Output:

AB

I expected that Serialwrite(0x0D) should bring the curson at the beginning of the current line, but it is not doing it. It looks like that the OutputBox of the Serial Monitor just ignores the command.

2. Serial.write(0x0A) brings the cursor at the beginning of the next (new) line. Does it imply that the said code executes Serial.write(0x0D) at the background to bring the cursor at the beginning of the current line and then pushes down the cursor at the beginning of the next (new) line?

3. Serial.println(), according to documentation, executes the following two codes/frames in the order shown:

Serial.write(0x0D);
Serial.write(0x0A);

Serial.println() brings the cursor at the beginning of the next (new) line which means that it is behaving as Serialwrite(0x0A) behaves. Then are there any difference between Serial.write(0x0A) and Serial.println()?

4. Serial.print('\n') brings the cursor at the beginning of the next (new) line. Is it equivalent to Serial.write(0x0A) or Serial.println()?

Would appreciate to hear better/correct clarifications.

Serial.println()
Serial.print('\n')
and
Serial.write(0xa)
do the same thing.

CR simple ignored by IDE Monitor

Note : fixed mistaped code 0xa instead of 0xd

  1. The term "Carriage return" derives from old printers where a "carriage" prints the letters over a specific location then moves to the right to the next position of the current row. So don't expect the same on a basic serial monitor (but if you use a serial terminal emulator it will "move" the cursor to the first column then the next character will overwrite the existing one).

  2. Same considerations as above, but for "line feed" that made the paper advance 1 row, without moving the printer head (the "carriage").

  3. Serial monitor simply ignores '\r' (Carriage Return, or 0x0D) and goes to the beginning of the next line just on "'\n' (Line feed, or 0x0A).

  4. Yes, it is equivalent to Serial.write(0x0A); but not to Serial.println(); (see previous points)

From MDN web docs

CRLF

CR and LF are control characters or bytecode that can be used to mark a line break in a text file.

  • CR = Carriage Return (\r, 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.
  • LF = Line Feed (\n, 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.

A CR immediately followed by a LF (CRLF, \r\n, or 0x0D0A) moves the cursor down to the next line and then to the beginning of the line.

I have Serial.write(0x0D) and NOT Serial.print(0x0D) which correctly shows 13 on the OutputBox of the Serial Monitor.

I expected that Serialwrite(0x0D) should bring the curson at the beginning of the current line, but it is not doing it. It looks like that the OutputBox of the Serial Monitor just ignores the command.

That's what I said before :wink:

1 Like

yes, that's my mistake, that's what I meant is Serial.write(0xd) - fixed in original post

1 Like

You mean:

Serial.write(0x0A);

Is it Serial.write(0x0D) or Serial.write(0x0A) that is executed first? If Serial.write(0x0D) is executed first, then it brings the cursor at beginning of the current line and then moves the cursor down to the beginning next (new) line. But many posters have said that the Serial Monitor ignores the Serial.write(0x0D) command.

@GolamMostafa!

I know you are only being provocative and rhetorical.

Please repeat your experiments with a real terminal emulator, like CoolTerm or PuTTY and report back…

a7

My context is Arduino Platform which involves, UNO, PC and IDE's Serial Monitor.

Right. The IDE Serial Monitor is… lame. It is not so hard to use a real terminal emulator.

There are lotsa things the Serial Monitor just isn't written to do. Live with it or use an alternate.

a7

2 Likes

If I would not post my thread, probably I would never know that the Serial Monitor simply ignores the Serial.write(0x0D) command.

And yet, it (The Serial Monitor) is helping people to learn C++ Programming and understand AVR Architecture.

You're right, corrected my typo. thanks.

1 Like

Different operating systems use different codes for end of line.
For example, Windows uses \r\n, but Linux and (probably?) Mac - only the '\n'
I meant that is not only the arduino monitor not used CR because it is bad, the CR code is generally an obsolete command and in most cases is not used at all

1 Like

Not exactly. It doesn't ignore Serial.write(0x0D) command, it ignores 0x0D characters aka CR in general, and acts 0x0A ('\n') characters aka LF like they were both CR+LF thus moving to the beginning of next line.
Btw, think it as "Linux-style": text files have always just \n as line terminator, \r\n is a Windows crap...:slight_smile:

1 Like

the IDE Serial Monitor i’d NOT a serial terminal emulator… it simply spits out the ASCII equivalent of most incoming bytes.

Use a real serial terminal package - like PuTTY, or other.

A “dumb” terminal emulator (and the serial monitor certainly qualifies) will frequently include logic to treat “\n”, “\r”, “\r\n”, and “\n\r” all the same: “advance to the beginning of the next line.”
(This avoid unexpected behavior when copying data from one “flavor” of operating system to the “terminal”)

It looks like this occurs deep inside of some Java class that implements something like “display text in a window.”

The main reason that Serial Monitor exists is that it is under control of the IDE. If you have Serial Monitor open and a connection with the Arduino (that you want to upload code to) is open, the IDE will close the port so the upload tool can do the upload without you getting a message that the port is in use.

A second reason is that one does not have to tell a beginner to install another tool (terminal program) for elementary debugging.

Note:
It has nothing to do with understanding AVR architecture.

For example:
To understand the working principles of the Watchdog Timer Module (for example -- a part of architecture) of ATmega328P MCU, a learner performs various experiments on trial-and-error basis. The results are dumped on the OutputBox of the Serial Monitor to asses the progress of learning. So, the Serial Monitor plays an important role (at least to me) in learning the AVR architecture.