1

Ok, here comes my problem. I have a program that tells me the value of a sensor, this program runs in a terminal and the output is just like that:

110

362

492

655

and so on....indefinitely with a rate of 4 lines (status) per second. Then i need to display this values in real time in a level bar in my objective c program. I try to use this code Execute a terminal command from a Cocoa app which is basically the use of nstask and pipes. I realize that the program gets stuck when reaches data = [file readDataToEndOfFile]; I think is because is waiting to finish the program or the output when this never ends. So, is there a way to get line by line in real time the status of this sensor?

Here is my code, i just change the command for ping google.com due to the same endless output and similar rate.

- (void) epocSender
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/sbin/ping"];
NSArray *arguments;arguments = [NSArray arrayWithObjects: @"google.com", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData:) name:NSFileHandleDataAvailableNotification object:file];
[file waitForDataInBackgroundAndNotify];
}

- (void)receivedData:(NSNotification *)notif {
    NSLog(@"notification received");
}
Community
  • 1
  • 1

1 Answers1

2

Get the NSFileHandle for reading from your pipe and use waitForDataInBackgroundAndNotify to be notified of new data. Each time you receive the notification, call waitForDataInBackgroundAndNotify again to re-register.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Ive tried already (for 3days) using readInBackgroundAndNotify but somehow I've never get the notification so i cant call the nsnotification method. I got a hint how to use it from this posthttp://stackoverflow.com/questions/6931732/nstasks-real-time-output/6931865#comment8260186_6931865  .. Please help me! – Sergio García Oct 23 '13 at 19:05
  • I've updated to `waitForDataInBackgroundAndNotify` because there is always a delay before the next set of data will be available. If you still have issues, show your code. – Wain Oct 23 '13 at 22:28