资源描述:
《matlab串口程序》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、实用标准文案functionserial_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;%表
2、征是否正在进行数据显示,即是否正在执行函数dataDispisStopDisp=false;%表征是否按下了【停止显示】按钮isHexDisp=false;%表征是否勾选了【十六进制显示】isHexSend=false;%表征是否勾选了【十六进制发送】numRec=0;%接收字符计数numSend=0;%发送字符计数strRec='';%已接收的字符串%%将上述参数作为应用数据,存入窗口对象内setappdata(hObject,'hasData',hasData);setappdata(hObject,'strRec
3、',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(hObjec
4、t,handles);functionstart_serial_Callback(hObject,eventdata,handles)%【打开/关闭串口】按钮的回调函数%打开串口,并初始化相关参数%%若按下【打开串口】按钮,打开串口ifget(hObject,'value')%%获取串口的端口名com_n=sprintf('com%d',get(handles.com,'value'));%%获取波特率rates=[3006001200240048009600192003840043000560005760011520
5、0];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');%
6、%创建串口对象scom=serial(com_n);%%配置串口属性,指定其回调函数set(scom,'BaudRate',baud_rate,'Parity',jiaoyan,'DataBits',...data_bits,'StopBits',stop_bits,'BytesAvailableFcnCount',10,...'BytesAvailableFcnMode','byte','BytesAvailableFcn',{@bytes,handles},...'TimerPeriod',0.05,'timerf
7、cn',{@dataDisp,handles});%%将串口对象的句柄作为用户数据,存入窗口对象set(handles.figure1,'UserData',scom);%%尝试打开串口tryfopen(scom);%打开串口catch%若串口打开失败,提示“串口不可获得!”msgbox('串口不可获得!');set(hObject,'value',0);%弹起本按钮return;end%%打开串口后,允许串口发送数据,清空接收显示区,点亮串口状态指示灯,%%并更改本按钮文本为“关闭串口”set(handles.per
8、iod_send,'Enable','on');%启用【自动发送】按钮set(handles.manual_send,'Enable','on');%启用【手动发送】按钮set(handles.xianshi,'string','');%清空接收显示区set(handles.activex1,'value',1);%点亮串口状态指