Reading from comand prompt executables

5 ビュー (過去 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 Center および File ExchangeData Import and Analysis についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by