title: receiving data from smart card reader through serial connection (C#)
body: <p>I have to design a software to communicate with a smart card reader ..</p>
  
  <p>as a first step i want to get to receive the data from the card reader and show them in a text box which i can see instantly as it changes (to make sure that there is data has been sent)..</p>
  
  <p>the data(as i used to program it with PLC) sent by card reader is an array of 256 bytes </p>
  
  <p>i did perform the same approach but nothing is shown in my richtextbox.. </p>
  
  <p>i'd like you to take a look at my code and tell me whats wrong:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.IO.Ports;
  using System.IO;
  
  namespace mycard
  {
      public partial class Form1 : Form
      {
          byte[] memo = new byte[256];
          byte[] buffer = new byte[255];
  
          private SerialPort serialPort = new SerialPort();
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              serialPort.DataReceived += 
                  new SerialDataReceivedEventHandler(serialPort_DataReceived);
          }
  
          //open port
          private void button1_Click(object sender, EventArgs e)
          {
              if (s1.IsOpen)
              {
                  s1.Close();
              }
              s1.Open();
          }
  
  
          //Eject order which is working fine
          private void button1_Click_1(object sender, EventArgs e)
          {
              byte[] data= new byte[4];
  
              memo[0]=0x60;
              memo[1]=0x00;
              memo[2]=0x02;
              memo[3]=0x43;
              memo[4]=0x31; //32
              memo[5]=0x10; //13
  
              s1.Write(memo,0,6);
          }
  
          //close port
          private void button2_Click(object sender, EventArgs e)
          {
              s1.Close();
          }
  
          private void button3_Click(object sender, EventArgs e)
          {
              txtDataReceived.Text = "\r\n";
          }
  
          public delegate void myDelegate();
          public void updateTextBox()
          {
  
              byte[] buffer = new byte[255];
  
              serialPort.Read(buffer, 0, 4);
              txtDataReceived.Text = (buffer[0].ToString() + Environment.NewLine 
                  + buffer[1].ToString() + Environment.NewLine 
                  + buffer[2].ToString() + Environment.NewLine 
                  + buffer[3].ToString() + Environment.NewLine);
  
              // i have tried the following code as well but i got nothing as well
              // txtDataReceived.AppendText(serialPort.ReadExisting());
              // txtDataReceived.ScrollToCaret();
  
          }
      }
  }
  </code></pre>
  
  <p>i expect to get the following output in the created richtextbox as bellow .. where the whole message appears and the xxx values of each byte of the buffer array is changed according to state of the smart card reader (or in other words according to what card reader sends )...</p>
  
  <p>for example(what i have been trying to do in my code above) .. while there is connection, and a card is inserted in the card reader ,the buffer[0],[1],[2]and[3] values should change instantly from 0's to 48,48,48,48 ( as sent by card reader) .. and so on..</p>
  
  <pre><code>buffer:
  [0]=xxx
  [1]=xxx
  [2]=xxx
  [3]=xxx
  [4]=xxx
  [5]=xxx
  [6]=xxx
  ...
  ...
  ...
  [255]=xxx
  </code></pre>
  
  <p>instead, the box is empty ... </p>
  
  <p>hope my prob. is clear .. and i really wish to find the solution here .. it's a very important matter to me .. and i have spent a lot of time trying without any success </p>
  
  <hr>
  
  <p>smart card reader type : KDM9650</p>