2011-05-11 98 views
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]; 
+0

這就是'screen(1)'告訴你它只能從終端運行。你不能使用其他程序或直接讀取/ dev/tty嗎?看到這個問題:http://stackoverflow.com/questions/2504714/reading-serial-data-from-c-osx-dev-tty – 2011-05-11 03:18:52

回答

-1

我想你最好直接訪問COM端口,或者通過使用基礎庫,或通過第三方的OBJ-C庫(例如,https://github.com/pbosetti/PBSerialPort )。 此外,如果你想監視器的COM端口,你將不得不設置一個線程讀取串口,並更新UI中的文本區域。請記住,輔助線程應通過方法- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait更新UI。