title: C# - SerialPort Encoding help needed
body: <p>I need help with my program with which I want to read codes from several microcontrollers simultaneously and translate them into plain text. There is only one serial interface where all codes are collected. The translation of the codes works, but I have the problem that the codes are not read correctly. He makes pauses again and again where there are no gaps and where there are gaps (500 micro sec. ) He doesn't always recognize them correctly. </p>
  
  <p>Here's part of the code: </p>
  
  <pre><code>public byte[] block = new byte[4096];
          public int posBlock = 0;
  
          public void Read(object sender, SerialDataReceivedEventArgs e)
          {
              console_.WriteLine("--------------------------------------------------");
              console_.WriteLine("Void: Read(object sender, SerialDataReceivedEventArgs e)");
              int offset = 0, count = serialPort.BytesToRead, i = 0;
              byte[] rd = new byte[count];
  
              serialPort.Read(rd, offset, count);
  
              for (int buffer = 0; buffer &lt; count; buffer++) { block[posBlock + buffer] = rd[buffer]; i = buffer; }
              i++;
              posBlock += i;
              string rcpausgabe = null;
              for (int i2 = 0; i2 &lt; posBlock; i2++) { rcpausgabe += " " + (int)(block[i2]); }
              //WriteProtocol(global_.variables.protocol_other, rcpausgabe, true);
              console_.WriteLine("ReadCode: " + rcpausgabe);
              if (IsDecode == false) { Zersetzten(); } else { console_.WriteLine("Add but not Zersetzen()"); }
              IsDecode = false;
              console_.WriteLine("--------------------------------------------------");
          }
  
          bool IsDecode = true;
          public int ib = 0;
          public string bs = null;
  
          private void Zersetzten()
          {
              IsDecode = true;
              console_.WriteLine("--------------------------------------------------");
              console_.WriteLine("Void: Zersetzten()");
  
              byte[] b = new byte[4096];
  
              while (!(ib &gt;= posBlock))
              {
                  for (int i7 = 0; i7 &lt; b[1] + 3; i7++)
                  {
                      string rcpausgabe1 = null;
                      b[i7] = block[ib];
                      ib++;;
                      if (i7+1 == (b[1] + 3))
                      {
                          Thread.Sleep(500);
                          console_.WriteLine("Decoding b");
                          for (int i2 = 0; i2 &lt; b[1]+3; i2++) { rcpausgabe1 += " " + (int)(b[i2]); }
                          console_.WriteLine("Decoding Code: " + rcpausgabe1);
                          bs = rcpausgabe1;                        
                          Decoding(b);
                      }                    
                  }
              }
              IsDecode = false;
              block = new byte[4096];
              posBlock = 0;
              ib = posBlock;
              console_.WriteLine("--------------------------------------------------");
          }
  </code></pre>
  
  <p>I have programmed my own console (console_) to help me with this, which allows me to track all processes in real time.</p>
  
  <p>I also thank everyone for their answers in advance. </p>
  
  <p>(I translated this text with Pons translator because my english is not so good) </p>