public bool connectCard(int rdrIndex)
        {
            connActive = true;

            if (rdrIndex == 0)
            {
                //retCode = ACR122uCard.SCardConnect(hContext, rdrStates[rdrIndex].RdrName.ToString(), ACR122uCard.SCARD_SHARE_SHARED,
                //        ACR122uCard.SCARD_PROTOCOL_T0 | ACR122uCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);
            }
            else if (rdrIndex == 1)
            {
                //retCode1 = ACR122uCard.SCardConnect(hContext, rdrStates[rdrIndex].RdrName.ToString(), ACR122uCard.SCARD_SHARE_SHARED,
                //          ACR122uCard.SCARD_PROTOCOL_T0 | ACR122uCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);
            }



            if (retCode != ACR122uCard.SCARD_S_SUCCESS)
            {
                //MessageBox.Show(Card.GetScardErrMsg(retCode), "Card not available");
                //this.Text = ACR122uCard.GetScardErrMsg(retCode).ToString() + " " + "Card not available 0 " + retCode.ToString();
                connActive = false;
                return(false);
            }

            if (retCode1 != ACR122uCard.SCARD_S_SUCCESS)
            {
                //MessageBox.Show(Card.GetScardErrMsg(retCode), "Card not available");
                this.Text  = ACR122uCard.GetScardErrMsg(retCode).ToString() + " " + "Card not available 1 " + retCode.ToString();
                connActive = false;
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
 internal void establishContext()
 {
     retCode = ACR122uCard.SCardEstablishContext(ACR122uCard.SCARD_SCOPE_SYSTEM, 0, 0, ref hContext);
     if (retCode != ACR122uCard.SCARD_S_SUCCESS)
     {
         MessageBox.Show("Check your device and please restart again", "Reader not connected");
         connActive = false;
         return;
     }
 }
        public List <string> ListReaders()
        {
            int           ReaderCount         = 0;
            List <string> AvailableReaderList = new List <string>();

            //Make sure a context has been established before
            //retrieving the list of smartcard readers.
            retCode = ACR122uCard.SCardListReaders(hContext, null, null, ref ReaderCount);
            if (retCode != ACR122uCard.SCARD_S_SUCCESS)
            {
                //MessageBox.Show(ACR122uCard.GetScardErrMsg(retCode));
                //connActive = false;
            }

            byte[] ReadersList = new byte[ReaderCount];

            //Get the list of reader present again but this time add sReaderGroup, retData as 2rd & 3rd parameter respectively.
            retCode1 = ACR122uCard.SCardListReaders(hContext, null, ReadersList, ref ReaderCount);
            if (retCode != ACR122uCard.SCARD_S_SUCCESS)
            {
                //MessageBox.Show(ACR122uCard.GetScardErrMsg(retCode));
            }

            string rName = "";
            int    indx  = 0;

            if (ReaderCount > 0)
            {
                // Convert reader buffer to string
                while (ReadersList[indx] != 0)
                {
                    while (ReadersList[indx] != 0)
                    {
                        rName = rName + (char)ReadersList[indx];
                        indx  = indx + 1;
                    }

                    //Add reader name to list

                    AvailableReaderList.Add(rName);
                    rName = "";
                    indx  = indx + 1;
                }
            }
            GC.Collect();
            return(AvailableReaderList);
        }
        public bool connectCard()
        {
            connActive = true;

            retCode = ACR122uCard.SCardConnect(hContext, readername, ACR122uCard.SCARD_SHARE_SHARED,
                                               ACR122uCard.SCARD_PROTOCOL_T0 | ACR122uCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);

            if (retCode != ACR122uCard.SCARD_S_SUCCESS)
            {
                //MessageBox.Show(Card.GetScardErrMsg(retCode), "Card not available");
                //this.Text = ACR122uCard.GetScardErrMsg(retCode).ToString() + " " + "Card not available " + retCode.ToString();
                connActive = false;
                return(false);
            }

            return(true);
        }
        private void deviceStatusUpdater(int retVal)
        {
            if (retVal == 0)
            {
                //tsslblDevice.Text = "Connected to " + this.RdrState.RdrName.ToString() + " device.";
                //tsslblDevice.Text = "Connected to " + this.rdrStates[0].RdrName.ToString() + " device.";
                tsslblDevice.Image = Z.IconLibrary.FarmFresh.Icon.AcceptButton.GetImage16();

                /*tmrDeviceConn.Enabled = false;
                 * tmrReadTags.Enabled = true;*/
            }
            else
            {
                tsslblDevice.Text  = ACR122uCard.GetScardErrMsg(retCode);
                tsslblDevice.Image = Z.IconLibrary.FarmFresh.Icon.Error.GetImage16();

                /*tmrDeviceConn.Enabled = true;
                 * tmrReadTags.Enabled = false;*/
            }
        }
Ejemplo n.º 6
0
        private string getcardUID()//only for mifare 1k cards
        {
            string cardUID = "";

            byte[] receivedUID = new byte[256];
            ACR122uCard.SCARD_IO_REQUEST request = new ACR122uCard.SCARD_IO_REQUEST();
            request.dwProtocol  = ACR122uCard.SCARD_PROTOCOL_T1;
            request.cbPciLength = System.Runtime.InteropServices.Marshal.SizeOf(typeof(ACR122uCard.SCARD_IO_REQUEST));
            byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 }; //get UID command      for Mifare cards
            int    outBytes  = receivedUID.Length;
            int    status    = ACR122uCard.SCardTransmit(hCard, ref request, ref sendBytes[0], sendBytes.Length, ref request, ref receivedUID[0], ref outBytes);

            if (status != ACR122uCard.SCARD_S_SUCCESS)
            {
                cardUID = "Error";
            }
            else
            {
                cardUID = BitConverter.ToString(receivedUID.Take(4).ToArray()).Replace("-", string.Empty).ToLower();
            }

            return(cardUID);
        }
 public void SelectDevice()
 {
     //MessageBox.Show(this.ListReaders().Count.ToString());
     if (this.ListReaders().Count > 0)
     {
         List <string> availableReaders = this.ListReaders();
         this.RdrState         = new ACR122uCard.SCARD_READERSTATE();
         readername            = availableReaders[0].ToString();//selecting first device
         this.RdrState.RdrName = readername;
         if (retCode == 0)
         {
             MessageBox.Show("Device successfully connected.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
         else
         {
             MessageBox.Show(ACR122uCard.GetScardErrMsg(retCode).ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     else
     {
         MessageBox.Show(ACR122uCard.GetScardErrMsg(retCode).ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        private void tmrReadTags_Tick(object sender, EventArgs e)  // for reading the tags
        //MessageBox.Show(availableReaders[0].ToString() + " " + availableReaders[1].ToString());
        //return;
        {
            try
            {
                if (DeviceCount > 0)
                {
                    retCode = ACR122uCard.SCardConnect(hContext, rdrState1.RdrName.ToString(), ACR122uCard.SCARD_SHARE_SHARED,
                                                       ACR122uCard.SCARD_PROTOCOL_T0 | ACR122uCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            if (retCode == ACR122uCard.SCARD_S_SUCCESS)
            {
                string cardUID = getcardUID();
                //this.Text = cardUID; //displaying on text block
                this.Text = ACR122uCard.GetScardErrMsg(retCode);
                if (cardUID != "63000000" && cardUID != "Error")
                {
                    //if (!StudentQueue.Contains(cardUID))
                    //{
                    //StudentQueue.Enqueue(cardUID);

                    checkStudentTag(cardUID);
                    //}
                }
            }

            //try{
            //    if (DeviceCount > 1)
            //    {
            //        retCode1 = ACR122uCard.SCardConnect(hContext, rdrState2.RdrName.ToString(), ACR122uCard.SCARD_SHARE_SHARED,
            //        ACR122uCard.SCARD_PROTOCOL_T0 | ACR122uCard.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message.ToString());
            //}
            //if (retCode1 == ACR122uCard.SCARD_S_SUCCESS)
            //{
            //    string cardUID = getcardUID();
            //    //displaying on text block
            //    if (cardUID != "63000000" && cardUID != "Error")
            //    {
            //        //if (!StudentQueue.Contains(cardUID))
            //        //{
            //            //StudentQueue.Enqueue(cardUID);
            //            checkStudentTag(cardUID);
            //        //}
            //    }
            //}
            //retCode = 0;
            //retCode1 = 0;
            //hCard = 0;
            //Protocol = 0;
            //hContext = 0;
        }