Serial Communication Problem with Timeout
Show older comments
Hello,
I am trying to write a function for a piece of measurement equip I have. The problem I am having is that the measurement time is variable depending on my measurement. So if I ask the part for data while it still is measuring I get no response and my program times-out. When it is done measuring it outputs data. Is there a way to see if any data has been given before I try to read the data? As of now I am just waiting 30 seconds which is a bit of a waste of time.
function J = PR705f(P)
delete(instrfind)
timestamp = 0;
tic
s = serial(P,'BaudRate',9600,'DataBits',8,'StopBits',1);
%Open serial Port
fopen(s)
%start remote mode
fprintf(s,'PR705')
x = 1;
pause(2)
%Measure
fprintf(s,'M5\n\r')
pause(30)
%Record Results
fgets(s);
pause(.1)
fgets(s);
pause(.1)
tline(x,:) = fgets(s);
for x = 1:200;
tline(x,:) = fgets(s);
pause(.1)
end
pause(5)
fprintf(s,'D6\n\r')
pause(.1);
tline2(1,:) = fscanf(s);
pause(5)
fprintf(s,'Q\n\r')
fclose(s)
delete(instrfind)
timestamp = toc;
save_data('MySerialFile.txt',tline2)
save_data('MySerialFile.txt',timestamp)
clear x
clear tline
clear tline2
Answers (1)
Categories
Find more on External Language Interfaces in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!