matlab读取图像并用串口发送

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

functionvarargout=serial_communication2(varargin)gui_Singleton=1;gui_State=struct('gui_Name',mfilename,...'gui_Singleton',gui_Singleton,...'gui_OpeningFcn',@serial_communication2_OpeningFcn,...'gui_OutputFcn',@serial_communication2_OutputFcn,...'gui_LayoutFcn',[],...'gui_Callback',[]);ifnargin&&ischar(varargin{1})gui_State.gui_Callback=str2func(varargin{1});endifnargout[varargout{1:nargout}]=gui_mainfcn(gui_State,varargin{:});elsegui_mainfcn(gui_State,varargin{:});endfunctionserial_communication2_OpeningFcn(hObject,eventdata,handles,varargin)handles.output=hObject;warning('off');javaFrame=get(hObject,'JavaFrame');javaFrame.setFigureIcon(javax.swing.ImageIcon('icon.jpg'));%%初始化参数hasData=false;%表征串口是否接收到数据isShow=false;%表征是否正在进行数据显示,即是否正在执行函数dataDispisStopDisp=false;%表征是否按下了【停止显示】按钮isHexDisp=false;%表征是否勾选了【十六进制显示】isHexSend=false;%表征是否勾选了【十六进制发送】numRec=0;%接收字符计数numSend=0;%发送字符计数strRec='';%已接收的字符串%%将上述参数作为应用数据,存入窗口对象内setappdata(hObject,'hasData',hasData);setappdata(hObject,'strRec',strRec);setappdata(hObject,'numRec',numRec);setappdata(hObject,'numSend',numSend);setappdata(hObject,'isShow',isShow);setappdata(hObject,'isStopDisp',isStopDisp);setappdata(hObject,'isHexDisp',isHexDisp);setappdata(hObject,'isHexSend',isHexSend);guidata(hObject,handles);functionvarargout=serial_communication2_OutputFcn(hObject,eventdata,handles)varargout{1}=handles.output;functioncom_Callback(hObject,~,handles)functioncom_CreateFcn(hObject,eventdata,handles)ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunctionrate_Callback(hObject,eventdata,handles)functionrate_CreateFcn(hObject,eventdata,handles)ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunctionjiaoyan_Callback(hObject,eventdata,handles)functionjiaoyan_CreateFcn(hObject,eventdata,handles)ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunctiondata_bits_Callback(hObject,eventdata,handles)functiondata_bits_CreateFcn(hObject,eventdata,handles)ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunctionstop_bits_Callback(hObject,eventdata,handles)functionstop_bits_CreateFcn(hObject,eventdata,handles)ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunctionstart_serial_Callback(hObject,eventdata,handles)%【打开/关闭串口】按钮的回调函数%打开串口,并初始化相关参数%%若按下【打开串口】按钮,打开串口ifget(hObject,'value')%%获取串口的端口名com_n=sprintf('com%d',get(handles.com,'value'));%%获取波特率rates=[120024004800960019200384004300056000576001152009216001036800];baud_rate=rates(get(handles.rate,'value'));%%获取校验位设置switchget(handles.jiaoyan,'value')case1jiaoyan='none';case2jiaoyan='odd';case3jiaoyan='even';end%%获取数据位个数data_bits=5+get(handles.data_bits,'value');%%获取停止位个数stop_bits=get(handles.stop_bits,'value');%%创建串口对象scom=serial(com_n);%%配置串口属性,指定其回调函数set(scom,'BaudRate',baud_rate,'Parity',jiaoyan,'DataBits',...data_bits,'StopBits',stop_bits,'InputBufferSize',1638400,'OutputBufferSize',1638400,'BytesAvailableFcnCount',10,...'BytesAvailableFcnMode','byte','BytesAvailableFcn',{@bytes,handles},...'TimerPeriod',0.05,'timerfcn',{@dataDisp,handles});%%将串口对象的句柄作为用户数据,存入窗口对象set(handles.figure1,'UserData',scom);%%尝试打开串口tryfopen(scom);%打开串口catch%若串口打开失败,提示“串口不可获得!”msgbox('串口不可获得!');set(hObject,'value',0);%弹起本按钮return;end%%打开串口后,允许串口发送数据,清空接收显示区,点亮串口状态指示灯,%%并更改本按钮文本为“关闭串口”set(handles.period_send,'Enable','on');%启用【自动发送】按钮set(handles.manual_send,'Enable','on');%启用【手动发送】按钮set(handles.xianshi,'string','');%清空接收显示区set(handles.activex1,'value',1);%点亮串口状态指示灯set(hObject,'String','关闭串口');%设置本按钮文本为“关闭串口”else%若关闭串口%%停止并删除定时器t=timerfind;if~isempty(t)stop(t);delete(t);end%%停止并删除串口对象scoms=instrfind;stopasync(scoms);fclose(scoms);delete(scoms);%%禁用【自动发送】和【手动发送】按钮,熄灭串口状态指示灯set(handles.period_send,'Enable','off','value',0);%禁用【自动发送】按钮set(handles.manual_send,'Enable','off');%禁用【手动发送】按钮set(handles.activex1,'value',0);%熄灭串口状态指示灯endfunctiondataDisp(obj,event,handles)globalvalue%串口的TimerFcn回调函数%串口数据显示%%获取参数hasData=getappdata(handles.figure1,'hasData');%串口是否收到数据strRec=getappdata(handles.figure1,'strRec');%串口数据的字符串形式,定时显示该数据numRec=getappdata(handles.figure1,'numRec');%串口接收到的数据个数%%若串口没有接收到数据,先尝试接收串口数据if~hasDatabytes(obj,event,handles);end%%若串口有数据,显示串口数据ifhasData%%给数据显示模块加互斥锁%%在执行显示数据模块时,不接收串口数据,即不执行BytesAvailableFcn回调函数setappdata(handles.figure1,'isShow',true);set(handles.xianshi,'string',strRec);%%更新接收计数set(handles.rec,'string',numRec);%%更新hasData标志,表明串口数据已经显示setappdata(handles.figure1,'hasData',false);%%给数据显示模块解锁setappdata(handles.figure1,'isShow',false);endfunctionbytes(obj,~,handles)%串口的BytesAvailableFcn回调函数%串口接收数据%%获取参数strRec=getappdata(handles.figure1,'strRec');%获取串口要显示的数据numRec=getappdata(handles.figure1,'numRec');%获取串口已接收数据的个数isStopDisp=getappdata(handles.figure1,'isSt

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功