[英]Java Code to get output of running program
我想從Java程序運行以下命令:
socat -x -u /dev/ttyFTDI0,raw,echo=0,crnl /dev/ttyFTDI1,raw,echo=0,crnl
該程序在輸出十六進制字符串時應運行不確定的時間,如下所示:
b4 03 03 92 00 01 3f c6 b4 03 10 03 00 01 6a af
我當前的測試代碼是:
public void testOutput() {
try {
List<String> command = new ArrayList<String>();
command.add("socat");
command.add("-x");
command.add("-u");
command.add("/dev/ttyFTDI0,raw,echo=0,crnl");
command.add("/dev/ttyFTDI1,raw,echo=0,crnl");
ProcessBuilder proBui = new ProcessBuilder(command);
Process process = proBui.start();
//process.waitFor();
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
String temp = r.readLine();
if (temp != null && !temp.isEmpty()) {
//do something with data
System.out.print(temp);
}
Thread.sleep(1);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
盡管此代碼適用於在給定時間后完成的命令,但像給定命令或watch ls
這樣的命令將不起作用。
根據socat
文檔 , -x
選項
將傳輸的數據不僅寫入其目標流,還寫入stderr。
您正在閱讀process.getInputStream()
連接到本機進程標准輸出-讓標准錯誤,你應該讀process.getErrorStream()
來代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.