Serial port monitor in App Designer
25 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre STMicroelectronics Discovery Boards en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!