Converting RFID tag (unsigned char) to NSString

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

Converting RFID tag (unsigned char) to NSString

Postby intomo » Sun Jun 26, 2011 11:05 am

I've been able to take the RFID sample and adapt it for iOS.
I'm having one problem.

Code: Select all

- (void)RFIDTag:(unsigned char *)tag
{

   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   NSString *myTag = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x\n",tag[0],tag[1],tag[2],tag[3],tag[4]];

   NSLog(@"%s",__FUNCTION__);

   NSLog(@"currentTag: %@",myTag);
   
   [displayTxt setText:myTag];

   [pool release];
      
}


When I try to setText with myTag I get a memory leak and a crash reported from the phidgets logging.

There error I get is:
2011-06-26 12:26:58.123 FPBAdmin[1888:6603] bool _WebTryThreadLock(bool), 0x64059b0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...


It's working fine as long as I only use NSLog.

I assume the problem is a memory leak when converting between the unsigned char and NSString. In the MacOS version setWithMemnonic was used which isn't available in iOS. Can anyone point me in the right direction? Thanks in advance.

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

Re: Converting RFID tag (unsigned char) to NSString

Postby Patrick » Tue Jun 28, 2011 9:17 am

On iOS, you MUST always access the GUI from only the main thread. This means that if you want to write to the GUI from a Phidgets event, you need to use 'performSelectorOnMainThread' to call a selector (probably your RFIDTag method) in the main thread context. You can see this at work in the example.

-Patrick

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

Re: Converting RFID tag (unsigned char) to NSString

Postby intomo » Tue Jun 28, 2011 4:57 pm

Patrick,

Thanks for the answer.
I'm closer, and understand the errors I was getting before now, but still having trouble passing parameters.
Please tell me what I'm doing wrong.
I'm almost there.

Here's where I open the PhidgetRFID

Code: Select all

- (void)initRFID:(NSTimer*)timer
{
   NSLog(@"%s",__FUNCTION__);
   [self trace:@"Init RFID"];
   
   CPhidget_enableLogging(PHIDGET_LOG_ERROR, NULL);
      //CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
      //CPhidget_enableLogging(PHIDGET_LOG_DEBUG, NULL);
   CPhidgetRFID_create(&rfid);
   
   CPhidgetRFID_set_OnTag_Handler(rfid, gotTag, self);
      //CPhidgetRFID_set_OnTagLost_Handler(rfid, lostTag, self);
   CPhidget_set_OnAttach_Handler((CPhidgetHandle)rfid, gotAttach, self);
   CPhidget_set_OnDetach_Handler((CPhidgetHandle)rfid, gotDetach, self);
   
   CPhidget_openRemote((CPhidgetHandle)rfid, 32761, "main", NULL);
}

int gotTag(CPhidgetRFIDHandle phid, void *context, unsigned char *tag) {
   Phidget *phidget = [[Phidget alloc] initWithPhidgetHandle:phid];
   [(id)context performSelectorOnMainThread:@selector(onTagReceived:)
                          withObject:phidget
                        waitUntilDone:YES];
   [phidget release];
   return 0;
}

- (void)onTagReceived:(unsigned char *)tag
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   NSString *myTag = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x\n",tag[0],tag[1],tag[2],tag[3],tag[4]];
   NSLog(@"Tag: %@",myTag);
   [pool release];
}



Many thanks again.

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

Re: Converting RFID tag (unsigned char) to NSString

Postby intomo » Tue Jun 28, 2011 6:54 pm

Nevermind.
I figured it out.
Thanks anyway.
For anyone else following this:

Code: Select all

- (void)initRFID:(NSTimer*)timer
{
   self.RFIDLock = NO;
   NSLog(@"%s",__FUNCTION__);
   [self trace:@"Init RFID"];
   
      //CPhidget_enableLogging(PHIDGET_LOG_ERROR, NULL);
   CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
      //CPhidget_enableLogging(PHIDGET_LOG_DEBUG, NULL);
   CPhidgetRFID_create(&rfid);
   
   CPhidgetRFID_set_OnTag_Handler(rfid, gotTag, self);
   CPhidgetRFID_set_OnTagLost_Handler(rfid, lostTag, self);
   CPhidget_set_OnAttach_Handler((CPhidgetHandle)rfid, gotAttach, self);
   CPhidget_set_OnDetach_Handler((CPhidgetHandle)rfid, gotDetach, self);
   
   CPhidget_openRemote((CPhidgetHandle)rfid, 32761, "main", NULL);
}

Code: Select all

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 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;
}

int lostTag(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(onTagLost:)
                          withObject:myTag
                        waitUntilDone:YES];
   [myTag release];
   return 0;
}

Code: Select all

- (void)onTagReceived:(NSString *)tag
{
   NSLog(@"%s",__FUNCTION__);
   NSLog(@"Tag: %@",tag);
   NSString *msg = [NSString stringWithFormat:@"%@%@",@"Tag Received: ",tag];
   [self trace:msg];
}

- (void)onTagLost:(NSString *)tag
{
   NSLog(@"%s",__FUNCTION__);
   NSLog(@"Tag: %@",tag);
   NSString *msg = [NSString stringWithFormat:@"%@%@",@"Tag Lost: ",tag];
   [self trace:msg];
}


No leaks, no crashes. Thanks.

gwilkinson
Phidgetsian
Posts: 8
Joined: Thu Sep 01, 2011 5:00 pm
Contact:

Re: Converting RFID tag (unsigned char) to NSString

Postby gwilkinson » Mon Feb 11, 2013 3:50 pm

If you could email me your sample I would be greatful... I am a novice in iOS switching gears to monitor an already running program with an RFID and Load sensor setup.

If I had something to pick apart it would help me along quite a bit...


Return to “macOS”

Who is online

Users browsing this forum: No registered users and 0 guests