Having trouble connecting serial port inside GUI

49 Ansichten (letzte 30 Tage)
Kit Leeming
Kit Leeming am 27 Mai 2022
Beantwortet: Pratyush Swain am 1 Feb. 2024
So some background: I'm making a small app that can read pump performance from a flow and pressure sensor thats connected to a UNO board, and I'm wanting to have it read live data through the serial connection. Problem is that it doesn’t want to open the serial port connection properly inside of my application (made using the app designer). This is the error message that I get:
Error using internal.Serialport
Unable to connect to the serialport device at port 'COM3'. Verify that a device is connected to the port, the port is
not in use, and all serialport input arguments and parameter values are supported by the device.
See related documentation for troubleshooting steps.
Error in serialport (line 116)
obj = internal.Serialport(varargin{:});
I can open and read the serial port just fine outside of my GUI code, but can’t seem to get it working when its running inside the app.
Any help or ideas would be greatly appreciated.
Here is the code I have so far:
s=serialport("COM3",9600);
a=0;
F=1;
setappdata(app.Image_2,'stopvalue',a);
while a==0
Data(F,:)=readline(s);
if numel(Data(F,:))<3
continue
elseif numel(Data(F,:))>3
%error('Input data has too many variables')
app.Label_4.Text='Input data has too many variables';
break
end
V=Data(F,1);
C=Data(F,2);
DT=Data(F,3)/1000;
P=((V/5)/0.0018)-22.22;
FR=(C*0.00303)/DT;
T=T+DT;
H = (P)/(9.81);
HP = (FR*P)/1000;
drawnow limitrate
addpoints(PvsT,T,P);
addpoints(FRvsT,T,FR);
addpoints(HvsFR,FR,H);
addpoints(HPvsFR,FR,HP);
a=getappdata(app.Image_2,'stopvalue');
F=F+1;
setappdata(app.DataExport,'DataMatrix',Data);
end
Kit Leeming
  2 Kommentare
Kit Leeming
Kit Leeming am 27 Mai 2022
Also before anyone gets all up me for using a while loop instead of a counter, I'm not all that great yet and don't want to go through the hassle for something that I want to try and keep simple and finish quickly
Abdulkadir Arslan
Abdulkadir Arslan am 31 Mai 2022
I am having a similar problem. Could you find any solution?
This is my question: internal.Serialport error

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Pratyush Swain
Pratyush Swain am 1 Feb. 2024
Hi Kit,
I understand you are unable to connect to your serial port device from inside the application.Here are some of the steps/workarounds to troubleshoot and resolve the issue:
  1. Check port availability: Ensure that the serial port "COM3" is not being used by another application, including other instances of MATLAB. Only one application can use a serial port at a time.
  2. Run MATLAB as Administrator: Sometimes, permission issues can prevent MATLAB from accessing the serial port. Try running MATLAB as an administrator to see if that resolves the issue.
  3. Use "instrfind" to check for open serial objects: Use "instrfind" to check for any open serial port objects that might be blocking the connection. If there are any, close and delete them before trying to connect again.
  4. Implement error handling: Add error handling in your code to catch any issues when attempting to open the serial port and provide a user-friendly message..
You can make the following modifications to your implementation:
% Clear any existing serial connections
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
% Attempt to create the serialport object with error handling
try
s = serialport("COM3", 9600);
catch e
uialert(app.UIFigure, e.message, 'Serial Port Error');
return; % Exit the function if the serial port cannot be opened
end
% Initialize variables for the loop
a = 0;
F = 1;
setappdata(app.Image_2, 'stopvalue', a);
% Main loop implementation for reading data
% ------
% Close the serial port when done
clear s
Please refer to a similar query on connecting serial port from app designer for more information: https://www.mathworks.com/matlabcentral/answers/1730055-connecting-arduino-to-app-designer-with-drop-down-and-button
For more information on "instrfind" function,please refer to https://www.mathworks.com/help/matlab/ref/instrfind.html
Hope this helps.

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by