What is the correct way to close a serialport connection in App Designer?

조회 수: 20 (최근 30일)
Nolan King
Nolan King 2020년 3월 9일
답변: Dominik Mair 2020년 10월 27일
As per the the serialport documentation, the way to create and delete a serial connection should be
s = serialport(device,baudrate);
clear s;
However, in app designer, I want multiple functions to be able to read/write to the port, so I have made s a property of the app. You can't clear an app property thought. So what is the correct way free app.s so it can be reconnected to the same device later?
Failing to clear s gives the usual error of "Unable to connect". Setting app.s to some other value, and then creating a new serialport object doesn't close the old connection (i suppose it overwrites the location of the serialport object).

답변 (3개)

Jingyi Zhou
Jingyi Zhou 2020년 6월 17일
I find a solution for this question.
First, I add a public property for this app. It is called Serial_Port, used to save the serialport objet.
properties (Access = public)
Serial_Port % Description
end
then, when I want to open a serial
app.Serial_Port = serialport(app.DropDown_SerialPort.Value,str2double(app.DropDown_Baudrate.Value));
when I want to close this serialport,
app.Serial_Port = [];
And because this property is public, this open and close command don't have to be in the same function.
Hope can help you~

Dominik Mair
Dominik Mair 2020년 10월 27일
Hi!
I used a "dirty" workaround with the old guis. Maybe something similar works with apps:
In an old project i used following containing my serialport object:
handles.ACTIVECOMPORT
If i want to reset it i used:
temp=handles.ACTIVECOMPORT;
handles.ACTIVECOMPORT=[];
delete(temp)

Harsha Priya Daggubati
Harsha Priya Daggubati 2020년 3월 27일
편집: Harsha Priya Daggubati 2020년 3월 27일
Hi,
I guess you can set app.s to an empty value, after all the intended work is done.
Link to a similar question:
Hope this helps!
  댓글 수: 1
Nolan King
Nolan King 2020년 3월 30일
Unfortunatly that doesn't close the serial connection, which is the point of clearing the variable.

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by