先描述下问题现象:
正常打开关闭一次串口后,再次打开串口提示:
打开失败: Cannot connect to the COM31port. Possible reasons are another application is connected to the port or the port does not exist. 出错 MSerialV1>pbOpenAndClose_Callback(line 122) fopen(s);
确认该串口没有在其它程序中使用!
以下是程序代码
set(handles.pmComPort,'String',seriallist);
indexCom = get(handles.pmComPort,'Value');
strCom = get(handles.pmComPort,'String');
if indexCom > 1
comPort = strCom(indexCom);
else
comPort = strCom;
end
s = serial(comPort);
if handles.pbOpenAndClose.String == "打开"
handles.pbOpenAndClose.String = "关闭";
handles.pbOpenAndClose.BackgroundColor = 'g';
fopen(s);
else
handles.pbOpenAndClose.String = "打开";
handles.pbOpenAndClose.BackgroundColor = 'r';
fclose(s);
delete(s);
clear s;
end
|