-
Notifications
You must be signed in to change notification settings - Fork 200
Reading and Writing byte streams with SerialPortStream
The SerialPortStream class supports the Stream methods. The SerialPortStream is fully buffered, so that read and write operations occur very quickly without any I/O operations. This implies that you can use the Read() and Write() methods and not have to consider asynchronous I/O with BeginRead(), EndRead(), BeginWrite() and EndWrite(). In fact, there is probably no benefit to using asynchronous I/O with the SerialPortStream class.
The SerialPortStream class doesn't throw exceptions on asynchronous I/O, it leaves the implementation to the abstract Stream class.
The standard stream method Read(bytes[], int, int) is implemented. It copies as much data as possible from the internal stream into the buffer you provide. If there is no data in the read buffer, it will wait for at least one byte within the ReadTimeout period. The amount of data read is returned by the method.
You can also read bytes individually with the ReadByte() method. If there is no data available within the timeout period, a value of -1 is returned.
You can check how many bytes are available to read with the BytesToRead property. This defines the number of bytes available that can be returned with a single invocation of the Read method.
The standard stream method Write(bytes[], int, int) is implemented. Streams don't provide a way to know how many bytes were actually written to the stream. So that you can implement a reliable transmission method, the Write() method checks that there is enough free space in the internal write buffer, and will buffer the data, or throw an exception if not enough free space is available within the WriteTimeout property (in milliseconds).
If you attempt to write more bytes than is available as defined by the WriteBufferSize an exception will be immediately thrown.
You can determine how many bytes are available to write in the write buffer indirectly with the BytesToWrite property. The number of bytes available is WriteBufferSize - BytesToWrite.
After you've put data in the write buffer, you can wait for it to be sent over the serial port with the Flush method. This method will wait for the write buffer to be emtpy and for the driver to send the data over the serial port (indicated by the low level EV_TXEMPTY event).
As long as the object is not disposed, the property CanRead returns true. This allows you to later consume all data even if the serial port object has been closed.
It is possible to only write to the serial port while it is open.
You can wait for data to arrive with the timeouts ReadTimeout and WriteTimeout. By using a value of InfiniteTimeout, you disable the timeouts so that timeout operations wait forever. Timeouts are in units of milliseconds.
The ReadTimeout parameter is used with the ReadTo and ReadLine methods (when waiting for a string). The Read methods use the timeout in case there is no data in the read buffer at all. Otherwise data which is available is returned (you should not rely on ReadTimeout to give you count bytes within the timeout period, but rather, simply that at least one byte is available within the ReadTimeout).
The WriteTimeout parameter is used with the Write, WriteLine methods to ensure that data can be completely copied to the internal write buffer within the WriteTimeout parameter.
You can define the internally reserved buffer sizes for the read buffer and write buffer with ReadBufferSize and WriteBufferSize respectively. Unlike the MS implementation, this has no effect on the driver itself. It defines the buffers to be used when buffering read and write requests. The SerialPortStream uses these buffers to send and receive data over the serial port, looping indefinitely while data is available to receive or is required to send.
There are two readonly properties BytesToRead and BytesToSend. Both these properties indicate how much data is in the internal buffers only for receiving and sending. They do not take into account any buffers that might be in the driver of the serial port.