📄 seriescomm.m
字号:
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu5.
function popupmenu5_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu5 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu5
stopBit = get(hObject,'string');
handles.stopBit = str2num(stopBit{get(hObject,'Value')});
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function popupmenu5_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%打开串口按钮的回调程序
%首先检验是打开串口操作还是关闭操作
%打开操作
if(strcmp(get(hObject,'string') , 'Open Serial Port'))
%建立一个serial object,并设置其参数(接收的终止符号为\n)
try
handles.serial = serial(handles.com,'BaudRate',handles.baud_rate,'DataBits',handles.dataBits,...
'Parity',handles.verifiedBits,'StopBits',handles.stopBit,'FlowControl',...
handles.flowControl,'terminator',char(13));
set(handles.serial,'OutputBufferSize',512000);
set(handles.serial,'InputBufferSize',512000);
handles.serial.BytesAvailableFcnMode = 'terminator';
%当接受到数据后如果碰到结束符\n就读入,调用receiveData函数
handles.serial.BytesAvailableFcn={'receiveData',handles};
guidata(hObject,handles);
%打开串口
fopen(handles.serial);
set(hObject,'String','Close Serial Port','foreGroundColor',[67/255,64/255,119/255]);
str = ['Serial Port ', handles.com , ' opened sucessfully!'];
set(handles.text3,'String',str,'fontweight','bold');
set(handles.pushbutton4,'enable','on');
set(handles.pushbutton5,'enable','on');
set(handles.pushbutton6,'enable','on');
catch
%进行出错处理
errmsg = lasterr;
errordlg([errmsg,char(10),' Can''t Open Serial Port : ',handles.com, char(10),' Please choose another Port!'],'Serial Port Open Error');
rethrow(lasterror);
end
%关闭串口操作
else
fclose(handles.serial);
set(hObject,'String','Open Serial Port','foreGroundColor',[1,0,0.5]);
str = ['Serial Port ', handles.com , ' closed sucessfully!'];
set(handles.text3,'String',str,'fontweight','bold');
set(handles.pushbutton4,'enable','off');
set(handles.pushbutton5,'enable','off');
set(handles.pushbutton6,'enable','off');
end
% --- Executes on selection change in popupmenu6.
function popupmenu6_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu6 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu6
flowControl = get(hObject,'string');
handles.flowControl = flowControl{get(hObject,'Value')};
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function popupmenu6_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%发送数据按钮回调程序
str = get(handles.edit1,'string');
%检测端口状态
if(strcmp(handles.serial.status,'closed'))
str1 = ['Serial Port ', handles.com , ' is closed ,Please open it!'];
set(handles.text3,'String',str1,'fontweight','bold');
else
%打开时进行串口写出
if(handles.displayT == 0)
fprintf(handles.serial,'%s',str);
str2 = ['Data has sent in ASCI format sucessfully!'];
set(handles.text3,'String',str2,'fontweight','bold');
else
fprintf(handles.serial,'%x',str);
str3 = ['Data has sent in HEX format sucessfully!'];
set(handles.text3,'String',str3,'fontweight','bold');
end
end
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
handles.displayT = get(hObject,'Value');
guidata(hObject,handles);
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox2
handles.displayR = get(hObject,'Value');
guidata(hObject,handles);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
%推出界面时 检查是否关闭串口
button = questdlg('Do you really want to quit? ','Quit?','Yes','No','No');
if(strcmp(button, 'Yes'))
if(strcmp(get(handles.pushbutton1,'string'),'Close Serial Port'))
fclose(handles.serial);
end
delete(hObject);
else
return;
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%发送文件前选择文件
[filename, pathname] = uigetfile( ...
{'*.txt;*.c;*.cpp;*.m','TEXT Files (*.txt,*.c,*.cpp,*.m)';
'*.txt', 'TEXT-files (*.txt)'; ...
'*.c','C-Files (*.c)'; ...
'*.cpp','C++-Files (*.cpp)'; ...
'*.m','M-Files (*.m)'; ...
'*.*', 'All Files (*.*)'}, ...
'Pick a file');
if isequal([filename,pathname],[0,0])
return
% Otherwise construct the fullfilename and Check and load the file
else
handles.filePath = fullfile(pathname,filename);
end
guidata(hObject,handles);
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%发送文件 文件不能太大
fid = fopen(handles.filePath, 'r');
tempstr = ['File ',handles.filePath, ' has opened sucessfully!'];
set(handles.text4,'string',tempstr,'fontweight','bold');
str = fread(fid,'*char');
str = str';
set(handles.edit1,'string',str);
if(strcmp(handles.serial.status,'closed'))
str1 = ['Serial Port ', handles.com , ' is closed ,Please open it!'];
set(handles.text3,'String',str1,'fontweight','bold');
else
if(handles.displayT == 0)
fprintf(handles.serial,'%s',str);
str2 = ['Data has sent in ASCI format sucessfully!'];
set(handles.text3,'String',str2,'fontweight','bold');
else
fprintf(handles.serial,'%x',str);
str3 = ['Data has sent in HEX format sucessfully!'];
set(handles.text3,'String',str3,'fontweight','bold');
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -