5

If creating a serial port with the default constructor or parameterless constructor, the port is given default values. From the documentation:

// Create a new SerialPort object with default settings.
_serialPort = new SerialPort();

This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1.

There are several constructors

enter image description here

When using a constructor with parameters:
Are the attributes that are not within the parameters of the constructor of the serial port given the same default values when using a constructor with parameters and when using the default constructor?

The problem in this question, the interesting answers, comments and subsequent chat discussion let me to ask this.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Take a look at its [source code](http://referencesource.microsoft.com/#System/sys/system/io/ports/SerialPort.cs,0288da67ff57609d) – Reza Aghaei Sep 02 '16 at 17:30
  • All important properties including `BaudRate`, `DataBits`, `Parity` and `StopBits` have default values which if they are not present in constructor arguments, the default value will be used instead. – Reza Aghaei Sep 02 '16 at 17:35
  • 2
    Reza provided the relevant code, shows that the super-duper important HandShake property does **not** have a default. This was a big mistake and got lots of programmers in trouble since very few understand what it does and it always matters. If you don't set it then it stays at the same value used by whatever previous program used, leading to the common observation that it "suddenly works" when you use a terminal emulator first. It is especially ruinous when you use the Winforms designer to drop the component on the form, now it is the current setting on the dev machine. – Hans Passant May 05 '19 at 03:53
  • 2
    But more to the point, the existing defaults just don't mean much of anything. You must match the device settings and manufacturers have very few reasons to pay attention to .NET defaults. Rather the opposite, they consider vendor tie-in a feature and not a bug. Most notorious examples of this are MIDI (impossible baudrate) and coin vending machines (9 bit bytes), they want to sell you the hardware as well. – Hans Passant May 05 '19 at 03:57
  • @HansPassant would you consider writing an answer? –  May 05 '19 at 04:35

1 Answers1

8

You can take a look at its source code and see constructors and filed definitions.

You will see all important properties including PortName, BaudRate, DataBits, Parity and StopBits have a default value which if they are not present in constructor arguments, the default value will be used for them.

Also some other important properties they have default values as well, while they are not present in constructors, however in some cases their default value may not be good.

For example for HandShake the default value is Handshake.None; while You may want to set it to Handshake.XOnXOff, Handshake.RequestToSend or Handshake.RequestToSendXOnXOff. For this particular property, you may want to refer to Hans's comments.

Take a look at constructors:

public SerialPort(System.ComponentModel.IContainer container)
{
    container.Add(this);
}
public SerialPort()
{
}
// Non-design SerialPort constructors here chain, 
//using default values for members left unspecified by parameters
public SerialPort(string portName) 
    : this (portName, defaultBaudRate, defaultParity, defaultDataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate) 
    : this (portName, baudRate, defaultParity, defaultDataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate, Parity parity)
    : this (portName, baudRate, parity, defaultDataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate, Parity parity, int dataBits)
    : this (portName, baudRate, parity, dataBits, defaultStopBits)
{
}
public SerialPort(string portName, int baudRate, Parity parity, 
     int dataBits, StopBits stopBits)
{
    this.PortName = portName;
    this.BaudRate = baudRate;
    this.Parity = parity;
    this.DataBits = dataBits;
    this.StopBits = stopBits;
}

And here are those fields and default value definitions:

// ---------- default values -------------*

private const int defaultDataBits = 8;
private const Parity defaultParity = Parity.None;
private const StopBits defaultStopBits = StopBits.One;
private const Handshake defaultHandshake = Handshake.None;
private const int defaultBufferSize = 1024;
private const string defaultPortName = "COM1";
private const int defaultBaudRate = 9600;
private const bool defaultDtrEnable = false;
private const bool defaultRtsEnable = false;
private const bool defaultDiscardNull = false;
private const byte defaultParityReplace = (byte) '?';
private const int defaultReceivedBytesThreshold = 1;
private const int defaultReadTimeout = SerialPort.InfiniteTimeout;
private const int defaultWriteTimeout = SerialPort.InfiniteTimeout;
private const int defaultReadBufferSize = 4096;
private const int defaultWriteBufferSize = 2048;
private const int maxDataBits = 8;
private const int minDataBits = 5;
private const string defaultNewLine = "\n";

private const string SERIAL_NAME = @"\Device\Serial";
// --------- members supporting exposed properties ------------*
private int baudRate = defaultBaudRate;
private int dataBits = defaultDataBits;
private Parity parity = defaultParity;
private StopBits stopBits = defaultStopBits;
private string portName = defaultPortName;
// ASCII is default encoding for modem communication, etc.
private Encoding encoding = System.Text.Encoding.ASCII; 
private Decoder decoder = System.Text.Encoding.ASCII.GetDecoder();
private int maxByteCountForSingleChar = System.Text.Encoding.ASCII.GetMaxByteCount(1);
private Handshake handshake = defaultHandshake;
private int readTimeout = defaultReadTimeout;
private int writeTimeout = defaultWriteTimeout;
private int receivedBytesThreshold = defaultReceivedBytesThreshold;
private bool discardNull = defaultDiscardNull;
private bool dtrEnable = defaultDtrEnable;
private bool rtsEnable = defaultRtsEnable;
private byte parityReplace = defaultParityReplace;
private string newLine = defaultNewLine;
private int readBufferSize = defaultReadBufferSize;
private int writeBufferSize = defaultWriteBufferSize;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398