0
我想在我的Mac上監視虛擬COM端口(Arduino RFID)。我可以從終端運行「screen /dev/tty.serialnumber」,並在刷卡時輸出RFID序列號。如何從NSTask運行「screen」命令?
一旦我用NSTask從Xcode嘗試它,我會得到以下輸出。
必須連接到終端。
這裏是我的代碼:
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 fileHandleForReading];
[cd launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"%@", string);
[cd waitUntilExit];
[cd release];
這就是'screen(1)'告訴你它只能從終端運行。你不能使用其他程序或直接讀取/ dev/tty嗎?看到這個問題:http://stackoverflow.com/questions/2504714/reading-serial-data-from-c-osx-dev-tty – 2011-05-11 03:18:52