📄 serialport.cs
字号:
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: SerialPort
**
** Purpose: SerialPort wraps an internal SerialStream class,
** : providing a high but complete level of Serial Port I/O functionality
** : over the handle/Win32 object level of the SerialStream.
**
**
** Date: August 2002
**
===========================================================*/
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace System.IO.Ports
{
public class SerialPort : System.ComponentModel.Component
{
public const int InfiniteTimeout = -1;
// ---------- 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 SerialEvents defaultEventFilter = SerialEvents.All;
private const int defaultReceivedBytesThreshold = 1;
private const int defaultReadTimeout = SerialPort.InfiniteTimeout;
private const int defaultWriteTimeout = SerialPort.InfiniteTimeout;
private const int maxDataBits = 8;
private const int minDataBits = 5;
// --------- members supporting exposed properties ------------*
private int baudRate = defaultBaudRate;
private int dataBits = defaultDataBits;
private Parity parity = defaultParity;
private StopBits stopBits = defaultStopBits;
private string portName = defaultPortName;
private Encoding encoding = new ASCIIEncoding(); // ASCII is default encoding for modem communication, etc.
private Handshake handshake = defaultHandshake;
private int readTimeout = defaultReadTimeout;
private int writeTimeout = defaultWriteTimeout;
private SerialEvents eventFilter = defaultEventFilter;
private int receivedBytesThreshold = defaultReceivedBytesThreshold;
private bool discardNull = defaultDiscardNull;
private bool isOpen = false;
private bool inBreak = false;
private bool dtrEnable = defaultDtrEnable;
private bool rtsEnable = defaultRtsEnable;
private byte parityReplace = defaultParityReplace;
// ---------- members for internal support ---------*
private SerialStream internalSerialStream = null;
private byte[] inBuffer = new byte[defaultBufferSize];
private int readPos = 0; // position of next byte to read in the read buffer. readPos <= readLen
private int readLen = 0; // position of first unreadable byte => readLen - readPos is the number of readable bytes left.
private char[] oneChar = new char[1];
// ------ event members ------------------*
//public event EventHandler Disposed;
public event SerialEventHandler ErrorEvent;
public event SerialEventHandler PinChangedEvent;
public event SerialEventHandler ReceivedEvent;
//--- component properties---------------*
// ---- SECTION: public properties --------------*
// Note: information about port properties passes in ONE direction: from SerialPort to
// its underlying Stream. No changes are able to be made in the important properties of
// the stream and its behavior, so no reflection back to SerialPort is necessary.
// Gets the internal SerialStream object. Used to pass essence of SerialPort to another Stream wrapper.
public Stream BaseStream
{
get { return internalSerialStream; }
}
[Browsable(true),
DefaultValue(defaultBaudRate),
Description("The maximum baud rate at which to set the serial driver.")]
public int BaudRate
{
get { return baudRate; }
set {
if (isOpen)
internalSerialStream.BaudRate = value;
baudRate = value;
}
}
public bool CDHolding
{
get
{
if (!isOpen)
throw new InvalidOperationException("CDHolding - port not open");
return internalSerialStream.CDHolding;
}
}
public bool CtsHolding
{
get
{
if (!isOpen)
throw new InvalidOperationException("CtsHolding - port not open");
return internalSerialStream.CtsHolding;
}
}
[Browsable(true),
DefaultValue(defaultDataBits),
Description("The number of data bits per transmitted/received byte.")]
public int DataBits
{
get
{ return dataBits; }
set
{
if (isOpen)
internalSerialStream.DataBits = value;
dataBits = value;
}
}
[Browsable(true),
DefaultValue(defaultDiscardNull),
Description("Whether to discard null bytes received on the port before adding to serial buffer.")]
public bool DiscardNull
{
get
{
return discardNull;
}
set
{
if (isOpen)
internalSerialStream.DiscardNull = value;
discardNull = value;
}
}
public bool DsrHolding
{
get
{
if (!isOpen)
throw new InvalidOperationException("DsrHolding - port not open");
return internalSerialStream.DsrHolding;
}
}
[Browsable(true),
DefaultValue(defaultDtrEnable),
Description("Whether to enable the Data Terminal Ready (DTR) line during communications.")]
public bool DtrEnable
{
get { return dtrEnable; }
set
{
if (isOpen)
internalSerialStream.DtrEnable = value;
dtrEnable = value;
}
}
// Allows specification of an arbitrary encoding for the reading and writing functions of the port
// which deal with chars and strings. Set by default in the code to System.Text.ASCIIEncoding(), which
// is the standard text encoding for modem commands and most of serial communication.
// Clearly not designable.
public Encoding Encoding
{
get { return encoding; }
set { encoding = value; }
}
// Indicates which event-triggers we choose to acknowledge in the event-driven model provided.
[Browsable(true),
DefaultValue(defaultEventFilter),
Description("Flag enum of SerialEvents indicating the selected conditions on which to fire events.")]
public SerialEvents EventFilter
{
get { return eventFilter; }
set { eventFilter = value; }
}
[Browsable(true),
DefaultValue(defaultHandshake),
Description("The handshaking protocol for flow control in data exchange, which can be None.")]
public Handshake Handshake
{
get
{
return handshake;
}
set
{
if (isOpen)
internalSerialStream.Handshake = value;
handshake = value;
}
}
public bool InBreak
{
get
{
return inBreak;
}
}
// includes all bytes available on serial driver's input buffer as well as bytes internally buffered int the SerialPort class.
public int InBufferBytes
{
get
{
if (!isOpen)
throw new InvalidOperationException("InBufferBytes - port not open");
return internalSerialStream.InBufferBytes + readLen - readPos; // count the number of bytes we have in the internal buffer too.
}
}
// true only if the Open() method successfully called on this SerialPort object, without Close() being called more recently.
public bool IsOpen
{
get { return isOpen; }
}
// includes all bytes available on serial driver's output buffer. Note that we do not internally buffer output bytes in SerialPort.
public int OutBufferBytes
{
get
{
if (!isOpen)
throw new InvalidOperationException("OutBufferBytes - port not open");
return internalSerialStream.OutBufferBytes;
}
}
[Browsable(true),
DefaultValue(defaultParity),
Description("The scheme for parity checking each received byte and marking each transmitted byte.")]
public Parity Parity
{
get
{
return parity;
}
set
{
if (isOpen)
internalSerialStream.Parity = parity;
parity = value;
}
}
[Browsable(true),
DefaultValue(defaultParityReplace),
Description("Byte with which to replace bytes received with parity errors.")]
public byte ParityReplace
{
get { return parityReplace; }
set
{
if (isOpen)
internalSerialStream.ParityReplace = value;
parityReplace = value;
}
}
// Note that the communications port cannot be meaningfully re-set when the port is open,
// and so once set by the constructor becomes read-only.
[Browsable(true),
DefaultValue(defaultPortName),
Description("The string indicating the communications resource to open, e.g. \"COM2\".")]
public string PortName
{
get
{ return portName; }
set
{
if (isOpen)
throw new InvalidOperationException("PortName - port open");
portName = value;
}
}
// timeout for all read operations. May be set to SerialPort.InfiniteTimeout, 0, or any positive value
[Browsable(true),
DefaultValue(SerialPort.InfiniteTimeout),
Description("Milliseconds after any read operation starts before timeout if no data received.")]
public int ReadTimeout
{
get
{
return readTimeout;
}
set
{
if (isOpen)
internalSerialStream.ReadTimeout = value;
readTimeout = value;
}
}
[Browsable(true),
DefaultValue(defaultReceivedBytesThreshold),
Description("Number of bytes required to be available before the Read event is fired.")]
// If we have the ReceivedChars event set, this property indicates the number of bytes necessary
// to exist in our buffers before the event is thrown. This is useful if we expect to receive n-byte
// packets and can only act when we have this many, etc.
public int ReceivedBytesThreshold
{
get
{
return receivedBytesThreshold;
}
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("receivedBytesThreshold",
InternalResources.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
receivedBytesThreshold = value;
}
}
[Browsable(true),
DefaultValue(defaultRtsEnable),
Description("Whether to enable the Request To Send (RTS) line during communications.")]
public bool RtsEnable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -