trouble with PhidgetManager and multiple devices in iOS

Supporting iOS 8 and up
intomo
Phidgetly
Posts: 24
Joined: Tue May 13, 2008 11:00 pm

trouble with PhidgetManager and multiple devices in iOS

Postby intomo » Wed Oct 17, 2012 2:38 pm

I am using the PhidgetManager example to connect to multiple PhidgetRFID sensors. That all works well except I'm having trouble storing them in an array in iOS.
This example discovers all the connected Phidgets as expected. However, once PhidgetRFIDs are found and I try to create and remoteOpen each, the RFIDAdded never fires.
Can someone tell me what I'm doing wrong?

Please look in the phidgetAdded method. Thanks in advance.

Code: Select all

#import "RootViewController.h"


int gotAttach(CPhidgetHandle phid, void *context) {
   Phidget *phidget = [[Phidget alloc] initWithPhidgetHandle:phid];
   [(id)context performSelectorOnMainThread:@selector(phidgetAdded:)
                     withObject:phidget
                  waitUntilDone:YES];
   [phidget release];
   return 0;
}

int gotDetach(CPhidgetHandle phid, void *context) {
   Phidget *phidget = [[Phidget alloc] initWithPhidgetHandle:phid];
   [(id)context performSelectorOnMainThread:@selector(phidgetRemoved:)
                          withObject:phidget
                        waitUntilDone:YES];
   [phidget release];
   return 0;
}

int gotRFIDAttach(CPhidgetHandle phid, void *context) {
   Phidget *phidget = [[Phidget alloc] initWithPhidgetHandle:phid];
   [(id)context performSelectorOnMainThread:@selector(RFIDAdded:)
                                  withObject:phidget
                               waitUntilDone:YES];
   [phidget release];
   return 0;
}

int gotRFIDDetach(CPhidgetHandle phid, void *context) {
   Phidget *phidget = [[Phidget alloc] initWithPhidgetHandle:phid];
   [(id)context performSelectorOnMainThread:@selector(RFIDRemoved:)
                          withObject:phidget
                        waitUntilDone:YES];
   [phidget release];
   return 0;
}

static int gotTag(CPhidgetRFIDHandle phid, void *context, unsigned char *tag) {
    NSString *myTag = [[NSString alloc] initWithFormat:@"%02x%02x%02x%02x%02x",tag[0],tag[1],tag[2],tag[3],tag[4]]; 
   [(id)context performSelectorOnMainThread:@selector(onTagReceived:)
                          withObject:myTag
                        waitUntilDone:YES];
   [myTag release];
   return 0;
}

@implementation RootViewController
CPhidgetRFIDHandle rfid = 0;


- (void)viewDidLoad {
    [super viewDidLoad];
    devices = [[NSMutableArray alloc] init];
    sensorNum = 0;
   
   CPhidgetManager_create(&phidMan);
   CPhidgetManager_set_OnAttach_Handler(phidMan, gotAttach, self);
   CPhidgetManager_set_OnDetach_Handler(phidMan, gotDetach, self);   
   CPhidgetManager_openRemote(phidMan, NULL, NULL);
}

- (void)phidgetAdded:(Phidget *)phidget
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *shortName = [[phidget.name substringFromIndex:8] substringToIndex:4];
    if ([shortName isEqualToString:@"RFID"])
    {
        [devices insertObject:[NSNumber numberWithInt: CPhidgetRFID_create(&rfid)] atIndex:sensorNum];
        CPhidgetRFID_set_OnTag_Handler((CPhidgetRFIDHandle)[[devices objectAtIndex:sensorNum] intValue], gotTag, self);
        CPhidget_set_OnAttach_Handler((CPhidgetHandle)[[devices objectAtIndex:sensorNum] intValue], gotRFIDAttach, self);
        CPhidget_set_OnDetach_Handler((CPhidgetHandle)[[devices objectAtIndex:sensorNum] intValue], gotRFIDDetach, self);
        CPhidget_openRemote((CPhidgetHandle)[[devices objectAtIndex:sensorNum] intValue], phidget.serialNumber,[phidget.serverID UTF8String], NULL);
        NSLog(@"devices count: %d: ",[devices count]);
         sensorNum ++;
       
    }     
    [pool release];

}

- (void)RFIDAdded:(Phidget *)phidget
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"RFID added: %@   serial: %d",phidget.name, phidget.serialNumber);
    [pool release];
   
}


- (void)onTagReceived:(NSString *)tag
{
   NSLog(@"Tag: %@",tag);
}


User avatar
Patrick
Lead Developer
Posts: 3140
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: trouble with PhidgetManager and multiple devices in iOS

Postby Patrick » Wed Oct 17, 2012 2:55 pm

Code: Select all

[devices insertObject:[NSNumber numberWithInt: CPhidgetRFID_create(&rfid)] atIndex:sensorNum];

Is simply going to insert an NSNumber with value 0 into devices - because you are using the return value of CPhidgetRFID_create. Try:

Code: Select all

CPhidgetRFIDHandle rfid = 0;
CPhidgetRFID_create(&rfid);
[devices insertObject:[NSNumber numberWithInt: rfid] atIndex:sensorNum];

-Patrick

intomo
Phidgetly
Posts: 24
Joined: Tue May 13, 2008 11:00 pm

Re: trouble with PhidgetManager and multiple devices in iOS

Postby intomo » Wed Oct 17, 2012 4:11 pm

Thanks!
That fires the RFIDAdded.

Now what am I doing wrong with the turning the antenna on?
The following works fine on individual sensors.
Thanks in advance.

Code: Select all

- (void)RFIDAdded:(Phidget *)phidget
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"RFID added: %@   serial: %d",phidget.name, phidget.serialNumber);

   CPhidgetRFID_setLEDOn(rfid,PTRUE);
   CPhidgetRFID_setAntennaOn(rfid,PTRUE);
    [pool release];
   
}

User avatar
Patrick
Lead Developer
Posts: 3140
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: trouble with PhidgetManager and multiple devices in iOS

Postby Patrick » Thu Oct 18, 2012 9:05 am

You need to keep track of your phidget handles. Try using the handle from the phidget argument in the rfid attach event for calling the rfid methods. Your global rfid handle has not been initialized to anything.

-Patrick

intomo
Phidgetly
Posts: 24
Joined: Tue May 13, 2008 11:00 pm

Re: trouble with PhidgetManager and multiple devices in iOS

Postby intomo » Thu Oct 18, 2012 11:47 am

You mean like this:

Code: Select all

    for (int i=0; i<devices.count; i++ )
    {
        CPhidgetRFID_setAntennaOn((CPhidgetRFIDHandle)[[devices objectAtIndex:i] intValue],PTRUE);
        CPhidgetRFID_setLEDOn((CPhidgetRFIDHandle)[[devices objectAtIndex:i] intValue],PTRUE);
    }


This works. If I expanded the devices array to an array of dicts then I could look up each individually by serial number and add/remove them from the array.
Can you explain initializing the global RFID handle?

Thanks for your patience.


Return to “iOS”

Who is online

Users browsing this forum: No registered users and 1 guest