I have found that performing the reads on a separate thread works quite well.
I do that whether I'm communicating over serial, socket, or stream.
Here's the Reader method for my SerialCommunicator:
private void
Reader
(
object Stream
)
{
byte[] buffer = new byte [ this.port.ReadBufferSize ] ;
PIEBALD.Types.StreamReader stream = new StreamReader
( (System.IO.Stream) Stream , buffer.Length ) ;
while ( !this.abort )
{
int len = stream.Read ( buffer , 0 , buffer.Length ) ;
if ( len > 0 )
{
this.RaiseDataReceived ( this.Encoding.GetString ( buffer , 0 , len ) ) ;
}
else
{
System.Threading.Thread.Sleep ( 100 ) ;
}
}
return ;
}
After I open the port I spawn off the Reader thread:
this.stream = this.port.BaseStream ;
this.reader = new System.Threading.Thread(this.Reader)
{
Priority = System.Threading.ThreadPriority.BelowNormal
,
IsBackground = true
} ;
this.reader.Start ( this.stream ) ;
I can't believe I
still haven't finished writing my SerialCommunicator article... :mad: