Page 71 of 77 Copyright © Access-IS 2020
C. NFC module example code and API
functions
This section presents code snippets for the NFC module process flow (see Figure 9 on page 32).
The main API functions in are shown in red.
C.1 Initialise smartcard sub-system
// Try to establish the Smartcard sub-system context
while(SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hRFIDContext) !=
SCARD_S_SUCCESS)
{
Sleep(1000); // Wait for some time and retry
}
// Optional: List all the readers available in the smartcard sub-system
// If the reader name is already known then this step is not required. However, there may
// be more than one reader connected to the system. Hence it is recommended to list all the
// readers and select the one that is required
RcvLength = 128;
memset(Reader_Tracker_Buffer, 0 , sizeof(Reader_Tracker_Buffer));
while(SCardListReaders(hRFIDContext, NULL, (LPSTR)_Buffer, &RcvLength) != SCARD_S_SUCCESS)
{
Sleep(1000); // Wait for some time and retry
}
C.2 Poll for card arrival
if(SCardGetStatusChange(hRFIDContext, INFINITE, RdrState, 1) == SCARD_S_SUCCESS)
{
if(RdrState[0].dwEventState & SCARD_STATE_PRESENT)
{
MessageBox(“Card Present”);
}
else if(RdrState[0].dwEventState & SCARD_STATE_EMPTY)
{
MessageBox(“Card Removed”);
}
}
else
{
MessageBox(“Reader removed”);
}
C.3 Connect to the card
if(SCardConnect(hRFIDContext, _str, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0 |
SCARD_PROTOCOL_T1, &hCrd, &dwProtocol) != SCARD_S_SUCCESS)
{
// Unable to connect to card
MessageBox(“Unable to connect to card”);
}
else
{
// Connected to card
MessageBox(“Connected to card”);
}