public ThaiNationalIDCardReader()
        {
            context = ContextFactory.Instance.Establish(SCardScope.System);
            reader  = new SCardReader(context);
            apdu    = new ThaiNationalIDCardAPDUCommandType02();

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        }
        private void Open()
        {
            try
            {
                Thread.Sleep(1500);

                string[] szReaders = context.GetReaders();
                if (szReaders.Length <= 0)
                {
                    throw new PCSCException(SCardError.NoReadersAvailable, "Could not find any Smartcard reader.");
                }

                error = reader.Connect(szReaders[0], SCardShareMode.Shared, SCardProtocol.T0 | SCardProtocol.T1);
                HandleError();

                intPtr = new IntPtr();
                switch (reader.ActiveProtocol)
                {
                case SCardProtocol.T0:
                    intPtr = SCardPCI.T0;
                    break;

                case SCardProtocol.T1:
                    intPtr = SCardPCI.T1;
                    break;

                case SCardProtocol.Raw:
                    intPtr = SCardPCI.Raw;
                    break;

                default:
                    throw new PCSCException(SCardError.ProtocolMismatch, "Protocol not supported: " + reader.ActiveProtocol.ToString());
                }

                error = reader.Status(out string[] readerNames, out SCardState state, out SCardProtocol protocol, out byte[] atrs);
                HandleError();

                if (atrs == null || atrs.Length < 2)
                {
                    throw new PCSCException(SCardError.InvalidAtr);
                }

                if (atrs[0] == 0x3B && atrs[1] == 0x67)
                {
                    apdu = new ThaiNationalIDCardAPDUCommandType01();
                }

                if (!SelectApplet())
                {
                    throw new Exception("SmartCard not support (Can't select Ministry of Interior Applet).");
                }
            }
            catch (PCSCException e)
            {
                throw e;
            }
        }