Serial Communication Problem with Timeout

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

답변 (1개)

Walter Roberson
Walter Roberson 2013년 2월 13일

0 개 추천

You can check BytesAvailable of the serial object.

댓글 수: 1

Steven
Steven 2013년 2월 13일
This would work if I had the instrument control toolbox...

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

카테고리

도움말 센터File Exchange에서 External Language Interfaces에 대해 자세히 알아보기

질문:

2013년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by