title: Read only 8 characters from Serial Port
body: <p>Im trying to teach myself C Sharp with making an RFID reader.</p>
  
  <p>I have created some code to read from a serial port (rfid reader over Bluetooth RS232)</p>
  
  <p>The issue im hoping someone can help me with is that: </p>
  
  <p>My RFID reader transmits the card code in very quick succession meaning when I swipe a card it will fire my event handler more than once with different sections of the card code so at the moment I cant receive a full card code in one hit making it impossible to process the card.</p>
  
  <p>The code I have so far is:</p>
  
  <pre><code>    private SerialPort serialPort = new SerialPort("COM14", 9600, Parity.None, 8, StopBits.One); // set com port
  
      String code; // this stores the code from the RFID reader / serial port
      Int32 id; // this is the ID of the person that the RFID code belongs to
      String data;
  
      bool addtag;
  
      public Int32 ID // set the ID so it can be passed to other forms
      {
          get { return id; }
      }
  
      public rfidreader()
      {
          serialPort.DtrEnable = true; // enable data to flow from the SerialPort
          OpenSerialPort(); // Call the OpenSerialPort section below
  
          serialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); // when data is recieved from the RFID reader fire the event handaler
      }
  
      public void PauseRFIDReader()
      {
          addtag = false;
          OpenSerialPort();
      }
  
      public void CloseSerialPort()
      {
          serialPort.Close();
          addtag = true;
      }
  
      private void OpenSerialPort() // called from above
      {
          try
          {
              serialPort.Open(); // open the serialport
          }
          catch // if serail port unavalable show message box, if Retry is pressed run this section again, if cancel carry on without serial port
          {
              DialogResult result = MessageBox.Show("Failed to connect to the RFID reader" + "\n" + "Check the reader is powered on and click Retry" + "\n\n" + "Press Cancel to use the program without RFID reader", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
              if (result == DialogResult.Retry)
              {
                  OpenSerialPort(); // if retry is pressed run the sectiona gain
              }
              else
              {
              } 
          }
      }
  
      private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) // data recieved from RFID reader
      {
          if (addtag == false)
          {
              data = serialPort.ReadExisting(); // read what came from the RFID reader
  
              if (data.Length &gt;= 9) // check if the string if bigger than 9 characters
              {
                  code = data.Substring(0, 9); // if string is bigget than 9 characters trim the ending characters until it is only 9 long
              }
              else
              {
                  code = data; // if less that 9 characters use however many it gets
              }
              MessageBox.Show(code.ToString());
              Start(); // start to process the person
  
          }
          else
          {  
          }
      }
  </code></pre>
  
  <p>Can anyone let me know how to limit the event handler from firing until 8 characters are received and only fire once a second?</p>
  
  <p>Thanks in advance a very mind boggled Ryan</p>