开发者

How to run the "screen" command from NSTask?

I want to monitor a virtual COM port (Arduino RFID) on my Mac. I can run "screen /dev/tty.serialnumber" from the terminal, and it outputs the RFID serial number when I swipe it.

As soon as I try it from Xcode with an NSTask I get the following output.

Must be connected to a terminal.

Here's my code:

NSTask *cd = [[NSTask alloc] init];

[cd setLaunchPath:@"/usr/bin/screen"];
[cd setArguments:[NSArray arrayWithObjects:@"-L",@"/dev/tty.usbserial-A800509K",nil]];

NSPipe *pipe;
pipe = [NSPipe pipe];
[cd setStandardOutput: pipe];
[cd setStandardInput:[NSPipe pipe]];

NSFileHandle *file;
file = [pipe fileHandleFo开发者_StackOverflow社区rReading];

[cd launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

NSLog (@"%@", string);

[cd waitUntilExit];
[cd release];


I think you'd better directly access the COM port, either by using the foundation libraries, or by using third party Obj-C libs (for example, https://github.com/pbosetti/PBSerialPort). Also, if you want to monitor the COM port, you will have to set a thread reading the serial port, and updating a text area in the UI. Remember that secondary thread should update the UI via the method - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