0

I am trying to write some code that integrates an RFID reader, and I have gotten kind of far with it, but I am still struggling on how I can set a wait time of some sort.

Basically, I have an RFID reader that is hooked up to a jailbroken iPhone, and I have created an "NSMutableString" that stores the RFID Badge number, but the problem is the serial port is opened when the user clicks the "AddRFID" button within the app, and I can't figure out how I would setup a loop or something of that nature to wait for the user to scan their RFID badge, so they can associate a badge with a user account, and put the RFID badge # in the "NSMutableString".

If you have any ideas on how I could accomplish this, I wouldn't mind see some code examples.

So far this is what I have,

 /* Begin addRFID - Serial Communication */

- (IBAction)addRFID:(id)sender {

   // btnAddRFID pressed

   NSLog(@"rfid badge # is %@",rfidbadgenumber);

  // launch an alert with text input
   UIAlertView *alertrfid = [[UIAlertView alloc] initWithTitle:@"Scan RFID badge"
                                                    message:@"Associate RFID badge with user account"
                                                   delegate:self
                                          cancelButtonTitle:@"Dismiss"
                                          otherButtonTitles:@"Save", nil];



// set alert with a text input field
[alertrfid setAlertViewStyle:UIAlertViewStylePlainTextInput];
// set text field input to max character length of 10
//[[alertrfid textFieldAtIndex:0].text substringWithRange:NSMakeRange(0,10)];



    [alertrfid show];


    // set the delegate for the UIAlertView textfield
    [alertrfid textFieldAtIndex:0].delegate = self;



     //open serial port
     [serial open:B2400];

     NSLog(@"rfid badge # is: %@",rfidbadgenumber);


    // assign alert input text to RFID badge #
    [alertrfid textFieldAtIndex:0].text = rfidbadgenumber;


  }

  # pragma mark - JailbrokenSerialDelegate
  - (void) JailbrokenSerialReceived:(char) ch {

    NSString *rfidbadgenumbernew = [[NSString alloc]init];

    NSLog(@"rfidbadgenumbernew is: %@",rfidbadgenumbernew);

    // put serial received data in a char buffer
    [rfidbadgenumber stringByReplacingOccurrencesOfString:rfidbadgenumber withString:rfidbadgenumbernew];

     NSLog(@"serialrecvd -  rfidbadgenumber is: %@",rfidbadgenumber);

    //[rfidbadgenumber appendFormat:@"%c", ch];

    //[rfidbadgenumber stringByReplacingOccurrencesOfString:rfidbadgenumber withString:rfidbadgenumbernew];
  }

  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSLog(@"Range: %@", NSStringFromRange(range));
return (textField.text.length - range.length + string.length <= 10);
}

 // add method for cancel button
  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

  if (buttonIndex == 0) {
    NSLog(@"The cancel button was clicked");

    rfidbadgenumber = NULL;

    //rfidbadgenumbernew = NULL;

    [serial close];
 } 

 // do stuff for additonal buttons
}

   // close serial port


  /* End addRFID - Serial Communication */
dsolimano
  • 8,870
  • 3
  • 48
  • 63
ipatch
  • 3,933
  • 8
  • 60
  • 99

1 Answers1

0

With the help of a friend yesterday I came up with this code. The following code puts the captured RFID tagID into a textfield located in a UIAlertView.

/* Begin addRFID - Serial Communication */

- (IBAction)addRFID:(id)sender {

// btnAddRFID pressed

NSLog(@"rfid badge # is %@",newrfidtagid);


// set alert with a text input field
[alertrfid setAlertViewStyle:UIAlertViewStylePlainTextInput];
// set text field input to max character length of 10
//[[alertrfid textFieldAtIndex:0].text substringWithRange:NSMakeRange(0,10)];

[alertrfid show];

// set the delegate for the UIAlertView textfield
[alertrfid textFieldAtIndex:0].delegate = self;


//open serial port

[serial open:B2400];
if(serial.isOpened)
{
    NSLog(@"Open");
}
else NSLog(@"closed dingo");
}


 # pragma mark - JailbrokenSerialDelegate
 - (void) JailbrokenSerialReceived:(char) ch {

NSLog(@"got it");

NSString *s = [NSString stringWithFormat:@"%c",ch];
NSLog(@"s = %@",s);

[newrfidtagid appendString:s];

NSLog(@"rfid char  = %@",newrfidtagid);

if (newrfidtagid.length == 10)
{
    NSLog(@"new tagid = %@",newrfidtagid);
    [alertrfid textFieldAtIndex:0].text = newrfidtagid;
    //[serial open:B2400];
  }
  }

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSLog(@"Range: %@", NSStringFromRange(range));
return (textField.text.length - range.length + string.length <= 10);
}

// add method for cancel button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 0) {
    NSLog(@"The cancel button was clicked");

    [serial close];

    [alertrfid dismissWithClickedButtonIndex:0 animated:YES];
    [newrfidtagid setString:@""];
    [alertrfid textFieldAtIndex:0].text = @"";

    //alertrfid.hidden = TRUE;

}

// do stuff for additonal buttons
}

// close serial port
ipatch
  • 3,933
  • 8
  • 60
  • 99