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