title: fast reading constant data stream from serial port in C++.net
body: <p>I'm trying to establish a SerialPort connection which transfers 16 bit data packages at a rate of 10-20 kHz. Im programming this in C++/CLI. The sender just enters an infinte while-loop after recieving the letter "s" and constantly sends 2 bytes with the data.</p>
  
  <p>A Problem with the sending side is very unlikely, since a more simple approach works perfectly but too slow (in this approach, the reciever sends always an "a" first, and then gets 1 package consisting of 2 bytes. It leads to a speed of around 500Hz).
  Here is the important part of this working but slow approach:</p>
  
  <pre><code>public: SerialPort^ port;
  
  in main:
  Parity p = (Parity)Enum::Parse(Parity::typeid, "None");
  StopBits s = (StopBits)Enum::Parse(StopBits::typeid, "1");
  port = gcnew SerialPort("COM16",384000,p,8,s);
  port-&gt;Open();
  
  and then doing as often as wanted:
  port-&gt;Write("a");
  int i = port-&gt;ReadByte();
  int j = port-&gt;ReadByte();
  </code></pre>
  
  <p>This is now the actual approach im working with:</p>
  
  <pre><code>static int values[1000000];
  static int counter = 0;
  
  void reader(void)
      {
          SerialPort^ port;
          Parity p = (Parity)Enum::Parse(Parity::typeid, "None");
          StopBits s = (StopBits)Enum::Parse(StopBits::typeid, "1");
          port = gcnew SerialPort("COM16",384000,p,8,s);
          port-&gt;Open();
          unsigned int i = 0;
          unsigned int j = 0;
          port-&gt;Write("s"); //with this command, the sender starts to send constantly
          while(true)
          {
              i = port-&gt;ReadByte();
              j = port-&gt;ReadByte();
              values[counter] = j + (i*256);
              counter++;
          }
      }
  
  in main:
  Thread^ readThread = gcnew Thread(gcnew ThreadStart(reader));
  readThread-&gt;Start();
  </code></pre>
  
  <p>The counter increases (much more) rapidly at a rate of 18472 packages/s, but the values are somehow wrong.
  Here is an example:
  The value should look like this, with the last 4 bits changing randomly (its a signal of an analogue-digital converter):</p>
  
  <pre><code>111111001100111 
  </code></pre>
  
  <p>Here are some values of the threaded solution given in the code: </p>
  
  <pre><code>1110011001100111 
  1110011000100111 
  1110011000100111 
  1110011000100111
  </code></pre>
  
  <p>So it looks like the connection reads the data in the middle of the package (to be exact: 3 bits too late). What can i do? I want to avoid a solution where this error is fixed later in the code while reading the packages like this, because I don't know if the the shifting error gets worse when I edit the reading code later, which I will do most likely. </p>
  
  <p>Thanks in advance,</p>
  
  <p>Nikolas</p>
  
  <p>PS: If this helps, here is the code of the sender-side (an AtMega168), written in C.</p>
  
  <pre><code>uint8_t activate = 0;
  
  void uart_puti16(uint16_t val) //function that writes the data to serial port
  {
  
      while ( !( UCSR0A &amp; (1&lt;&lt;UDRE0)) ) //wait until serial port is ready
              nop(); // wait 1 cycle
              UDR0 = val &gt;&gt; 8; //write first byte to sending register
      while ( !( UCSR0A &amp; (1&lt;&lt;UDRE0)) ) //wait until serial port is ready
              nop(); // wait 1 cycle
              UDR0 = val &amp; 0xFF; //write second byte to sending register
  }
  
  in main:
  while(1)
  {
          if(active == 1)
          {   
              uart_puti16(read()); //read is the function that gives a 16bit data set
          }               
  }
  
  ISR(USART_RX_vect) //interrupt-handler for a recieved byte
  {
          if(UDR0 == 'a') //if only 1 single data package is requested
          {
              uart_puti16(read());
          }
          if(UDR0 == 's') //for activating constant sending
          {
              active = 1;
          }
          if(UDR0 == 'e') //for deactivating constant sending
          {
              active = 0;
          }
  }
  </code></pre>