Serial port monitor in App Designer

25 views (last 30 days)
Gareth Jenkins
Gareth Jenkins on 16 Jan 2018
Commented: Gareth Jenkins on 27 Feb 2020
Hi
I'm a Matlab newbie using App Designer to make a nice GUI control for an STM32 microcontroller. To do this, I open a serial port connection and issue ascii commands (which works fine).
However, I'm struggling to read back the response of such commands. E.g. I send a command to start a motor connected to the STM and then read back confirmation it is running and then monitor when it has finished.
I can do this via a standard terminal connection (I'm using TeraTerm) and confirm commands are received and I get appropriate text responses back.
However, I can't get Matlab / App designer to achieve the same effect. Below is some cut down code showing some of what I have tried:
properties (Access = public)
obj1 % Description
end
methods (Access = private)
% Value changed function: ConnectSTMSwitch
function ConnectSTMSwitchValueChanged(app, event)
value = app.ConnectSTMSwitch.Value;
ConState = string(app.ConnectSTMSwitch.Value);
if ConState == 'Connect'
delete(instrfind('Port','COM14')); % delete any existing connection first
app.obj1 = serial('COM14','BaudRate',115200,'FlowControl','none','ByteOrder','littleEndian','Timeout',20,'Terminator',{'CR', 'CR'});
fopen(app.obj1); % open serial port connection
elseif ConState == 'Disconnect'
delete(instrfind('Port','COM14'));
end
end
% Value changed function: Enter_commands
function Enter_commandsValueChanged(app, event)
value = app.Enter_commands.Value;
command = string(app.Enter_commands.Value);
fprintf(app.obj1,command); % send command to STM32
pause(1); % probably unessary delay
app.STM_Output.Value = fscanf(app.obj1); % read output and display in textbox
pause(1); % probably unnecessary delay
app.STM_Output.Value = fscanf(app.obj1); % read 2nd ouput line (if no line, will reach Timeout limit)
pause(1); % probably unnecessary delay
app.STM_Output.Value = fscanf(app.obj1); % read 3rd ouput line (if no line, will reach Timeout limit)
pause(1); % probably unnecessary delay
app.STM_Output.Value = fscanf(app.obj1); % read 4th ouput line (if no line, will reach Timeout limit)
pause(1); % probably unnecessary delay
end
end
I use fscanf to read the port but it only ever reads 1 line. I'm guessing it reaches the terminator and stops. To read multiple lines I can simply issue another fscanf command but I generally don't know how many lines will be available for reading. If I issue too many reads, it hangs until the timeout is reached. In addition, what I read all gets out of step with the command being issued. I often see the result of the previous commands coming back a few commands later.
I assume I am not reading all the input buffer due to the terminator so I tried using Bytes Available in a while loop to keep reading until nothing is left in the input buffer. However, this didn't work. I could see Bytes Available change in value but it would never get back to zero and my loop got stuck.
I'm sure there is a better way to do this but I can't seem to find it. Any help would be much appreciated!
Thanks
  2 Comments
michael woodcock
michael woodcock on 17 Feb 2020
Did you ever find an aswer? related to what I'm doing now
Gareth Jenkins
Gareth Jenkins on 27 Feb 2020
Hi
I got the while loop to work in the end but it's very flaky.
I used something like:
fprintf(app.obj1,command);
while app.obj1.BytesAvailable > 0
ReadData = fscanf(app.obj1);
pause(0.01);
end
Adding a short pause seems to help in some cases.
Overall it is not that reliable and I still frequently get timeout errors. For simple cases it is OK but if you are reading lots of data and trying to prcoess it at the same time it gets very clunky.
My experience of getting MatLab working well with serial port hardware has not be great.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!