Arthur Mamou-Mani
Arthur Mamou-Mani

Reputation: 2333

Difference between SerialPort.Write() and SerialPort.WriteLine()?

I looked at the SerialPort.Write() and SerialPort.WriteLine() methods on msdn and tried them on a simple code such as the ones below but they seem very similar to me.

Can someone please explain what the main difference is in simple terms?

if (sendtoprint == true)
            {
                for (int i = 0; i < gcode.Count(); i++)
                {
                    port.Write(gcode[i]);
                }

and

if (sendtoprint == true)
            {
                for (int i = 0; i < gcode.Count(); i++)
                {
                    port.WriteLine(gcode[i]);
                }

and

if (sendtoprint == true)
            {
                for (int i = 0; i < gcode.Count(); i++)
                {
                    port.Write(gcode[i]+"\r\n");
                }

Upvotes: 0

Views: 15411

Answers (3)

TheZ
TheZ

Reputation: 3732

From the WriteLine doc, right at the top:

[WriteLine] writes the specified string and the NewLine value to the output buffer.

WriteLine adds the NewLine character to the end of the output whereas Write does not.

So SerialPort.Write("Hello") will output "Hello" to the buffer.

And SerialPort.WriteLine("Hello") will output something like "Hello\n" to the buffer. (Depending on the current newline value)

Upvotes: 5

Jay Patel
Jay Patel

Reputation: 21

I believe the only difference is that the WriteLine method adds the \n so the next data stream will be printed on a new line. This is the same for Console.Write() and Console.WriteLine().

Upvotes: 1

hl2mukkel
hl2mukkel

Reputation: 118

WriteLine appends the specified text and a newline character. Write solely appends the specified text.

For example:

Write("A");
Write("B");
Write("C");

would result in: ABC

however:

WriteLine("A");
WriteLine("B");
WriteLine("C");

would result in:

A
B
C

Upvotes: 6

Related Questions