<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=CSharp_-_Serial_Handling_Notes</id>
	<title>CSharp - Serial Handling Notes - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=CSharp_-_Serial_Handling_Notes"/>
	<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=CSharp_-_Serial_Handling_Notes&amp;action=history"/>
	<updated>2026-05-18T17:22:59Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://performiq.com/kb/index.php?title=CSharp_-_Serial_Handling_Notes&amp;diff=3912&amp;oldid=prev</id>
		<title>PeterHarding: Created page with &quot; * http://stackoverflow.com/questions/4661486/not-reading-complete-serial-port-data-in-c-sharp?rq=1  Use the data received event to fill a buffer:  &lt;pre&gt;     private void data...&quot;</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=CSharp_-_Serial_Handling_Notes&amp;diff=3912&amp;oldid=prev"/>
		<updated>2012-11-25T21:35:05Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; * http://stackoverflow.com/questions/4661486/not-reading-complete-serial-port-data-in-c-sharp?rq=1  Use the data received event to fill a buffer:  &amp;lt;pre&amp;gt;     private void data...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
* http://stackoverflow.com/questions/4661486/not-reading-complete-serial-port-data-in-c-sharp?rq=1&lt;br /&gt;
&lt;br /&gt;
Use the data received event to fill a buffer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    private void dataReceived(object sender, SerialDataReceivedEventArgs e) {&lt;br /&gt;
        while (port.BytesToRead &amp;gt; 0){&lt;br /&gt;
            byte[] newBytes = new byte[port.BytesToRead];&lt;br /&gt;
            int LengthRead = port.Read(newBytes, 0, newBytes.Length);&lt;br /&gt;
            Array.Resize(ref newBytes, LengthRead);&lt;br /&gt;
            System.Buffer.BlockCopy(newBytes, 0, buffer, positionInBuffer, newBytes.Length);&lt;br /&gt;
            positionInBuffer += newBytes.Length;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Looping for an expected number of bytes, in this case causing the TimeoutException:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    while (port.BytesToRead &amp;lt; expectedSize)&lt;br /&gt;
    {&lt;br /&gt;
        System.Threading.Thread.Sleep(10);&lt;br /&gt;
        waitingLoop++;&lt;br /&gt;
        if (waitingLoop &amp;gt; TimeOut)             // wait for a 1s timeout&lt;br /&gt;
            throw new TimeoutException();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    newBytes = new byte[expectedSize];&lt;br /&gt;
    LengthRead = port.Read(newBytes, 0, newBytes.Length);&lt;br /&gt;
    if (LengthRead != newBytes.Length)&lt;br /&gt;
        throw new TimeoutException(); // or any exception, doesn&amp;#039;t matter...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:CSharp]]&lt;br /&gt;
[[Category:Dot.NET]]&lt;br /&gt;
[[Category:Serial]]&lt;/div&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
</feed>