0

I am trying to implement code that will perform an SCardReconnect for certain error conditions when trying to read a TI Tag-IT RFID card with an HID 5321 smart card reader. In the event of a transmit error, I try a reconnect and retry the operation. However, when I perform the reconnect, I get a return value of 6 (INVALID HANDLE). Here is the code for the call for a working SCardConnect, and the non working SCardReconnect.

int iRetval = HID.SCardConnect(
                    m_hContext,
                    m_sReaderName,
                    HiDWinscard.SCARD_SHARE_SHARED,
                    HiDWinscard.SCARD_PROTOCOL_T1,
                    ref m_hCard,
                    ref m_protocol);

int iRetval = HID.SCardReconnect(ref m_hCard,
                                      HiDWinscard.SCARD_SHARE_SHARED,
                                      HiDWinscard.SCARD_PROTOCOL_T1,
                                      0,  //Leave card alone SCARD_LEAVE_CARD
                                      ref m_protocol);

Where m_hCard and m_protocol are the same (IntPtr).

The following SCardTransmit works as well, and it uses the same reference to m_hCard

iRetval = HID.SCardTransmit(m_hCard, ref sioreq, 
                            sendBuffer, sendbufferlen, 
                            ref rioreq, receiveBuffer, 
                            ref receivebufferlen);

This is quite a pressing matter. Does anyone happen to have an idea of what may be going on? I thank everyone in advance for their help!

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
mikem419
  • 61
  • 1
  • 7

1 Answers1

0

On a first quess, I would assume that you should not pass m_hCard as reference. Also I would reconnect using the protocol received from the initial connect as prefered protocol:

int iRetval = HID.SCardReconnect(m_hCard,
                                 HiDWinscard.SCARD_SHARE_SHARED,
                                 m_protocol,
                                 0,  //Leave card alone SCARD_LEAVE_CARD
                                 ref m_protocol);
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Thanks Michael. So I gave that a shot, and I am no longer getting that error, however, I now am occasionally getting error 80100069 (SCARD_W_REMOVED_CARD) even though the card has not been removed from the reader. Any thoughts? – mikem419 Jan 09 '14 at 16:32
  • Perhaps a way to reset the reader, and not just reset the card? – mikem419 Jan 09 '14 at 16:44
  • What sever error condition do you get in the first place that leads you to issuing a reconnect? – Michael Roland Jan 09 '14 at 16:59
  • The issue is an occasional SCardTransmit error for a command that randomly fails for reasons I have not yet figured out. I often get a return of 6A 82 from the TI Tag-It RFID card that is being read, but more often then not, the same SCardTransmit command works just fine – mikem419 Jan 09 '14 at 19:09
  • If you receive a `SCARD_W_REMOVED_CARD` error in that case and the same condition also causes your 0x6A82 error (I assume you refer to [this other question](http://stackoverflow.com/questions/20744981/iso-15693-omnikey-contactless-reader-occasional-read-failure)), I would guess that the error message is correct and the communication with the tag actually does drop. Might be that your tag does not work well with that reader. Tough I still wonder why you would always encounter that problemn after reading a certain page... – Michael Roland Jan 09 '14 at 19:39
  • Yes Michael you are correct, it is correlated to that other question. At this point, I need to implement a mechanism that will behave the same as removing the card and re-inserting it (as this fixes the problem). However per certain requirements, the system needs to be able to recover from a card read failure and retry/reset without removing and reinserting the card. – mikem419 Jan 09 '14 at 19:46
  • If the problem is really related to bad communication between the reader and the tag (possibly due to bad positioning of the tag on the reader in the first place), a soft reset of the reader won't solve the problem, only properly repositioning the tag on the reader will. – Michael Roland Jan 09 '14 at 19:53
  • Given that this issue happens for each reader on each system, and with different RFID cards, I am inclined to think that it is not a positioning issue (but it very well could be). Can you suggest any further possibilities, or maybe a way of digging further as to what the problem might be? At this point, I am lost as to what step to take next. And again, thank you for your ongoing help! – mikem419 Jan 09 '14 at 20:11