title: Reading and writing from a serial port in C#, part 2
body: <p>I am trying to replace a C program with a C# one. The C program connects to an older computer via the COM1 serial port. I do not have any kind of information on how the C code connects to the other machine, but it does connect correctly, so I have to guess as to how it is doing it. I have been using <a href="http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx" rel="nofollow noreferrer">Portmon</a> to try to figure out how the C program is writing and reading successfully. I have also been running Portmon when my new C# application is running. In my C# program, I use the SerialPort class.</p>
  
  <p>I am trying to determine what values the different items in the SerialPort class should have by using the output of Portmon on the working program. My thought is that if I can get the values correct for the class, it would write and read correctly. The program (below) is not working correctly. The program opens the COM1 port successfully, but it fails when it tries to write a period (and then another period). These give a timeout.</p>
  
  <p>Looking at the output of Portmon running behind the old C program, the working program opened the COM1 port and then wrote one period and then another before reading a successful value (&amp;OK) from the COM1 port. The old program set up several values that I want to duplicate in my C# program.</p>
  
  <p>I have tried a variety of combinations of the SerialPort options below, but I obviously have not hit on anything.</p>
  
  <p>BTW, I asked some preliminary questions in Stack Overflow question <em><a href="https://stackoverflow.com/questions/14405740">Reading and writing from a serial port in C#, part 2</a></em></p>
  
  <p>I am using Windows XP on the machine that is running the programs. The machine on the other side of the COM1 port is a very old PC.</p>
  
  <p>Is there a better way to do this? Use something other than Portmon? For the Portmon output for the old program the Process column is ntvdm.exe. Is that a problem? Should I not use the C# SerialPort class? <strong>Probably the most important question is: What values should I use for the Serialport class to match the old program?</strong></p>
  
  <p>My C# program:</p>
  
  <pre><code>SerialPort comport = new SerialPort();
  comport.BaudRate = 9600;
  comport.DataBits = 7;
  
  comport.StopBits = StopBits.One;
  comport.Parity = Parity.Odd;
  comport.RtsEnable = true;
  comport.DtrEnable = true;
  comport.Handshake = Handshake.RequestToSend;
  comport.ReadBufferSize = 8192;
  comport.WriteBufferSize = 100;
  comport.WriteTimeout = 30000; // 30 sec
  comport.ReadTimeout = 30000; // 30 sec
  
  comport.PortName = "COM1";
  
  string tempFbuffer;
  byte[] Fbuffer = new byte[200];
  string alldata5;
  
  tempFbuffer = "..";
  
  for (int cnt = 0; cnt &lt; tempFbuffer.Length; cnt++)
  {
     Fbuffer[cnt] = Convert.ToByte(tempFbuffer[cnt]);
  }
  
  comport.Open();
  comport.Write(Fbuffer, 0, 1);
  comport.Write(Fbuffer, 1, 1);
  
  for (i = 0; i &lt; 4; i++)
  {
      alldata5 = alldata5 + comport.ReadChar();
  
  }
  
  comport.Close();
  </code></pre>
  
  <p>The Portmon output of the application I want to mimic is below. This application connects to the other machine and works correctly. I manually put the headings in at the top.</p>
  
  <p>As you can see, on lines 33 and 34 there are successful writes and then on lines 35 and 36, there are successful reads. I ran the program with Portmon in the background.</p>
  
  <pre><code>#       Time            Process         Reuest                          Port    Result   Other
  0       0.00004407      ntvdm.exe       IRP_MJ_CREATE                   Serial0 SUCCESS Options: Open
  1       0.00000198      ntvdm.exe       IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  2       0.00000115      ntvdm.exe       IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  3       0.00000104      ntvdm.exe       IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  4       0.00000106      ntvdm.exe       IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  5       0.0000008       ntvdm.exe       IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  6       0.00000082      ntvdm.exe       IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  7       0.00000085      ntvdm.exe       IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  8       0.00000081      ntvdm.exe       IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  9       0.00000712      ntvdm.exe       IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 1200
  10      0.00000349      ntvdm.exe       IOCTL_SERIAL_CLR_RTS            Serial0 SUCCESS
  11      0.00000366      ntvdm.exe       IOCTL_SERIAL_CLR_DTR            Serial0 SUCCESS
  12      0.00000225      ntvdm.exe       IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: NONE WordLength: 5
  13      0.00000111      ntvdm.exe       IOCTL_SERIAL_SET_CHAR           Serial0 SUCCESS EOF:0 ERR:0 BRK:0 EVT:0 XON:11 XOFF:13
  14      0.00000735      ntvdm.exe       IOCTL_SERIAL_SET_HANDFLOW       Serial0 SUCCESS Shake:0 Replace:0 XonLimit:2048 XoffLimit:512
  15      0.00000133      ntvdm.exe       IOCTL_SERIAL_LSRMST_INSERT      Serial0 SUCCESS Char: ffffffff
  16      0.00000338      ntvdm.exe       IOCTL_SERIAL_SET_QUEUE_SIZE     Serial0 SUCCESS InSize: 8192 OutSize: 100
  17      0.00000083      ntvdm.exe       IOCTL_SERIAL_GET_TIMEOUTS       Serial0 SUCCESS
  18      0.00000092      ntvdm.exe       IOCTL_SERIAL_SET_TIMEOUTS       Serial0 SUCCESS RI:-1 RM:0 RC:0 WM:0 WC:0
  19      0.00000349      ntvdm.exe       IOCTL_SERIAL_SET_DTR            Serial0 SUCCESS
  20      0.00000342      ntvdm.exe       IOCTL_SERIAL_SET_RTS            Serial0 SUCCESS
  21      0.00001121      ntvdm.exe       IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 1200
  22      0.00000262      ntvdm.exe       IOCTL_SERIAL_SET_WAIT_MASK      Serial0 SUCCESS Mask: RXCHAR CTS DSR RLSD ERR RING
  23      36.94054111     ntvdm.exe       IOCTL_SERIAL_WAIT_ON_MASK       Serial0 SUCCESS
  24      0.00000403      ntvdm.exe       IOCTL_SERIAL_CLR_DTR            Serial0 SUCCESS
  25      0.00000356      ntvdm.exe       IOCTL_SERIAL_CLR_RTS            Serial0 SUCCESS
  26      0.00000351      ntvdm.exe       IOCTL_SERIAL_SET_DTR            Serial0 SUCCESS
  27      0.00000348      ntvdm.exe       IOCTL_SERIAL_SET_RTS            Serial0 SUCCESS
  28      0.00000717      ntvdm.exe       IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 9600
  29      0.00000145      ntvdm.exe       IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  30      0.00000246      ntvdm.exe       IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 5
  31      0.00000086      ntvdm.exe       IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  32      0.00000226      ntvdm.exe       IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 7
  33      0.00002222      ntvdm.exe       IRP_MJ_WRITE                    Serial0 SUCCESS Length 1: .
  34      0.00002142      ntvdm.exe       IRP_MJ_WRITE                    Serial0 SUCCESS Length 1: .
  35      0.00000562      ntvdm.exe       IRP_MJ_READ                     Serial0 SUCCESS Length 4: &amp;OK.
  36      0.00000239      ntvdm.exe       IRP_MJ_READ                     Serial0 SUCCESS Length 0:
  37      0.00000533      ntvdm.exe       IOCTL_SERIAL_WAIT_ON_MASK       Serial0 SUCCESS
  38      0.0000023       ntvdm.exe       IRP_MJ_READ                     Serial0 SUCCESS Length 0:
  39      95.8854497      ntvdm.exe       IOCTL_SERIAL_WAIT_ON_MASK       Serial0 SUCCESS
  40      0.00002486      ntvdm.exe       IRP_MJ_WRITE                    Serial0 SUCCESS Length 1: D
  </code></pre>
  
  <p>The Portmon output of the C# application that I am creating is below. The C# code above, when run with Portmon in the background creates this output. I put in the headers.</p>
  
  <p>As you can see, there is a timeout error on line 69 during the write. I need this to run close enough to the C one so that the writes and reads work.</p>
  
  <p>Obviously the baud rate, wordlength, parity and several others are set correctly.</p>
  
  <pre><code>#       Time            Process         Reuest                          Port    Result   Other
  0       0.00004362      fancom.exe      IRP_MJ_CREATE                   Serial0 SUCCESS Options: Open
  1       0.0000019       fancom.exe      IOCTL_SERIAL_GET_PROPERTIES     Serial0 SUCCESS
  2       0.00000263      fancom.exe      IOCTL_SERIAL_GET_MODEMSTATUS    Serial0 SUCCESS
  3       0.00000096      fancom.exe      IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  4       0.00000097      fancom.exe      IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  5       0.00000084      fancom.exe      IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  6       0.00000097      fancom.exe      IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  7       0.00000081      fancom.exe      IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  8       0.00000088      fancom.exe      IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  9       0.0000008       fancom.exe      IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  10      0.00000079      fancom.exe      IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  11      0.00000715      fancom.exe      IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 9600
  12      0.00000355      fancom.exe      IOCTL_SERIAL_CLR_DTR            Serial0 SUCCESS
  13      0.0000024       fancom.exe      IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 7
  14      0.00000107      fancom.exe      IOCTL_SERIAL_SET_CHAR           Serial0 SUCCESS EOF:1a ERR:3f BRK:3f EVT:1a XON:11 XOFF:13
  15      0.00000779      fancom.exe      IOCTL_SERIAL_SET_HANDFLOW       Serial0 SUCCESS Shake:8 Replace:84 XonLimit:1024 XoffLimit:1024
  16      0.0000008       fancom.exe      IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  17      0.00000081      fancom.exe      IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  18      0.00000081      fancom.exe      IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  19      0.00000081      fancom.exe      IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  20      0.00000705      fancom.exe      IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 9600
  21      0.00000349      fancom.exe      IOCTL_SERIAL_SET_DTR            Serial0 SUCCESS
  22      0.0000022       fancom.exe      IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 7
  23      0.00000098      fancom.exe      IOCTL_SERIAL_SET_CHAR           Serial0 SUCCESS EOF:1a ERR:3f BRK:3f EVT:1a XON:11 XOFF:13
  24      0.00000493      fancom.exe      IOCTL_SERIAL_SET_HANDFLOW       Serial0 SUCCESS Shake:9 Replace:84 XonLimit:1024 XoffLimit:1024
  25      0.00000684      fancom.exe      IOCTL_SERIAL_SET_DTR            Serial0 SUCCESS
  26      0.00000108      fancom.exe      IOCTL_SERIAL_SET_TIMEOUTS       Serial0 SUCCESS RI:-1 RM:-1 RC:30000 WM:0 WC:30000
  27      0.00000227      fancom.exe      IOCTL_SERIAL_SET_WAIT_MASK      Serial0 SUCCESS Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING
  28      35.62327662     fancom.exe      IOCTL_SERIAL_WAIT_ON_MASK       Serial0 SUCCESS
  29      0.00000399      fancom.exe      IOCTL_SERIAL_SET_QUEUE_SIZE     Serial0 SUCCESS InSize: 8192 OutSize: 100
  30      30.00157726     fancom.exe      IRP_MJ_WRITE                    Serial0 TIMEOUT Length 1: .
  31      0.00000767      fancom.exe      IRP_MJ_FLUSH_BUFFERS            Serial0 SUCCESS
  32      0.00001012      fancom.exe      IOCTL_SERIAL_SET_WAIT_MASK      Serial0 SUCCESS Mask:
  33      0.00000402      fancom.exe      IOCTL_SERIAL_CLR_DTR            Serial0 SUCCESS
  34      0.00000116      fancom.exe      IRP_MJ_FLUSH_BUFFERS            Serial0 SUCCESS
  35      0.0000023       fancom.exe      IOCTL_SERIAL_PURGE              Serial0 SUCCESS Purge: RXABORT RXCLEAR
  36      0.00000163      fancom.exe      IOCTL_SERIAL_PURGE              Serial0 SUCCESS Purge: TXABORT TXCLEAR
  37      0.00000404      fancom.exe      IRP_MJ_CLEANUP                  Serial0 SUCCESS
  38      0.00322359      fancom.exe      IRP_MJ_CLOSE                    Serial0 SUCCESS
  39      0.00004607      fancom.exe      IRP_MJ_CREATE                   Serial0 SUCCESS Options: Open
  40      0.00000188      fancom.exe      IOCTL_SERIAL_GET_PROPERTIES     Serial0 SUCCESS
  41      0.00000277      fancom.exe      IOCTL_SERIAL_GET_MODEMSTATUS    Serial0 SUCCESS
  42      0.00000092      fancom.exe      IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  43      0.00000112      fancom.exe      IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  44      0.0000008       fancom.exe  IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  45      0.00000093      fancom.exe      IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  46      0.00000079      fancom.exe      IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  47      0.00000085      fancom.exe      IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  48      0.00000081      fancom.exe      IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  49      0.00000082      fancom.exe      IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  50      0.00000704      fancom.exe      IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 9600
  51      0.00000352      fancom.exe      IOCTL_SERIAL_CLR_DTR            Serial0 SUCCESS
  52      0.00000225      fancom.exe      IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
  53      0.00000113      fancom.exe      IOCTL_SERIAL_SET_CHAR           Serial0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
  54      0.00000489      fancom.exe      IOCTL_SERIAL_SET_HANDFLOW       Serial0 SUCCESS Shake:8 Replace:80 XonLimit:1024 XoffLimit:1024
  55      0.00000084      fancom.exe      IOCTL_SERIAL_GET_BAUD_RATE      Serial0 SUCCESS
  56      0.00000083      fancom.exe      IOCTL_SERIAL_GET_LINE_CONTROL   Serial0 SUCCESS
  57      0.00000081      fancom.exe      IOCTL_SERIAL_GET_CHARS          Serial0 SUCCESS
  58      0.00000081      fancom.exe      IOCTL_SERIAL_GET_HANDFLOW       Serial0 SUCCESS
  59      0.00000696      fancom.exe      IOCTL_SERIAL_SET_BAUD_RATE      Serial0 SUCCESS Rate: 9600
  60      0.00000344      fancom.exe      IOCTL_SERIAL_SET_DTR            Serial0 SUCCESS
  61      0.00000222      fancom.exe      IOCTL_SERIAL_SET_LINE_CONTROL   Serial0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
  62      0.00000102      fancom.exe      IOCTL_SERIAL_SET_CHAR           Serial0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
  63      0.00000474      fancom.exe      IOCTL_SERIAL_SET_HANDFLOW       Serial0 SUCCESS Shake:9 Replace:80 XonLimit:1024 XoffLimit:1024
  64      0.00000345      fancom.exe      IOCTL_SERIAL_SET_DTR            Serial0 SUCCESS
  65      0.00000081      fancom.exe      IOCTL_SERIAL_SET_TIMEOUTS       Serial0 SUCCESS RI:-1 RM:-1 RC:30000 WM:0 WC:30000
  66      0.00000192      fancom.exe      IOCTL_SERIAL_SET_WAIT_MASK      Serial0 SUCCESS Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING
  67      30.00755135     fancom.exe      IOCTL_SERIAL_WAIT_ON_MASK       Serial0 SUCCESS
  68      0.00000353      fancom.exe      IOCTL_SERIAL_SET_QUEUE_SIZE     Serial0 SUCCESS InSize: 8192 OutSize: 100
  69      29.99287343     fancom.exe      IRP_MJ_WRITE                    Serial0 TIMEOUT Length 1: .
  70      0.00000349      fancom.exe      IRP_MJ_FLUSH_BUFFERS            Serial0 SUCCESS
  71      0.00000985      fancom.exe      IOCTL_SERIAL_SET_WAIT_MASK      Serial0 SUCCESS Mask:
  </code></pre>