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