Code for serial port behaves differently when run

12 views (last 30 days)
Hi
We have build a rather big app/program to control a USB Sound Card including a lot of analysing. The program is too big to share. The connection to the sound card happens over Serial port connection. We have now added a homebuild Arduino climate station which also connects over USB and serial port (another serial port than the sound card). I have made a class for the climate station wherin the serial port object is made and there is a function called getClimate() which asks the Arduino for the given temperature, humidy and pressure. This is read into MatLab with the fscanf function. The code works as intended until Matlab has communicated with the sound card. When running the getClimate function after interacting with the Sound Card, Matlab runs the getClimate code differently the first 3 times. Please see the getClimate code in the end of the message.
The first time running getClimate, three warnings appear in the command history window. One warning for each fscanf call, since nothing is returned from the arduino. I do not understand why three warnings are returned since the code sets warning('') before the first fscanf call and thereafter should "return" out of the function if a warning is received. Which is does not.
The second time running getClimate, one warning appear in the command history window. This indicates that the code is run correctly this time since it returns out of the function when a warning is received.
The thrids time running getClimate, the code and interaction with the Arudino works and the three fscanf calls work as intended.
Do you have any idea of what could be wrong and how to tackle this issue? I have no idea of how the same code can be run three times given three different code behavious and especially with the first where MatLab ignores the Warning in the code.
function getClimate(obj)
% get climate data. obj.uno is the serial connection.
if ~strcmp(obj.uno.status,'open')
fopen(obj.uno);
end
% clear input buffer
flushinput(obj.uno);
fprintf(obj.uno,"GetClimate\n");
pause(0.1);
warning('');
obj.temp = str2num(fscanf(obj.uno));
if ~isempty(lastwarn)
return
end
obj.RH = str2num(fscanf(obj.uno));
obj.pressure = str2num(fscanf(obj.uno));
end
  2 Comments
Morten Halvorsen
Morten Halvorsen on 2 Sep 2019
It is the timeout warning:
Warning: A timeout occurred before the Terminator was reached

Sign in to comment.

Answers (0)

Categories

Find more on Climate Science and Analysis in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!