Reading from comand prompt executables

조회 수: 3 (최근 30일)
Evan
Evan 2011년 4월 5일
I have two versions of an executable that run in windows cmd interface. Each executable connects to a different device and continuously reports information back from the device. The output in the cmd window would be the line ""X = 5.3 Y = 5.3 Z = 3.4 ButtonState = 0x0"" And every few milliseconds, it will write a new line with the new value from the device. I want to be able to read into MATLAB the last line from the cmd window from the two separate cmd windows so that I can convert them and display them in a MATLAB gui.
Is there a way to read from two windows cmd (without saving the output) in close to real time?
All help is appreciated! Thank you, Evan

답변 (1개)

Richard Alcock
Richard Alcock 2011년 4월 5일
I think you will have to look at using some Java classes to do this. The ProcessBuilder and Process classes will enable you to do this. Here's an example that calls the Windows "ping.exe" utility.
pingCmd = {'ping.exe', '-n', '10', 'www.google.com'};
processBuilder = java.lang.ProcessBuilder(pingCmd);
pingProcess = processBuilder.start();
% Set up a reader to read the output from the
% ping process
reader = ...
java.io.BufferedReader(...
java.io.InputStreamReader(...
pingProcess.getInputStream() ...
) ...
);
% Loop until there is some output
nextLine = char( reader.readLine );
while isempty(nextLine)
nextLine = char( reader.readLine );
end
% Then loop until there is no more output
while ~isempty(nextLine);
fprintf('PING output: %s\n', nextLine);
nextLine = char( reader.readLine );
end
% Get the exit value of the process
exitValue = pingProcess.exitValue
  댓글 수: 1
Bill Comisky
Bill Comisky 2013년 1월 16일
The read loop works a little better if you check for null return from readLine which is an empty double matrix in matlab. Otherwise it returns a java string.. this way you don't quit too early if your output has empty lines in it:
while true
line = input.readLine;
if isnumeric(line) && isempty(line)
% java null is empty double in matlab
break;
end
% do stuff with char(line) here
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by