Receive data from the smart card reader via a serial connection (C #)


I have to design a software to communicate with a smart card reader ..

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)..

the data(as i used to program it with PLC) sent by card reader is an array of 256 bytes

i did perform the same approach but nothing is shown in my richtextbox..

i'd like you to take a look at my code and tell me whats wrong:

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()

        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)

        //Eject order which is working fine
        private void button1_Click_1(object sender, EventArgs e)
            byte[] data= new byte[4];

            memo[4]=0x31; //32
            memo[5]=0x10; //13


        //close port
        private void button2_Click(object sender, EventArgs e)

        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();


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 )...

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..


instead, the box is empty ...

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

smart card reader type : KDM9650

You need to actually read the data in serialPort_DataReceived. You're not doing anything here. Also: There's a button1_Click event and a button1_Click_1 event. Which one is being called by button1? If the first one is not being called, the serial port isn't opened at all.

Please note in addition: The DataReceived event is called in its own thread, so before you update the UI you need to change to the UI thread using this.Invoke (WinForms) or this.Dispatcher.Invoke (WPF).