bool attach( CPhidgetRFIDHandle &phid, int serial_number) { 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_OnTag_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_OnTagLost_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 ROS_INFO("Waiting for RFID sensor to be attached...."); if ((result = CPhidget_waitForAttachment((CPhidgetHandle)rfid, 10000))) { CPhidget_getErrorDescription(result, &err); ROS_INFO("Problem waiting for RFID sensor attachment: %s\n", err); return false; } else return true; }
void RFIDreader::setup() { #if ENABLE_RFID if(cfg().test){ int result; const char *err; tagAvailable=false; 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_OnTag_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_OnTagLost_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, 2000))) { CPhidget_getErrorDescription(result, &err); printf("Problem waiting for attachment: %s\n", err); } CPhidgetRFID_setAntennaOn(rfid, 1); } #endif }
int rfid_simple() { int result; const char *err; //Declare an RFID handle CPhidgetRFIDHandle rfid = 0; printf("RFIDHandler\n"); //create the RFID object CPhidgetRFID_create(&rfid); printf("Create\n"); //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); printf("device EVENT Handlers\n"); //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_OnTag_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_OnTagLost_Handler(rfid, TagLostHandler, NULL); printf("RFID EVENT Handlers\n"); //open the RFID for device connections CPhidget_open((CPhidgetHandle)rfid, -1); printf("OPENED\n"); sleep(1); //get the program to wait for an RFID device to be attached printf("Waiting for RFID to be attached....\n"); if((result = CPhidget_waitForAttachment((CPhidgetHandle)rfid, 10000))) { CPhidget_getErrorDescription(result, &err); printf("Problem waiting for attachment: %s\n", err); return 0; } //Display the properties of the attached RFID device display_properties(rfid); 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(); //toggle the digital output (when making this example I had an LED plugged into the digital output index 0 CPhidgetRFID_setOutputState(rfid, 0, 1); //keep displaying RFID event data until user input is read printf("Press any key to continue\n"); getchar(); //toggle the digital output (when making this example I had an LED plugged into the digital output index 0 CPhidgetRFID_setOutputState(rfid, 0, 0); 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; }