|
| | SerialPortStream () |
| | Constructor. Create a stream that doesn't connect to any port. More...
|
| |
| | SerialPortStream (string port) |
| | Constructor. Create a stream that connects to the specified port. More...
|
| |
| | SerialPortStream (string port, int baud) |
| | Constructor. Create a stream that connects to the specified port and sets the initial baud rate. More...
|
| |
| | SerialPortStream (string port, int baud, int data, Parity parity, StopBits stopbits) |
| | Constructor. Create a stream that connects to the specified port with standard parameters. More...
|
| |
| void | GetPortSettings () |
| | Update properties based on the current port, overwriting already existing properties. More...
|
| |
| void | Open () |
| | Opens a new serial port connection. More...
|
| |
| void | OpenDirect () |
| | Opens a new serial port connection with control if the port settings are initialised or not. More...
|
| |
| new void | Close () |
| | Closes the port connection, sets the IsOpen property to false. Does not dispose the object. More...
|
| |
| override int | Read (byte[] buffer, int offset, int count) |
| | Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. More...
|
| |
| override IAsyncResult | BeginRead (byte[] buffer, int offset, int count, AsyncCallback callback, object state) |
| | Begins an asynchronous read operation. More...
|
| |
| override int | EndRead (IAsyncResult asyncResult) |
| | Waits for the pending asynchronous read to complete. More...
|
| |
| override int | ReadByte () |
| | Synchronously reads one byte from the SerialPort input buffer. More...
|
| |
| int | Read (char[] buffer, int offset, int count) |
| | Reads a number of characters from the SerialPortStream input buffer and writes them into an array of characters at a given offset. More...
|
| |
| int | ReadChar () |
| | Synchronously reads one character from the SerialPortStream input buffer. More...
|
| |
| string | ReadLine () |
| | Reads up to the NewLine value in the input buffer. More...
|
| |
| string | ReadTo (string text) |
| | Reads a string up to the specified text in the input buffer. More...
|
| |
| string | ReadExisting () |
| | Reads all immediately available bytes. More...
|
| |
| void | DiscardInBuffer () |
| | Discards data from the serial driver's receive buffer. More...
|
| |
| override void | Flush () |
| | Clears all buffers for this stream and causes any buffered data to be written to the underlying device. More...
|
| |
| override void | Write (byte[] buffer, int offset, int count) |
| | Write the given data into the buffered serial stream for sending over the serial port. More...
|
| |
| override IAsyncResult | BeginWrite (byte[] buffer, int offset, int count, AsyncCallback callback, object state) |
| | Begins an asynchronous write operation. More...
|
| |
| override void | EndWrite (IAsyncResult asyncResult) |
| | Ends an asynchronous write operation. More...
|
| |
| void | Write (char[] buffer, int offset, int count) |
| | Writes a specified number of characters to the serial port using data from a buffer. More...
|
| |
| void | Write (string text) |
| | Writes the specified string to the serial port. More...
|
| |
| void | WriteLine (string text) |
| | Writes the specified string and the NewLine value to the output buffer. More...
|
| |
| void | DiscardOutBuffer () |
| | Discards data from the serial driver's transmit buffer. More...
|
| |
| override long | Seek (long offset, SeekOrigin origin) |
| | This stream does not support seeking. More...
|
| |
| override void | SetLength (long value) |
| | This stream does not support the SetLength property. More...
|
| |
| override string | ToString () |
| | Returns a System.String that represents this instance. More...
|
| |
|
| string | Version [get] |
| | Get the version of this assembly (or components driving this assembly). More...
|
| |
| string | PortName [get, set] |
| | Gets the port for communications, including but not limited to all available COM ports. More...
|
| |
| bool | IsOpen [get] |
| | Gets a value indicating the open or closed status of the SerialPortStream object. More...
|
| |
| Encoding | Encoding [get, set] |
| | Gets or sets the byte encoding for pre- and post-transmission conversion of text. More...
|
| |
| string | NewLine [get, set] |
| | Gets or sets the value used to interpret the end of a call to the ReadLine and WriteLine methods. More...
|
| |
| int | DriverInQueue [get, set] |
| | Specify the driver In Queue at the time it is opened. More...
|
| |
| int | DriverOutQueue [get, set] |
| | Specify the driver Out Queue at the time it is opened. More...
|
| |
| override bool | CanTimeout [get] |
| | Gets a value that determines whether the current stream can time out. More...
|
| |
| override bool | CanRead [get] |
| | Check if this stream supports reading. More...
|
| |
| override int | ReadTimeout [get, set] |
| | Define the time out when reading data from the stream. More...
|
| |
| int | ReadBufferSize [get, set] |
| | Gets or sets the size of the SerialPortStream input buffer. More...
|
| |
| int | ReceivedBytesThreshold [get, set] |
| | Gets or sets the number of bytes in the read buffer before a DataReceived event occurs. More...
|
| |
| int | BytesToRead [get] |
| | Gets the number of bytes of data in the receive buffer. More...
|
| |
| override bool | CanWrite [get] |
| | Check if this stream supports writing. More...
|
| |
| override int | WriteTimeout [get, set] |
| | Define the time out when writing data to the local buffer. More...
|
| |
| int | WriteBufferSize [get, set] |
| | Gets or sets the size of the serial port output buffer. More...
|
| |
| int | BytesToWrite [get] |
| | Gets the number of bytes of data in the send buffer. More...
|
| |
| bool | CDHolding [get] |
| | Gets the state of the Carrier Detect line for the port. More...
|
| |
| bool | CtsHolding [get] |
| | Gets the state of the Clear-to-Send line. More...
|
| |
| bool | DsrHolding [get] |
| | Gets the state of the Data Set Ready (DSR) signal. More...
|
| |
| bool | RingHolding [get] |
| | Gets the state of the Ring line signal. More...
|
| |
| int | BaudRate [get, set] |
| | Gets or sets the serial baud rate. More...
|
| |
| int | DataBits [get, set] |
| | Gets or sets the standard length of data bits per byte. More...
|
| |
| StopBits | StopBits [get, set] |
| | Gets or sets the standard number of stop bits per byte. More...
|
| |
| Parity | Parity [get, set] |
| | Gets or sets the parity-checking protocol. More...
|
| |
| byte | ParityReplace [get, set] |
| | Gets or sets the byte that replaces invalid bytes in a data stream when a parity error occurs. More...
|
| |
| bool | DiscardNull [get, set] |
| | Gets or sets a value indicating whether null bytes are ignored when transmitted between the port and the receive buffer. More...
|
| |
| bool | DtrEnable [get, set] |
| | Gets or sets a value that enables the Data Terminal Ready (DTR) signal during serial communication. More...
|
| |
| bool | RtsEnable [get, set] |
| | Gets or sets a value indicating whether the Request to Send (RTS) signal is enabled during serial communication. More...
|
| |
| Handshake | Handshake [get, set] |
| | Gets or sets the handshaking protocol for serial port transmission of data. More...
|
| |
| int | XOnLimit [get, set] |
| | Define the limit of actual bytes in the transmit buffer when XON is sent. More...
|
| |
| int | XOffLimit [get, set] |
| | Define the limit of free bytes in the buffer before XOFF is sent. More...
|
| |
| bool | TxContinueOnXOff [get, set] |
| | Enable or Disable transmit of data when software flow control is enabled. More...
|
| |
| bool | BreakState [get, set] |
| | Gets or sets the break signal state. More...
|
| |
| override bool | CanSeek [get] |
| | This stream is not seekable, so always returns false. More...
|
| |
| override long | Length [get] |
| | This stream does not support the Length property. More...
|
| |
| override long | Position [get, set] |
| | This stream does not support the Position property. More...
|
| |
| bool | IsDisposed [get] |
| | Indicates if this object has already been disposed. More...
|
| |
The SerialPortStream is a stream class to communicate with serial port based devices.
This implementation is a ground up reimplementation of the Microsoft SerialPort class but one that is a stream. There are numerous small issues with the Microsoft .NET 4.0 implementation (and assumed earlier) that this class attempts to resolve.
For detailed information about serial port programming, refer to the site: http://msdn.microsoft.com/en-us/library/ms810467.aspx
When instantiating.
| void RJCP.IO.Ports.SerialPortStream.GetPortSettings |
( |
| ) |
|
Update properties based on the current port, overwriting already existing properties.
- Exceptions
-
| System.ObjectDisposedException | - Exceptions
-
| System.InvalidOperationException | Serial Port already opened. |
|
This method opens the serial port and retrieves the current settings from Windows. These settings are then made available via the various properties, BaudRate, DataBits, Parity, ParityReplace, Handshake, StopBits, TxContinueOnXoff, DiscardNull, XOnLimit and XOffLimit.
| void RJCP.IO.Ports.SerialPortStream.Open |
( |
| ) |
|
Opens a new serial port connection.
- Exceptions
-
| InvalidOperationException | This object is already managing a serial port connection. |
| System.ObjectDisposedException | SerialPortStream is disposed of. |
Opens a connection to the serial port provided by the constructor or the Port property. If this object is already managing a serial port, this object raises an exception.
When opening the port, only the settings explicitly applied will be given to the port. That is, if you read the default BaudRate as 115200, this value will only be applied if you explicitly set it to 115200. Else the default baud rate of the serial port when its opened will be used.
Normally when you instantiate this stream on a COM port, it is opened for a brief time and queried for the capabilities and default settings. This allows your application to use the settings that were already available (such as defined by the windows user in the Control Panel, or the last open application). If you require to open the COM port without briefly opening it to query its status, then you need to instantiate this object through the default constructor. Set the property UpdateOnPortSet to false and then set the Port property. Provide all the other properties you require then call the Open() method. The port will be opened using the default properties providing you with a consistent environment (independent of the state of the Operating System or the driver beforehand).
| void RJCP.IO.Ports.SerialPortStream.OpenDirect |
( |
| ) |
|
Opens a new serial port connection with control if the port settings are initialised or not.
- Exceptions
-
| System.ObjectDisposedException | SerialPortStream is disposed of. |
| System.InvalidOperationException | Serial Port already opened |
Opens a connection to the serial port provided by the constructor or the Port property. If this object is already managing a serial port, this object raises an exception.
You can override the open so that no communication settings are retrieved or set. This is useful for virtual COM ports that do not manage state bits (some as some emulated COM ports or USB based communications that present themselves as a COM port but do not have any underlying physical RS232 implementation).
<note type="note">If you use this method to avoid setting parameters for the serial port, instead only to open the serial port, you should be careful not to set any properties associated with the serial port, as they will set the communications properties.</note>
| string RJCP.IO.Ports.SerialPortStream.ReadExisting |
( |
| ) |
|
Reads all immediately available bytes.
Reads all data in the current buffer. If there is no data available, then no data is returned. This is different to the Microsoft implementation, that will read all data, and if there is no data, then it waits for data based on the time outs. This method employs no time outs.
Because this method returns only the data that is currently in the cached buffer and ignores the data that is actually buffered by the driver itself, there may be a slight discrepancy between the value returned by BytesToRead and the actual length of the string returned.
This method differs slightly from the Microsoft implementation in that this function doesn't initiate a read operation, as we have a dedicated thread to reading data that is running independently.
- Returns
- The contents of the stream and the input buffer of the SerialPortStream.
| string RJCP.IO.Ports.SerialPortStream.ReadTo |
( |
string |
text | ) |
|
Reads a string up to the specified text in the input buffer.
The ReadTo() function will read text from the byte buffer up to a predetermined limit (1024 characters) when looking for the string text. If text is not found within this limit, data is thrown away and more data is read (effectively consuming the earlier bytes).
This method is provided as compatibility with the Microsoft implementation. There are some important differences however. This method attempts to fix a minor pathological problem with the Microsoft implementation. If the string text is not found, the MS implementation may modify the internal state of the decoder. As a workaround, it pushes all decoded characters back into its internal byte buffer, which fixes the problem that a second call to the ReadTo() method returns the consistent results, but a call to Read(byte[], ..) may return data that was not actually transmitted by the DCE. This would happen in case that an invalid byte sequence was found, converted to a fall back character. The original byte sequence is removed and replaced with the byte equivalent of the fall back character.
This method is rather slow, because it tries to preserve the byte buffer in case of failure.
In case the data cannot be read, an exception is always thrown. So you may assume that if this method returns, you have valid data.
- Parameters
-
| text | The text to indicate where the read operation stops. |
- Returns
- The contents of the input buffer up to the specified text.
- Exceptions
-
| TimeoutException | Data was not available in the timeout specified. |
| IOException | Device Error (e.g. device removed). |
| ObjectDisposedException | |
| override void RJCP.IO.Ports.SerialPortStream.Write |
( |
byte [] |
buffer, |
|
|
int |
offset, |
|
|
int |
count |
|
) |
| |
Write the given data into the buffered serial stream for sending over the serial port.
- Parameters
-
| buffer | The buffer containing data to send. |
| offset | Offset into the array buffer where data begins. |
| count | Number of bytes to copy into the local buffer. |
- Exceptions
-
| System.TimeoutException | Not enough buffer space was made available before the time out expired. |
| System.ObjectDisposedException | Object is disposed, or disposed during flush operation. |
| System.ArgumentNullException | NULL buffer was provided. |
| System.ArgumentOutOfRangeException | Negative offset or negative count provided. |
| System.ArgumentException | Offset and count exceed buffer boundaries. |
| System.InvalidOperationException | Serial port not open. |
| System.IO.IOException | Serial Port was closed during the flush operation; or there was a device error. |
Data is copied from the array provided into the local stream buffer. It does not guarantee that data will be sent over the serial port. So long as there is enough local buffer space to accept the write of count bytes, this function will succeed. In case that the buffered serial stream doesn't have enough data, the function will wait up to WriteTimeout milliseconds for enough buffer data to become available. In case that there is not enough space before the write time out expires, no data is copied to the local stream and the function fails with an exception.
For reliability, this function will only write data to the write buffer if the complete set of data requested can be written. This implies that the parameter count be less or equal to the number of bytes that are available in the write buffer. Equivalently, you must make sure that you have a write buffer with at least count allocated bytes or this function will always raise an exception.
| int RJCP.IO.Ports.SerialPortStream.BytesToRead |
|
get |
Gets the number of bytes of data in the receive buffer.
This method returns the number of bytes available in the input read buffer. Bytes that are cached by the driver itself are not accounted for, as they haven't yet been read by the local thread.
This has the effect, that if the local buffer is full (let's say that it is arbitrarily picked to be 64KB) and the local driver also has buffered 4KB, only the size of the local buffer is given, so 64KB (instead of the expected 68KB).
| int RJCP.IO.Ports.SerialPortStream.DataBits |
|
getset |
Gets or sets the standard length of data bits per byte.
The range of values for this property is from 5 through 8 and 16. A check is made by setting this property against the advertised capabilities of the driver. That is, a driver lists its capabilities to say what byte sizes it can support. If the driver cannot support the byte size requested, an exception is raised.
Not all possible combinations are allowed by all drivers. That implies, that an exception may be raised for a valid setting of the DataBits property, if the other parameters are not valid. Such an example might be that 5-bits are only supported with 2 stop bits and not otherwise. The driver itself will raise an exception to the application in this case.
If the serial driver doesn't support setting the data bits, setting this property is silently ignored and the number of data bits isn't updated.
| Handshake RJCP.IO.Ports.SerialPortStream.Handshake |
|
getset |
Gets or sets the handshaking protocol for serial port transmission of data.
Enables handshaking on the serial port. We support three types of handshaking: RTS/CTS; XON/XOFF; and DTR/DTS. The Microsoft implementation SerialPort only supports RTS/CTS and XON/XOFF.
This method is not 100% compatible with the Microsoft implementation. By disabling RTS flow control, it sets behaviour so that the RTS line is enabled. You must set the property RtsEnable as appropriate. Although the Microsoft implementation doesn't support DSR/DTR at all, setting this property will also set the DTR line to enabled. You must set the property DtrEnable as appropriate.
When enabling DTR flow control, the DsrSensitivity flag is set, so the driver ignores any bytes received, unless the DSR modem input line is high. Otherwise, if DTR flow control is disabled, the DSR line is ignored. For more detailed information about how windows works with flow control, see the site: http://msdn.microsoft.com/en-us/library/ms810467.aspx. When DTR flow control is specified, the fOutxDsrFlow is set along with fDsrSensitivity.
Note, the windows feature RTS_CONTROL_TOGGLE is not supported by this class. This is also not supported by the Microsoft implementation.
| string RJCP.IO.Ports.SerialPortStream.PortName |
|
getset |
Gets the port for communications, including but not limited to all available COM ports.
A list of valid port names can be obtained using the GetPortNames method.
When changing the port name, and the property UpdateOnPortSet is true, setting this property will cause the port to be opened, status read and the port then closed. Thus, you can use this behaviour to determine the actual settings of the port (which remain constant until a program actually changes the port settings).
Setting this property to itself, while having UpdateOnPortSet to true has the effect of updating the local properties based on the current port settings.
| override int RJCP.IO.Ports.SerialPortStream.ReadTimeout |
|
getset |
Define the time out when reading data from the stream.
This defines the time out when data arrives in the buffered memory of this stream, that is, when the driver indicates that data has arrived to the application.
Should the user perform a read operation and no data is available to copy in the buffer, a time out will occur.
Set this property to InfiniteTimeout for an infinite time out.
| bool RJCP.IO.Ports.SerialPortStream.TxContinueOnXOff |
|
getset |
Enable or Disable transmit of data when software flow control is enabled.
MSDN documentation states this flag as follows:
If this member is TRUE, transmission continues after the input buffer has come within XOffLimit bytes of being full and the driver has transmitted the XoffChar character to stop receiving bytes. If this member is FALSE, transmission does not continue until the input buffer is within XonLim bytes of being empty and the driver has transmitted the XonChar character to resume reception.
When the driver buffer fills up and sends the XOFF character (and software flow control is active), this property defines if the driver should continue to send data over the serial port or not.
The Microsoft SerialPort implementation doesn't provide this option (in fact, in .NET 4.0 it doesn't appear to control this at all).
Some DCE devices will resume sending after any character arrives. The TxContinueOnXOff member should be set to FALSE when communicating with a DCE device that resumes sending after any character arrives.
| override int RJCP.IO.Ports.SerialPortStream.WriteTimeout |
|
getset |
Define the time out when writing data to the local buffer.
This defines the time out when writing data to the local buffer. No guarantees are given to when the data will actually be transferred over to the serial port as this is dependent on the hardware configuration and flow control.
When writing data to the stream buffer, a time out will occur if not all data can be written to the local buffer and the buffer wasn't able to empty itself in the period given by the time out.
Naturally then, this depends on the size of the send buffer in use, how much data is already in the buffer, how fast the data can leave the buffer.
In case the data cannot be written to the buffer in the given time out, no data will be written at all.