The following guide shows how to open and read fomr a serial port using matlab:
And it is done like this:
s = serial('COM1');
fopen(s)
fprintf(s,'*IDN?')
idn = fscanf(s);
fclose(s)
I have a program that continually gets the serial output and plots it:
figure
s = serial('COM11');
fopen(s)
while(true)
if (strcmp(comsStatus, 'open') == 1)
tline(i) = str2num(fgetl(s));
i = i+1
plot(tline(1:i-1))
drawnow
end
end
fclose(s)
What i am looking to do is automatically break out of the while loop. But there does not seem to be an easy indication of when the serial has stopped coming through. the fgetl(s) part will wait until something actually comes through. Is there a way to time this out? Is there a better way to do this?
s.BytesAvailable
and only callfgetl
whens.BytesAvailable
is larger than 0? – H.Muster