Reading from comand prompt executables
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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
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 Exchange 中查找有关 Data Import and Analysis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!