C# Class PInvokeSerialPort.SerialPort

PInvokeSerialPort main class. Borrowed from http://msdn.microsoft.com/en-us/magazine/cc301786.aspx ;)
Inheritance: IDisposable
显示文件 Open project: ebraminio/PInvokeSerialPort Class Usage Examples

Public Properties

Property Type Description
AutoReopen bool
BaudRate int
CheckAllSends bool
DataBits int
Parity Parity
RxFlowX bool
RxGateDsr bool
RxHighWater int
RxLowWater int
RxQueue int
SendTimeoutConstant int
SendTimeoutMultiplier int
StopBits StopBits
TxFlowCts bool
TxFlowDsr bool
TxFlowX bool
TxQueue int
TxWhenRxXoff bool
UseDtr HsOutput
UseRts HsOutput
XoffChar ASCII
XonChar ASCII

Public Methods

Method Description
Close ( ) : void

Closes the com port.

Dispose ( ) : void

For IDisposable

Flush ( ) : void

Block until all bytes in the queue have been transmitted.

Open ( ) : bool

Opens the com port and configures it with the required settings

SendImmediate ( byte tosend ) : void

Sends a protocol byte immediately ahead of any queued bytes.

SerialPort ( string portName ) : System

Class contructor

SerialPort ( string portName, int baudRate ) : System

Class contructor

Write ( byte toSend ) : void

Queues bytes for transmission.

Write ( char toSend ) : void

Queues a single char for transmission.

Write ( string toSend ) : void

Queues string for transmission.

WriteLine ( string toSend ) : void

Queues string with a new line ("\r\n") for transmission.

Protected Methods

Method Description
AfterOpen ( ) : bool

Override this to provide processing after the port is openned (i.e. to configure remote device or just check presence).

BeforeClose ( bool error ) : void

Override this to provide processing prior to port closure.

GetModemStatus ( ) : ModemStatus

Gets the status of the modem control input signals.

GetQueueStatus ( ) : QueueStatus

Get the status of the queues

OnBreak ( ) : void

Override this to take action when a break condition is detected on the input line.

OnRing ( ) : void

Override this to take action when a ring condition is signalled by an attached modem.

OnRxChar ( byte ch ) : void

Override this to process received bytes.

OnRxException ( Exception e ) : void

Override this to take action when the reception thread closes due to an exception being thrown.

OnStatusChange ( ModemStatus mask, ModemStatus state ) : void

Override this to take action when one or more modem status inputs change state

OnTxDone ( ) : void

Override this to take action when transmission is complete (i.e. all bytes have actually been sent, not just queued).

Sleep ( int milliseconds ) : void

Delay processing.

ThrowException ( string reason ) : void

Use this to throw exceptions in derived classes. Correctly handles threading issues and closes the port if necessary.

Private Methods

Method Description
CheckOnline ( ) : bool
CheckResult ( ) : void
InternalClose ( ) : void
ReceiveThread ( ) : void

Method Details

AfterOpen() protected method

Override this to provide processing after the port is openned (i.e. to configure remote device or just check presence).
protected AfterOpen ( ) : bool
return bool

BeforeClose() protected method

Override this to provide processing prior to port closure.
protected BeforeClose ( bool error ) : void
error bool True if closing due to an error
return void

Close() public method

Closes the com port.
public Close ( ) : void
return void

Dispose() public method

For IDisposable
public Dispose ( ) : void
return void

Flush() public method

Block until all bytes in the queue have been transmitted.
public Flush ( ) : void
return void

GetModemStatus() protected method

Gets the status of the modem control input signals.
protected GetModemStatus ( ) : ModemStatus
return ModemStatus

GetQueueStatus() protected method

Get the status of the queues
protected GetQueueStatus ( ) : QueueStatus
return QueueStatus

OnBreak() protected method

Override this to take action when a break condition is detected on the input line.
protected OnBreak ( ) : void
return void

OnRing() protected method

Override this to take action when a ring condition is signalled by an attached modem.
protected OnRing ( ) : void
return void

OnRxChar() protected method

Override this to process received bytes.
protected OnRxChar ( byte ch ) : void
ch byte The byte that was received
return void

OnRxException() protected method

Override this to take action when the reception thread closes due to an exception being thrown.
protected OnRxException ( Exception e ) : void
e System.Exception The exception which was thrown
return void

OnStatusChange() protected method

Override this to take action when one or more modem status inputs change state
protected OnStatusChange ( ModemStatus mask, ModemStatus state ) : void
mask ModemStatus The status inputs that have changed state
state ModemStatus The state of the status inputs
return void

OnTxDone() protected method

