0

我正在尝试编写一些集成 RFID 阅读器的代码,并且我已经取得了一些进展,但我仍在努力研究如何设置某种等待时间。

基本上,我有一个连接到越狱 iPhone 的 RFID 阅读器,并且我创建了一个存储 RFID 徽章编号的“NSMutableString”,但问题是当用户单击“添加RFID”按钮时串行端口被打开应用程序,我不知道如何设置循环或类似性质的东西来等待用户扫描他们的 RFID 徽章,这样他们就可以将徽章与用户帐户相关联,并将 RFID 徽章 # 放入“NSMutableString”。

如果您对我如何实现这一点有任何想法,我不介意看一些代码示例。

到目前为止,这就是我所拥有的,

 /* 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 */
4

1 回答 1

0

昨天在朋友的帮助下,我想出了这段代码。以下代码将捕获的 RFID tagID 放入位于 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
于 2012-12-21T18:34:42.737 回答