LAb #5

Lab # 5 Working with Phidgets devices

Description:

Ideally we are supposed to learn how to use the device and its functions.  For instance learning how to install the proper drivers and libraries on a Linux machine. I’ve covered this installation through the terminal in a word document that is attached to this page all you must do is open it and follow the instructions. Installing through terminal_Phidgets_libraries if this link is un-clickable just right click and open a new page to download my instructions. 

For the code i used the example codes provided by Phidgets.com and i checked to see what was not necessary for the code to be run-able so the code may look a bit different than the example but essentially does the same.

Code: RFID             PS: some pieces of code could have inadvertently been clipped.

// - RFID simple -
// This program simply displays the data that is generated by an RFID phidget in a very simple case and outputs it to the console.
// This simple example covers the basics of connecting and using an RFID phidget.
//
// Copyright 2008 Phidgets Inc.  All rights reserved.
// This work is licensed under the Creative Commons Attribution 2.5 Canada License. 
// view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/

#include 
#include 

int CCONV AttachHandler(CPhidgetHandle RFID, void *userptr)
{
  	int serialNo;
	    const char *name;

	  CPhidget_getDeviceName (RFID, &name);
	  CPhidget_getSerialNumber(RFID, &serialNo);
   	 printf("%s %10d attached!\n", name, serialNo);

     	return 0;
}

int CCONV DetachHandler(CPhidgetHandle RFID, void *userptr)
{
	 int serialNo;
  	const char *name;

	  CPhidget_getDeviceName (RFID, &name);
	  CPhidget_getSerialNumber(RFID, &serialNo);
	   printf("%s %10d detached!\n", name, serialNo);

	     return 0;
}

int CCONV ErrorHandler(CPhidgetHandle RFID, void *userptr, int ErrorCode, const ch   ar *unknown)
{
	  printf("Error handled. %d - %s\n", ErrorCode, unknown);
	   return 0;
}

int CCONV OutputChangeHandler(CPhidgetRFIDHandle RFID, void *usrptr, int Index, in  t State)
{
	if(Index == 0 || Index == 1)
	{
  		printf("Output: %d > State: %d\n", Index, State);
	}
	   return 0;
}
//needed for read...?
int CCONV TagHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhidget   RFID_Protocol proto)
{
	//turn on the Onboard LED
   	CPhidgetRFID_setLEDOn(RFID, 1);

   	printf("Tag Read: %s\n", TagVal);
	    return 0;
}
//needed for read...?
int CCONV TagLostHandler(CPhidgetRFIDHandle RFID, void *usrptr, char *TagVal, CPhi     dgetRFID_Protocol proto)
{
	//turn off the Onboard LED

	   CPhidgetRFID_setLEDOn(RFID, 0);

	   printf("Tag Lost: %s\n", TagVal);
	    return 0;
}

int rfid_simple()
{
	  int result;
	   const char *err;

	//Declare an RFID handle
	   CPhidgetRFIDHandle rfid = 0;

	//create the RFID object
	   CPhidgetRFID_create(&rfid);

	//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
	   CPhidget_set_OnAttach_Handler((CPhidgetHandle)rfid, AttachHandler, NULL);
	   CPhidget_set_OnDetach_Handler((CPhidgetHandle)rfid, DetachHandler, NULL);
	   CPhidget_set_OnError_Handler((CPhidgetHandle)rfid, ErrorHandler, NULL);

	//Registers a callback that will run if an output changes.
	//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
   	CPhidgetRFID_set_OnOutputChange_Handler(rfid, OutputChangeHandler, NULL);

	//Registers a callback that will run when a Tag is read.
	//Requires the handle for the PhidgetRFID, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
   	CPhidgetRFID_set_OnTag2_Handler(rfid, TagHandler, NULL);

	//Registers a callback that will run when a Tag is lost (removed from antenna read range).
	//Requires the handle for the PhidgetRFID, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
	  CPhidgetRFID_set_OnTagLost2_Handler(rfid, TagLostHandler, NULL);

	//open the RFID for device connections
   	CPhidget_open((CPhidgetHandle)rfid, -1);

	//get the program to wait for an RFID device to be attached
	   printf("Waiting for RFID to be attached....");
	if((result = CPhidget_waitForAttachment((CPhidgetHandle)rfid, 10000)))
	{
   		CPhidget_getErrorDescription(result, &err);
		   printf("Problem waiting for attachment: %s\n", err);
  		  return 0;
	}

	  CPhidgetRFID_setAntennaOn(rfid, 1);

	//read RFID event data
	   printf("Reading.....\n");

	//keep displaying RFID event data until user input is read
  	printf("Press any key to continue\n");
	   getchar();

	   printf("Press any key to end\n");
	    getchar();

	//since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
	  printf("Closing...\n");
	   CPhidget_close((CPhidgetHandle)rfid);
	   CPhidget_delete((CPhidgetHandle)rfid);

	//all done, exit
  	return 0;
}

int main(int argc, char* argv[])
{
	  rfid_simple();
  	return 0;
}

 

Screen shots:

RFID_Read_then_exit

 

I’ve found something interesting if you run the code and then let it read an ID it runs fine. However after this if you unplug the device then reinsert it the RFID reader will not be able to be detected by the currently running script–code.RFID_cantread_after_detach_then_atach

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *