使用NSTASK进行交互式访问命令



我正在使用nstask运行Interactive中的CLI应用程序。我正在使用它在USB连接上拉数据。

我正在打开一个带有轮廓和错误管道的NSTASK,但是当我发出命令时,它可以正常工作,但是在循环中旋转海滩球,从输出中提取数据。

我希望能够捣碎 - (ibaction)单击:(id)执行help命令并取回输出的发件人按钮:

NSTask *usbCommandTask;
NSPipe *outPipe;
NSPipe *inPipe;
NSPipe *errorPipe;
NSFileHandle *inFile;
NSFileHandle *outFile;
NSTimer *pollTimer;
dispatch_queue_t mtpTask;
@implementation AppDelegate

- (void)commandNotification:(NSNotification *)notification
{
    NSData *data = nil;
    while ((data = [outFile availableData]) && [data length]){
        NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Data: %@",myString);
        }
    NSLog(@"Execution never gets here");
}
-(void)checkTask
{
    if(usbCommandTask){
        if([usbCommandTask isRunning])NSLog(@"Task running"); else NSLog(@"Task dead");
    } else NSLog(@"Task is nil");

}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    usbCommandTask = [[NSTask alloc] init];
    [usbCommandTask setLaunchPath:@"/Applications/usb-debugger-cli"];
    [usbCommandTask setCurrentDirectoryPath:@"/"];
    [usbCommandTask setArguments:[NSArray arrayWithObject:@"-i"]];
    inPipe = [NSPipe pipe];
    [usbCommandTask setStandardInput:inPipe];


    outPipe = [NSPipe pipe];
    [usbCommandTask setStandardOutput:outPipe];
    errorPipe = [NSPipe pipe];
    [usbCommandTask setStandardError:errorPipe];
    outFile = [outPipe fileHandleForReading];
    inFile = [inPipe fileHandleForWriting];
     [outFile waitForDataInBackgroundAndNotify];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(commandNotification:)
                                                 name:NSFileHandleDataAvailableNotification
                                               object:nil];

    pollTimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(checkTask) userInfo:nil repeats:TRUE];
    [usbCommandTask launch];
    NSLog(@"Launched");

}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
    if(usbCommandTask)[usbCommandTask terminate];

}

- (IBAction)clicked:(id)sender {
    if(usbCommandTask){
    NSString *command=@"helpn";
    NSData *commandData=[NSData dataWithBytes:command.UTF8String length:command.length];
    [inFile writeData:commandData];
    }
    else
    {NSLog(@"Comamnd task dead");
    }
}
[outFile availableData] 

实际上是一个封锁调用,没有数据或明确的EOF,所以这总是会阻止,而是我使用了:

data = [outFile availableData];
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",myString);
[outFile waitForDataInBackgroundAndNotify];

最新更新