Override this to take action when transmission is complete (i.e. all bytes have actually been sent, not just queued).
protected OnTxDone ( ) : void
return void

Open() public method

Opens the com port and configures it with the required settings
public Open ( ) : bool
return bool

SendImmediate() public method

Sends a protocol byte immediately ahead of any queued bytes.
public SendImmediate ( byte tosend ) : void
tosend byte Byte to send
return void

SerialPort() public method

Class contructor
public SerialPort ( string portName ) : System
portName string
return System

SerialPort() public method

Class contructor
public SerialPort ( string portName, int baudRate ) : System
portName string
baudRate int
return System

Sleep() protected method

Delay processing.
protected Sleep ( int milliseconds ) : void
milliseconds int Milliseconds to delay by
return void

ThrowException() protected method

Use this to throw exceptions in derived classes. Correctly handles threading issues and closes the port if necessary.
protected ThrowException ( string reason ) : void
reason string Description of fault
return void

Write() public method

Queues bytes for transmission.
public Write ( byte toSend ) : void
toSend byte Array of bytes to be sent
return void

Write() public method

Queues a single char for transmission.
public Write ( char toSend ) : void
toSend char Byte to be sent
return void

Write() public method

Queues string for transmission.
public Write ( string toSend ) : void
toSend string Array of bytes to be sent
return void

WriteLine() public method

Queues string with a new line ("\r\n") for transmission.
public WriteLine ( string toSend ) : void
toSend string Array of bytes to be sent
return void

Property Details

AutoReopen public_oe property

If true, the port will automatically re-open on next send if it was previously closed due to an error (default: false)
public bool AutoReopen
return bool

BaudRate public_oe property

Baud Rate (default: 2400) unsupported rates will throw "Bad settings"
public int BaudRate
return int

CheckAllSends public_oe property

If true, subsequent Send commands wait for completion of earlier ones enabling the results to be checked. If false, errors, including timeouts, may not be detected, but performance may be better.
public bool CheckAllSends
return bool

DataBits public_oe property

Number of databits 1..8 (default: 8) unsupported values will throw "Bad settings"
public int DataBits
return int

Parity public_oe property

The parity checking scheme (default: none)
public Parity Parity
return Parity

RxFlowX public_oe property

If true, Xon and Xoff characters are sent to control the data flow from the remote station (default: false)
public bool RxFlowX
return bool

RxGateDsr public_oe property

If true, received characters are ignored unless DSR is asserted by the remote station (default: false)
public bool RxGateDsr
return bool

RxHighWater public_oe property

The number of free bytes in the reception queue at which flow is disabled (default: 2048)
public int RxHighWater
return int

RxLowWater public_oe property

The number of bytes in the reception queue at which flow is re-enabled (default: 512)
public int RxLowWater
return int

RxQueue public_oe property

Requested size for receive queue (default: 0 = use operating system default)
public int RxQueue
return int

SendTimeoutConstant public_oe property

Constant. Max time for Send in ms = (Multiplier * Characters) + Constant (default: 0)
public int SendTimeoutConstant
return int

SendTimeoutMultiplier public_oe property

Multiplier. Max time for Send in ms = (Multiplier * Characters) + Constant (default: 0 = No timeout)
public int SendTimeoutMultiplier
return int

StopBits public_oe property

Number of stop bits (default: one)
public StopBits StopBits
return StopBits

TxFlowCts public_oe property

If true, transmission is halted unless CTS is asserted by the remote station (default: false)
public bool TxFlowCts
return bool

TxFlowDsr public_oe property

If true, transmission is halted unless DSR is asserted by the remote station (default: false)
public bool TxFlowDsr
return bool

TxFlowX public_oe property

If true, transmission is halted when Xoff is received and restarted when Xon is received (default: false)
public bool TxFlowX
return bool

TxQueue public_oe property

Requested size for transmit queue (default: 0 = use operating system default)
public int TxQueue
return int

TxWhenRxXoff public_oe property

If false, transmission is suspended when this station has sent Xoff to the remote station (default: true) Set false if the remote station treats any character as an Xon.
public bool TxWhenRxXoff
return bool

UseDtr public_oe property

Specidies the use to which the DTR output is put (default: none)
public HsOutput UseDtr
return HsOutput

UseRts public_oe property

Specifies the use to which the RTS output is put (default: none)
public HsOutput UseRts
return HsOutput

XoffChar public_oe property

The character used to signal Xoff for X flow control (default: DC3)
public ASCII XoffChar
return ASCII

XonChar public_oe property

The character used to signal Xon for X flow control (default: DC1)
public ASCII XonChar
return ASCII