/// <summary> /// Sample code to hande a couple of different cards based on the identification process /// </summary> /// <returns>None</returns> private async Task HandleCard(CardAddedEventArgs args) { try { card?.Dispose(); card = args.SmartCard.CreateMiFareCard(); var cardIdentification = await card.GetCardInfo(); DisplayText("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString() + "\r\nCard name: " + cardIdentification.PcscCardName.ToString()); if (cardIdentification.PcscDeviceClass == MiFare.PcSc.DeviceClass.StorageClass && (cardIdentification.PcscCardName == CardName.MifareStandard1K || cardIdentification.PcscCardName == CardName.MifareStandard4K)) { // Handle MIFARE Standard/Classic DisplayText("MIFARE Standard/Classic card detected"); var uid = await card.GetUid(); DisplayText("UID: " + BitConverter.ToString(uid)); // 16 sectors, print out each one for (var sector = 0; sector < 16; sector++) { try { var data = await card.GetData(sector, 0, 48); string hexString = ""; for (int i = 0; i < data.Length; i++) { hexString += data[i].ToString("X2") + " "; } DisplayText(string.Format("Sector '{0}':{1}", sector, hexString)); } catch (Exception) { DisplayText("Failed to load sector: " + sector); } } } } catch (Exception e) { PopupMessage("HandleCard Exception: " + e.Message); } }
private async void CardAdded(object sender, CardEventArgs args) { cardEventArg = args; lblCardPresence.Invoke(new Action(() => lblCardPresence.Text = "Card Present")); MiFareCard card = args.SmartCard.CreateMiFareCard(); var cardIdentification = await card.GetCardInfo(); var uid = await card.GetUid(); lblCardInfo.Invoke(new Action(() => lblCardInfo.Text = "Card Info \n Device Class: " + cardIdentification.PcscDeviceClass.ToString() + "\n Card Name: " + cardIdentification.PcscCardName.ToString() + "\n Card UID: " + BitConverter.ToString(uid))); }
public static int Anticollision(IntPtr obj, IntPtr serialNumberBuf, int bufSize, IntPtr serialNumberSize) { string text = $"!!! Anticollision !!!\tobj:{obj}\tSerialNumberBuf:{serialNumberBuf}\tBufSize:{bufSize}\tSerialNumberSize:{serialNumberSize}\t"; WriteToLog(text); try { var uid = _card?.GetUid().Result; if (uid != null) { byte[] truncUid = new byte[Math.Min(bufSize, uid.Length)]; Array.Copy(uid, truncUid, truncUid.Length); UnMemory <int> .SaveInMem(truncUid.Length, ref serialNumberSize); UnMemory <byte> .SaveInMemArr(uid, ref serialNumberBuf); //Marshal.StructureToPtr(memory_object, SerialNumberBuf, true); var uidWrited = UnMemory <byte> .ReadInMemArr(serialNumberBuf, truncUid.Length); var serialNumberSizeWrited = UnMemory <int> .ReadInMem(serialNumberSize); text = $"!!! Anticollision over !!!\tobj:{obj}\tSerialNumberBuf:{serialNumberBuf}\tBufSize:{bufSize}\tuid:{BitConverter.ToString(uid)}\tuid_w:{BitConverter.ToString(uidWrited ?? new byte[] {})}\tSerialNumberSize:{serialNumberSize}\tSerialNumberSize_writed:{serialNumberSizeWrited}\t"; WriteToLog(text); return((int)ErrorCodes.E_SUCCESS); } WriteToLog($"Anticollision ERROR !!! _card:{_card == null}\r\n uid:{uid == null}"); return((int)ErrorCodes.E_CARDREADER_NOT_INIT); } catch (Exception e) { WriteToLog($"Anticollision ERROR !!! {text}\r\n {e}"); return((int)ErrorCodes.E_GENERIC); } }
private static async Task HandleCard(SmartCard args) { try { var newConnection = args.CreateMiFareCard(); lock (cardConnectionLock) { if (currentConnection != null) { currentConnection.Dispose(); currentCardId = null; currentConnection = null; } currentConnection = newConnection; } var cardId = await currentConnection.GetCardInfo(); Debug.WriteLine("Connected to card\r\nPC/SC device class: {0}\r\nCard name: {1}", cardId.PcscDeviceClass, cardId.PcscCardName); if (cardId.PcscDeviceClass == DeviceClass.StorageClass && (cardId.PcscCardName == CardName.MifareStandard1K || cardId.PcscCardName == CardName.MifareStandard4K)) { Debug.WriteLine("MiFare Classic card detected"); var uid = await currentConnection.GetUid(); currentCardId = uid.ByteArrayToString(); Debug.WriteLine("UID: " + currentCardId); } else { throw new NotImplementedException("Card type is not implemented"); } } catch (Exception ex) { Debug.WriteLine(ex); } }