Programmation C pour lecteur RFID
Répondre à la discussion
Affichage des résultats 1 à 2 sur 2

Programmation C pour lecteur RFID



  1. #1
    mich-mich

    Programmation C pour lecteur RFID


    ------

    Bonjour,

    Je travaille actuellement sur un projet RFID pour un client. Il faut que je reconnaisse le propriétaire s'approchant d'une porte pour ensuite mettre sous tension l'atelier. J'ai déjà la partie hardware et je reste bloquer sur la partie programmation en C. Il faut savoir que le lecteur RFID sera piloté par un PIC24F sur une carte à proximité du lecteur.
    J'ai, avec le lecteur RFID, la SDK fournie. Mon problème est que je ne comprends pas comment piloter le lecteur avec ce SDK, on me donne des fonctions pour appeler le module, par exemple:
    Code:
    /** @fn int NurApiTuneAntenna(HANDLE hApi, int antenna, BOOL wideTune, BOOL bSaveResults, int *dBmResults)
     * Executes an antenna tune.
     *
     * @param hApi            	Handle to valid NurApi object instance
     * @param antenna       	Antenna to use. Value is 0...3.
     * @param wideTune			If set to TRUE the tuning is done in wider range opposing to FALSE which 'fast tune'
     * @param bSaveResults		If set to TRUE then the tuning results will be stored into the module's non-volatile memory.
     * @param dBmResults		Pointer to 6 integer values. The reflected power will be stored into these values in format dBm * 1000.
     *							This parameter may be NULL.
     *
     * @return	Zero when succeeded, on error non-zero error code is returned.
     */
    NUR_API
    int NURAPICONV NurApiTuneAntenna(HANDLE hApi, int antenna, BOOL wideTune, BOOL bSaveResults, int *dBmResults);
    Seulement je ne vois pas comment utiliser cette fonction sur mon programme dans le PIC24F pour dire que je veux l'antenne sur le port 3...
    Je vous remercie pour la moindre précision/aide apportée!!

    La datasheet fourni:

    -----
    Images attachées Images attachées
    Dernière modification par mich-mich ; 26/05/2014 à 09h46.

  2. #2
    mich-mich

    Re : Programmation C pour lecteur RFID

    Bonjour,

    Ne pas hésiter pour donner le moindre avis...
    Je rajoute une partie des codes qui permet d'utiliser le module:

    Code:
    /**
     * Data sent with notification NUR_NOTIFICATION_TRIGGERREAD.
     * @sa NUR_NOTIFICATION_TRIGGERREAD, enum NUR_NOTIFICATION
     */
    struct NUR_TRIGGERREAD_DATA
    {
    	BOOL sensor;	/**< TRUE if notification source is sensor, otherwise FALSE and source is GPIO. */
    	int source;		/**< Sensor/GPIO source number. */
    	int antennaID;	/**< ID of antenna where tag was read. */
    	int rssi;		/**< Tag RSSI, -127 if tag could not be found. */
    	int scaledRssi; /**< Tag RSSI scaled to 0% - 100%. */
    	int epcLen;		/**< Number of bytes stored in epc field, zero if tag could not be found. */
    	BYTE epc[NUR_MAX_EPC_LENGTH];	/**< Tag EPC data. */
    };
    
    /** @fn int NurApiFree(HANDLE hApi)
     * Free allocated NurApi object.
     * @param	hApi			Handle to valid NurApi object instance.
     * @return	Zero when succeeded, On error non-zero error code is returned.
     */
    NUR_API
    int NURAPICONV NurApiFree(HANDLE hApi);
    
    /** @fn int NurApiGetReaderInfo(HANDLE hApi, struct NUR_READERINFO *ri, DWORD riSize)
     * Get general information about the reader.
     * 
     * @sa NurApiGetRegionInfo()
     *
     * @param	hApi	Handle to valid NurApi object instance
     * @param	ri		Pointer to the NUR_READERINFO structure.
     * @param	riSize	Size of <i>ri</i> structure in bytes. Usually: sizeof(struct NUR_READERINFO).	
     * 						
     * @return	Zero when succeeded, On error non-zero error code is returned.
     */
    NUR_API
    int NURAPICONV NurApiGetReaderInfo(HANDLE hApi, struct NUR_READERINFO *ri, DWORD riSize);
    
    /** @fn int NurApiScanSingle(HANDLE hApi, int timeout, struct NUR_TRIGGERREAD_DATA *resp)
     * The single scan is used to read a single tag's EPC contents using a timeout defined in milliseconds.
     * 
     * @remarks If there's more than one tag in the reader range, this function will most likely fail.
     * 			
     * @sa NUR_TRIGGERREAD_DATA, NurApiSimpleInventory()
     * 
     * @param	hApi		Handle to valid NurApi object instance.
     * @param	timeout		Timeout in milliseconds. Range 50 - 2000.
     * @param	resp		Pointer to a structure that receives read tag information.
     * 			
     * @return	Zero when succeeded, On error non-zero error code is returned.
     */
    NUR_API
    int NURAPICONV NurApiScanSingle(HANDLE hApi, int timeout, struct NUR_TRIGGERREAD_DATA *resp);
    
    /** @fn int NurApiSimpleInventory(HANDLE hApi, struct NUR_INVENTORY_RESPONSE *resp)
     * Perform Inventory command. 
     * This function uses rounds, Q and session values from current module settings.
     * 
     * @sa NurApiInventory(), NurApiInventorySelect32(), NurApiInventorySelectByEPC(), NUR_INVENTORY_RESPONSE
     * 
     * @param	hApi		Handle to valid NurApi object instance.
     * @param	resp		Pointer to a structure that receives inventory response.
     *
     * @return	Zero when succeeded, On error non-zero error code is returned.
     */
    NUR_API
    int NURAPICONV NurApiSimpleInventory(HANDLE hApi, struct NUR_INVENTORY_RESPONSE *resp);
    Et ci-dessous, un début de programme pour utiliser le module:

    Code:
    #include <NurApi.h>
    
    #define USE_USB_AUTO_CONNECT 1
    
    // Print error string and free API object
    int ShowErrorAndExit(HANDLE hApi, int error)
    {
    	TCHAR errorMsg[128+1]; // 128 + NULL
    
    	NurApiGetErrorMessage(error, errorMsg, 128);
    
    	_tprintf(_T("NurApi error %d: [%s]\r\n"), error, errorMsg);
    
    	// Free API object
    	NurApiFree(hApi);
    
    	return 1;
    }
    
    // Convert EPC byte array to string
    void EpcToString(BYTE *epc, DWORD epcLen, TCHAR *epcStr)
    {
    	DWORD n;
    	int pos = 0;
    	for (n=0; n<epcLen; n++) {
    		pos += _stprintf_s(&epcStr[pos], NUR_MAX_EPC_LENGTH-pos, _T("%02x"), epc[n]);
    	}
    	epcStr[pos] = 0;
    }
    
    // Perform inventory
    int PerformInventory(HANDLE hApi)
    {
    	int rounds = 3;
    	int error = 0;
    	int tagsFound = 0, tagsMem = 0;
    	int tagCount = 0;
    	int idx = 0;
    	HANDLE hStorage = NULL;
    	HANDLE hTag = NULL;
    	struct NUR_INVENTORY_RESPONSE invResp;
    	struct NUR_TAG_DATA tagData;
    	TCHAR epcStr[128];
    
    	// Clear previously inventoried tags from memory
    	error = NurApiClearTags(hApi);
    	if (error != NUR_NO_ERROR)
    	{
    		// Failed
    		return error;
    	}
    
    	_tprintf(_T("Perform inventory ."));
    
    	while (rounds-- > 0)
    	{		
    		_tprintf(_T("."));
    		
    		// Perform inventory with 500ms timeout, Q 3, session 0
    		//error = NurApiInventory(hApi, 500, 3, 0, &invResp);
    
    		// Perform simple inventory
    		error = NurApiSimpleInventory(hApi, &invResp);
    		if (error != NUR_NO_ERROR && error != NUR_ERROR_NO_TAG)
    		{
    			// Failed
    			return error;
    		}
    	}
    
    	_tprintf(_T(". %d tags found\r\n"), invResp.numTagsMem);
    
    	// Fetch tags from module, including tag meta
    	error = NurApiFetchTags(hApi, TRUE, NULL);
    	if (error != NUR_NO_ERROR)
    	{
    		// Failed
    		return error;
    	}
    
    	error = NurApiGetTagCount(hApi, &tagCount);
    	if (error != NUR_NO_ERROR)
    	{
    		// Failed
    		return error;
    	}
    
    	// Loop through tags
    	for (idx = 0; idx < tagCount; idx++)
    	{		
    		error = NurApiGetTagData(hApi, idx, &tagData);
    
    		if (error == NUR_NO_ERROR)
    		{
    			EpcToString(tagData.epc, tagData.epcLen, epcStr);
    
    			// Print tag info
    			_tprintf(_T("Tag info:\r\n"));
    			_tprintf(_T("  EPC: [%s]\r\n"), epcStr);
    			_tprintf(_T("  EPC length: %d\r\n"), tagData.epcLen);
    			_tprintf(_T("  RSSI: %d\r\n"), tagData.rssi);
    			_tprintf(_T("  Timestamp: %u\r\n"), tagData.timestamp);
    			_tprintf(_T("  Frequency: %u\r\n"), tagData.freq);
    			_tprintf(_T("  PC bytes: %04x\r\n"), tagData.pc);
    		}
    		else
    		{
    			// Print error
    			TCHAR errorMsg[128+1]; // 128 + NULL    
    			NurApiGetErrorMessage(error, errorMsg, 128);
    			_tprintf(_T("NurApiTSGetTagData() returns error %d: [%s]\r\n"), error, errorMsg);
    		}
    	}
    
    	return NUR_NO_ERROR;
    }
    
    int PrintModuleVersion(HANDLE hApi)
    {
    	struct NUR_READERINFO info;
    	int error;
    
    	error = NurApiGetReaderInfo(hApi, &info, sizeof(info));
    	if (error == NUR_NO_ERROR)
    	{
    		_tprintf(_T("Module Version: [%d.%d-%c]\r\n\r\n"), info.swVerMajor, info.swVerMinor, info.devBuild);
    	}
    	return error;
    }
    
    void NURAPICALLBACK MyNotificationFunc(HANDLE hApi, DWORD timestamp, int type, LPVOID data, int dataLen)
    {
    	switch (type)
    	{
    	case NUR_NOTIFICATION_LOG:
    		{
    			const TCHAR *logMsg = (const TCHAR *)data;
    			_tprintf(_T("LOG: %s\r\n"), logMsg);
    		}
    		break;
    
    	case NUR_NOTIFICATION_MODULEBOOT:
    		_tprintf(_T("Module booted\r\n"));
    		break;
    
    	case NUR_NOTIFICATION_TRCONNECTED:
    		_tprintf(_T("Transport connected\r\n"));
    		break;
    
    	case NUR_NOTIFICATION_TRDISCONNECTED:
    		_tprintf(_T("Transport disconnected\r\n"));
    		break;
    
    	default:
    		_tprintf(_T("Unhandled notification: %d\r\n"), type);
    		break;
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int error = 0;
        
        HANDLE hApi = INVALID_HANDLE_VALUE;
    
        // Create new API object
        hApi = NurApiCreate();
        if (hApi == INVALID_HANDLE_VALUE)
        {
    		return 1;
        }
    
    	// Set notification callback
    	NurApiSetNotificationCallback(hApi, MyNotificationFunc);
    
    	// Set full log level, w/o data
    	// NurApiSetLogLevel(hApi, NUR_LOG_ALL & ~NUR_LOG_DATA);
    	
    	NurApiSetLogLevel(hApi, NUR_LOG_ERROR);
    
    #ifdef USE_USB_AUTO_CONNECT
    	NurApiSetUsbAutoConnect(hApi, TRUE);
    	error = NurApiIsConnected(hApi);
    #else
        // Connect API to module through serial port
        error = NurApiConnectSerialPort(hApi, 10, NUR_DEFAULT_BAUDRATE);
    #endif
        if (error != NUR_NO_ERROR)
        {
    		// Handle error some way, in this example we just print error out and exit.
    		return ShowErrorAndExit(hApi, error);
        }
    
    	error = NurApiPing(hApi, NULL);
    	if (error != NUR_NO_ERROR)
        {
    		return ShowErrorAndExit(hApi, error);
        }
    
        // API ready
        
        error = PrintModuleVersion(hApi);
        if (error != NUR_NO_ERROR)
        {
    		return ShowErrorAndExit(hApi, error);
        }
    
    	// Inventory
    	error = PerformInventory(hApi);
    	if (error != NUR_NO_ERROR)
        {
    		return ShowErrorAndExit(hApi, error);
        }
    
        // Free API object
        NurApiFree(hApi);
    
    	return 0;
    }
    Merci d'avance ( je cherche juste une explication du fonctionnement )

Discussions similaires

  1. Lecteur RFID
    Par invite9b4bf224 dans le forum Électronique
    Réponses: 1
    Dernier message: 26/03/2014, 08h16
  2. Quel lecteur RFID pour mon projet ?
    Par invite91b69453 dans le forum Électronique
    Réponses: 7
    Dernier message: 22/08/2013, 10h56
  3. Création d'une antenne RFID 13,56MHz pour lecteur
    Par invite97743677 dans le forum Électronique
    Réponses: 9
    Dernier message: 22/02/2011, 09h51
  4. [RFID] Cherche soft pour lecture carte RFID
    Par AG664 dans le forum Électronique
    Réponses: 4
    Dernier message: 07/05/2010, 17h26
  5. lecteur RFID et µC
    Par invite35e8c515 dans le forum Électronique
    Réponses: 0
    Dernier message: 14/09/2009, 15h02
Dans la rubrique Tech de Futura, découvrez nos comparatifs produits sur l'informatique et les technologies : imprimantes laser couleur, casques audio, chaises gamer...