public void PerformCardOperation() { switch (state) { case CardReaderState.Discard: IdleState(); break; case CardReaderState.Insert: state = CardReaderState.Hold; InsertCard(); break; case CardReaderState.Hold: state = CardReaderState.Discard; CardTasks(); break; } }
private void PollCardReaderState() { CardReaderState NewState = CardReaderState.Unknown; bool SimulateCardRemoved = false; this.BeginAtomic(); try { IntPtr Handle = this.AcquireHandle(); try { // Get Card status ScpCommands.CardStatus Status = ScpCommands.GetStatus(Handle); switch (Status) { case ScpCommands.CardStatus.NoCard: NewState = CardReaderState.NoCard; break; case ScpCommands.CardStatus.CardInsertedPowerOff: NewState = CardReaderState.CardPresent; break; case ScpCommands.CardStatus.CardInsertedPowerOn: NewState = CardReaderState.CardExclusivelyInUse; break; default: NewState = CardReaderState.Unknown; break; } // Get ATR if (Status == ScpCommands.CardStatus.CardInsertedPowerOff) //do NOT check power on cards, because the ATR cannot chnage when the card is powered! { ScpCommands.SelectApplicationProtocol(Handle, Scap.T0); byte[] Atr = ScpCommands.PowerOn(Handle, false); if (this.atr != null && !Atr.HasEqualValue(this.atr)) { //Card was changed during polls -> simulate 'card removed' event SimulateCardRemoved = true; } this.atr = Atr; ScpCommands.PowerOff(Handle); } else if (Status == ScpCommands.CardStatus.CardInsertedPowerOn && this.handleCount == 0) { // the card is powered, but we didn't do it (is done at 'connect', but then the handle count must be greater) NewState = CardReaderState.Unknown; } } // ReSharper disable EmptyGeneralCatchClause catch { //Do nothing in case of error (next loop) } finally { this.ReleaseHandle(); } } catch { //Do nothing in case of error (next loop) } finally { this.EndAtomic(); } // ReSharper restore EmptyGeneralCatchClause if (SimulateCardRemoved) { this.state = CardReaderState.NoCard; this.InvokeStateChanged(); } if (this.State != NewState) { this.state = NewState; this.InvokeStateChanged(); } Thread.Sleep(1000); }