title: Error opening serial port using the System.IO.Ports.SerialPort class
body: <p>I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class. I dragged the serial port from the tool pane (Visual&nbsp;Studio&nbsp;2008) on to my Windows Forms application.</p>
  
  <p>I've got a property grid set up so I can easily change properties of the serial port at runtime. When I try to open the port, I get the error shown below. I don't understand why because I can open and read from the port using Hyperterminal.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>System.IO.IOException Error connection: A device attached to the system is not functioning
     at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
     at System.IO.Ports.InternalResources.WinIOError()
     at System.IO.Ports.SerialStream.set_DtrEnable(Boolean value)
     at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
     at System.IO.Ports.SerialPort.Open()
     at Test.CardReader.frmMain.Connect() in D:\Develop\2.0\Projects\Kiosk\EmbeddedBrowser\Windows Forms\Test.CardReader\Form1.cs:line 166
  </code></pre>
  
  <p>Note, this is a USB connection, so it's actually a virtual serial port.</p>
  
  <p>My settings are as follows:<br/></p>
  
  <p>BaudRate = 9600 <br/>
  DataBits = 8 <br/>
  DiscardNull = false <br/>
  DtrEnable = false <br/>
  Handshake = None <br/>
  Parity = None <br/>
  ParityReplace = 63 <br/>
  PortName = COM3 <br/>
  ReadBufferSize = 4096 <br/>
  ReadTimeout = -1 <br/>
  ReceivedBytes = 1 <br/>
  TrsEnable = False <br/>
  StopBits = One <br/>
  WriteBufferSize = 2048 <br/>
  WriteTimeout = -1<br/></p>
  
  <p>I downloaded <a href="https://docs.microsoft.com/en-us/sysinternals/downloads/portmon" rel="nofollow noreferrer">PortMon</a> from <a href="https://en.wikipedia.org/wiki/Sysinternals" rel="nofollow noreferrer">Sysinternals</a>. I've captured two logs. The first is the log how what happens when <a href="https://en.wikipedia.org/wiki/HyperACCESS" rel="nofollow noreferrer">HyperTerminal</a> opens the port, and the second is what happens when the .NET SerialPort class tries to open the port:</p>
  
  <p>HyperTerminal:</p>
  
  <pre><code>IRP_MJ_CREATE                  USBSER000  SUCCESS       Options: Open
  IOCTL_SERIAL_SET_QUEUE_SIZE    USBSER000  SUCCESS       InSize: 8192 OutSize: 8192
  IOCTL_SERIAL_CONFIG_SIZE       USBSER000  SUCCESS       Size: 0
  IOCTL_SERIAL_GET_BAUD_RATE     USBSER000  SUCCESS
  IOCTL_SERIAL_GET_LINE_CONTROL  USBSER000  SUCCESS
  IOCTL_SERIAL_GET_CHARS         USBSER000  SUCCESS
  IOCTL_SERIAL_GET_HANDFLOW      USBSER000  SUCCESS
  IOCTL_SERIAL_GET_BAUD_RATE     USBSER000  SUCCESS
  IOCTL_SERIAL_GET_LINE_CONTROL  USBSER000  SUCCESS
  IOCTL_SERIAL_GET_CHARS         USBSER000  SUCCESS
  IOCTL_SERIAL_GET_HANDFLOW      USBSER000  SUCCESS
  IOCTL_SERIAL_SET_BAUD_RATE     USBSER000  SUCCESS       Rate: 9600
  IOCTL_SERIAL_SET_RTS           USBSER000  SUCCESS
  IOCTL_SERIAL_SET_DTR           USBSER000  * 0xC0000001
  IOCTL_SERIAL_SET_LINE_CONTROL  USBSER000  SUCCESS       StopBits: 1 Parity: NONE WordLength: 8
  IOCTL_SERIAL_SET_CHAR          USBSER000  SUCCESS       EOF:1a ERR:0 BRK:0 EVT:1a XON:f6 XOFF:6
  IOCTL_SERIAL_SET_HANDFLOW      USBSER000  SUCCESS       Shake:80000001 Replace:80000040 XonLimit:80 XoffLimit:200
  IOCTL_SERIAL_SET_TIMEOUTS      USBSER000  SUCCESS       RI:10 RM:0 RC:0 WM:0 WC:5000
  IOCTL_SERIAL_SET_WAIT_MASK     USBSER000  SUCCESS       Mask: RLSD ERR
  IOCTL_SERIAL_GET_MODEMSTATUS   USBSER000  SUCCESS
  IOCTL_SERIAL_WAIT_ON_MASK      USBSER000
  IRP_MJ_READ    USBSER000                                Length 80
  </code></pre>
  
  <p>.NET SerialPort:</p>
  
  <pre><code>IRP_MJ_CREATE                  USBSER000  SUCCESS       Options: Open
  IOCTL_SERIAL_GET_PROPERTIES    USBSER000  SUCCESS
  IOCTL_SERIAL_GET_MODEMSTATUS   USBSER000  SUCCESS
  IOCTL_SERIAL_GET_BAUD_RATE     USBSER000  SUCCESS
  IOCTL_SERIAL_GET_LINE_CONTROL  USBSER000  SUCCESS
  IOCTL_SERIAL_GET_CHARS         USBSER000  SUCCESS
  IOCTL_SERIAL_GET_HANDFLOW      USBSER000  SUCCESS
  IOCTL_SERIAL_GET_BAUD_RATE     USBSER000  SUCCESS
  IOCTL_SERIAL_GET_LINE_CONTROL  USBSER000  SUCCESS
  IOCTL_SERIAL_GET_CHARS         USBSER000  SUCCESS
  IOCTL_SERIAL_GET_HANDFLOW      USBSER000  SUCCESS
  IOCTL_SERIAL_SET_BAUD_RATE     USBSER000  SUCCESS       Rate: 9600
  IOCTL_SERIAL_CLR_RTS           USBSER000  SUCCESS
  IOCTL_SERIAL_CLR_DTR           USBSER000  * 0xC0000001
  IOCTL_SERIAL_SET_LINE_CONTROL  USBSER000  SUCCESS       StopBits: 1 Parity: NONE WordLength: 8
  IOCTL_SERIAL_SET_CHAR          USBSER000  SUCCESS       EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
  IOCTL_SERIAL_SET_HANDFLOW      USBSER000  SUCCESS       Shake:0 Replace:0 XonLimit:4096 XoffLimit:4096
  IOCTL_SERIAL_GET_BAUD_RATE     USBSER000  SUCCESS
  IOCTL_SERIAL_GET_LINE_CONTROL  USBSER000  SUCCESS
  IOCTL_SERIAL_GET_CHARS         USBSER000  SUCCESS
  IOCTL_SERIAL_GET_HANDFLOW      USBSER000  SUCCESS
  IOCTL_SERIAL_SET_BAUD_RATE     USBSER000  SUCCESS       Rate: 9600
  IOCTL_SERIAL_CLR_RTS           USBSER000  SUCCESS
  IOCTL_SERIAL_CLR_DTR           USBSER000  * 0xC0000001
  IOCTL_SERIAL_SET_LINE_CONTROL  USBSER000  SUCCESS       StopBits: 1 Parity: NONE WordLength: 8
  IOCTL_SERIAL_SET_CHAR          USBSER000  SUCCESS       EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
  IOCTL_SERIAL_SET_HANDFLOW      USBSER000  SUCCESS       Shake:0 Replace:0 XonLimit:4096 XoffLimit:4096
  IOCTL_SERIAL_CLR_DTR           USBSER000  * 0xC0000001
  IRP_MJ_CLEANUP                 USBSER000  SUCCESS
  IRP_MJ_CLOSE                   USBSER000  SUCCESS
  </code></pre>