title: How to set the ReadBufferSize on .NET serial port
body: <p>So, I am creating a VB.NET program that listens to a serial port.  I was using this</p>
  
  <pre><code>Dim WithEvents ptr_SerialPort As System.IO.Ports.SerialPort
  ...
   ptr_SerialPort = My.Computer.Ports.OpenSerialPort(serialportname, baudrate,parity, databits, stopbits)
  </code></pre>
  
  <p>which worked fine until i realized that I am overrunning my SerialPort.ReadBufferSize property, which is 4096, and losing data:</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readbuffersize(v=vs.110).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readbuffersize(v=vs.110).aspx</a></p>
  
  <p>So I tried to increase the size of the buffer as follows</p>
  
  <pre><code>ptr_SerialPort = My.Computer.Ports.OpenSerialPort(serialportname, baudrate, parity, databits, stopbits)
  ptr_SerialPort.ReadBufferSize = 2000000
  </code></pre>
  
  <p>But I get the error "can't set the buffer size while it's open".  Which is reasonable, so I tried this:</p>
  
  <pre><code>ptr_SerialPort = New System.IO.Ports.SerialPort(serialportname, baudrate, parity, databits, stopbits)
  ptr_SerialPort.ReadBufferSize = 2000000
  ptr_SerialPort.Open()
  </code></pre>
  
  <p>Well, this results in a Serial port that does NOT receive data... I guess that My.Computer.Ports.OpenSerialPort is doing more than just instantiating it, and what I have above is a serial port object that is not connected to my hardware.  So, my question is... how do I open a serial port on my PC, <em>and</em> specify the ReadBufferSize property??</p>
  
  <p>Thanks very much!</p>