MATLAB环境下地串口通信软件.doc

上传人:李司机 文档编号:1087700 上传时间:2022-06-20 格式:DOC 页数:14 大小:219KB
返回 下载 相关 举报
MATLAB环境下地串口通信软件.doc_第1页
第1页 / 共14页
MATLAB环境下地串口通信软件.doc_第2页
第2页 / 共14页
MATLAB环境下地串口通信软件.doc_第3页
第3页 / 共14页
MATLAB环境下地串口通信软件.doc_第4页
第4页 / 共14页
MATLAB环境下地串口通信软件.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《MATLAB环境下地串口通信软件.doc》由会员分享,可在线阅读,更多相关《MATLAB环境下地串口通信软件.doc(14页珍藏版)》请在三一办公上搜索。

1、word某某电子科技大学第二十一届“星火杯大学生课外学术科技作品竞赛科技发明制作类说明书作品名称:MATLAB环境下的串口通信软件作品类别:计算机软件开发和设计类作品作品编号:_作品功能简介与使用说明书MATLAB环境下的串口通信软件,图形化操作界面。 用于对计算机的串口进展设置,数据收发,数据处理。可实现串口开关,串口属性设置,串口状态检测。 可通过本软件对下位机发送指令。 根据回传的数据,绘制实时数据曲线,可切换显示模式,曲线图,回传数据可保存。可根据回传数据的变化情况发送相应的指令。演示视频中为此软件作为温度监测系统上位机软件的应用。此软件可作为通用的串口通信调试器。编译后生成EXE文件

2、,可脱离MATLAB环境运行。程序主界面:使用说明:光盘中有功能演示视频串口选择区如串口成功打开,按钮变为绿色,并弹出提示框串口设置区波特率设置 等待时间与奇偶校验设置 可选择或编辑波特率 默认为无奇偶校验串口状态检测按钮 点击显示串口名, 串口开关状态, 波特率 ,记录状态数据发送区点击Send Now按钮将编辑框中的数据通过串口发往下位机数据接收区文本框中为从计算机从串口接收到的数据。点击Clear按钮清空接收区。数据可点击工具栏中的相应按钮保存。温度控制局部 曲线显示模式选择 设置报警温度 局部显示按钮与整体显示按钮温度超过X围将提醒并发送相应指令代码到下位机数据曲线图曲线图显示模式可选

3、择,图片,数据可另存。点击Clear Figure按钮清空曲线图点击工具栏中的保存按钮,选择保存路径与存储格式。局部程序源代码:function varargout = MyfirstGui(varargin)%串口通信调试器与温度监控系统上位机软件% 2009.811%发送按钮回调函数function Send_Callback(hObject, eventdata, handles)global s;SendingSrting=get(handles.SendString,String);fprintf(s,%c,SendingSrting);%去除按钮回调函数function Clear

4、_Callback(hObject, eventdata, handles)set(handles.Received,String,Received:);global ReceivedFromMcu;ReceivedFromMcu=Reveived:;%串口设置局部%串口开关设置% - Executes on button press in 1.function 1_Callback(hObject, eventdata, handles)% Hint: get(hObject,Value) returns toggle state of 1global s;button_state = ge

5、t(hObject,Value);if button_state = get(hObject,Max) set(handles 1,BackgroundColor,default); set(handles 2,BackgroundColor,default); set(handles 3,BackgroundColor,default); set(handles 4,BackgroundColor,default); delete(s); s=serial(1); s.BytesAvailableFMode=byte; s.BytesAvailableFCount=1; s.BytesAva

6、ilableF=ReceiveButton; fopen(s); set(hObject,BackgroundColor,green); message=1 is Open; msgbox(message);elseif button_state = get(hObject,Min) fclose(s); set(hObject,BackgroundColor,default);end%编辑波特率function EditBaudRate_Callback(hObject, eventdata, handles)% Hints: get(hObject,String) returns cont

7、ents of EditBaudRate as text% str2double(get(hObject,String) returns contents of EditBaudRate% as a doubleglobal s;user_entry = str2double(get(hObject,string);if isnan(user_entry) errordlg(You must enter a numeric value,Bad Input,modal) uicontrol(hObject) returnends.BaudRate=user_entry;message=Baudr

8、ate= get(hObject,string);msgbox(message);% - Executes during object creation, after setting all properties.function EditBaudRate_CreateF(hObject, eventdata, handles)% Hint: edit controls usually have a white background on Windows.% See ISPC and PUTER.if ispc & isequal(get(hObject,BackgroundColor), g

9、et(0,defaultUicontrolBackgroundColor) set(hObject,BackgroundColor,white);end% 选择波特率function SelectBaundRate_Callback(hObject, eventdata, handles)% Hints: contents = get(hObject,String) returns SelectBaundRate contents as cell array% contentsget(hObject,Value) returns selected item from% SelectBaundR

10、ateglobal s;val = get(hObject,Value);switch val case 1 s.BaudRate=9600; set(handles.EditBaudRate,string, ); case 2 s.BaudRate=4800; set(handles.EditBaudRate,string, ); case 3 s.BaudRate=14400; set(handles.EditBaudRate,string, ); case 4 s.BaudRate=28800; set(handles.EditBaudRate,string, );end% 选择等待时间

11、function TimeOut_Callback(hObject, eventdata, handles)% Hints: contents = get(hObject,String) returns TimeOut contents as cell array% contentsget(hObject,Value) returns selected item from TimeOutglobal s;val = get(hObject,Value);switch val case 1 s.TimeOut=10; case 2 s.TimeOut=5; case 3 s.TimeOut=2;

12、 case 4 s.TimeOut=1;end% - Executes during object creation, after setting all properties.function TimeOut_CreateF(hObject, eventdata, handles)% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and PUTER.if ispc & isequal(get(hObject,BackgroundColor), get(0,defaultUicont

13、rolBackgroundColor) set(hObject,BackgroundColor,white);end%奇偶校验设置function Parity_Callback(hObject, eventdata, handles)% Hints: contents = get(hObject,String) returns Parity contents as cell array% contentsget(hObject,Value) returns selected item from Parityglobal s;val = get(hObject,Value);switch va

14、l case 1 s.Parity=none; case 2 s.Parity=even; case 3 s.Parity=odd;end%检查串口属性按钮function CheckSerial_Callback(hObject, eventdata, handles)global s;item1=s.Name;set(handles.Check1,String,item1);item2=s.Status;set(handles.Check2,String,item2);item3=s.Baudrate;set(handles.Check3,String,item3);item4=s.Rec

15、ordStatus;set(handles.Check4,String,item4);%接收字符回调函数function ReceiveButton(hObject, eventdata, handles)global s;global ReceivedFromMcu;global Myhandles;global x;global y;global a;persistent Char_Buffer;persistent count;%initialize the persistent variableif isempty(Char_Buffer) Char_Buffer=;endif ise

16、mpty(count) count=0;endReceived_Char=fscanf(s,%c,1);%ReceivedFromMcu will be displayed in the received zoneReceivedFromMcu=ReceivedFromMcu Received_Char;%Char_Buffer will be converted to numbersChar_Buffer=Char_Buffer Received_Char;%When the port has received a ,it means the begining of another%conv

17、erting action.if Received_Char= ; Char_Buffer=;end%Check the Char_Buffer,if available,it will be sent to ySizeof_Char_Buffer=size(Char_Buffer);if (Sizeof_Char_Buffer(2)=4)&(Char_Buffer(3)=.); %Give numbers to the figure y=y sscanf(Char_Buffer,%g); x=x count; count=count+1;end%Get the last value of y

18、a,b=size(y);if(b0) a=y(1,end);end%Plot the Temperature Line plot(Myhandles.axes1,x,y,m); plot(Myhandles.axes1,x,y,b);else plot(Myhandles.axes1,x,y,r);endset(Myhandles.axes1,YGrid,on);set(Myhandles.axes1,YMinorGrid,on);%Set axes according to DisplayModeif Myhandles.DisplayMode=0 set(Myhandles.axes1,Y

19、Lim,a-1.5 a+1.5);else set(Myhandles.axes1,YLim,a-10 a+10);end%Show the current temperatureif Received_Char= ; temp=Current Temperature:,Char_Buffer; set(Myhandles.Current_Temperature,String,temp);end%Warning Functionslope=0;if b50 slope=y(b)-y(b-49);end set(Myhandles.Warning,Visible,on); set(Myhandl

20、es.Warning,String,WARNING:Temperature is going up quickly!);elseif slope500 ReceivedFromMcu=Rcecived:;endset(Myhandles.Received,String,ReceivedFromMcu);%去除图像按钮function ClearFigure_Callback(hObject, eventdata, handles)global x;global y;x=x(end);y=y(end);plot(handles.axes1,x,y,r.);%曲线显示模式选择% - Execute

21、s on button press in Part.function Part_Callback(hObject, eventdata, handles)global Myhandles;global a;state = get(hObject,Value);if state = get(hObject,Max) %Change mode Myhandles.DisplayMode=0;end%Set axes according to DisplayModeif Myhandles.DisplayMode=0 set(Myhandles.axes1,YLim,a-1.5 a+1.5);els

22、e set(Myhandles.axes1,YLim,a-10 a+10);end% - Executes on button press in Whole.function Whole_Callback(hObject, eventdata, handles)global Myhandles;global a;state = get(hObject,Value);if state = get(hObject,Max) %Change mode Myhandles.DisplayMode=1;end%Set axes according to DisplayModeif Myhandles.D

23、isplayMode=0 set(Myhandles.axes1,YLim,a-1.5 a+1.5);else set(Myhandles.axes1,YLim,a-10 a+10);end%设置报警温度function Low_Callback(hObject, eventdata, handles)% Hints: get(hObject,String) returns contents of Low as text% str2double(get(hObject,String) returns contents of Low as a doubleglobal Myhandles;use

24、r_entry = str2double(get(hObject,string); errordlg(You must enter a rightful numeric value,Bad Input,modal) uicontrol(hObject) returnendMyhandles.Low=user_entry;message=Warning Low Temperature= get(hObject,string);msgbox(message);function High_Callback(hObject, eventdata, handles)% Hints: get(hObjec

25、t,String) returns contents of High as text% str2double(get(hObject,String) returns contents of High as a doubleglobal Myhandles;user_entry = str2double(get(hObject,string); errordlg(You must enter a rightful numeric value,Bad Input,modal) uicontrol(hObject) returnendMyhandles.High=user_entry;message

26、=Warning High Temperature= get(hObject,string);msgbox(message);%工具栏局部% -保存图片-function Save_ClickedCallback(hObject, eventdata, handles)file,path = uiputfile ( *.jpg;*.png;*.bmp;*.tif,Save Figure);if isequal(file,0) | isequal(path,0) return;endf=fullfile(path,file);h=getframe(handles.axes1);h=frame2i

27、m(h);imwrite(h, f);% -新任务 -function New_ClickedCallback(hObject, eventdata, handles)global s;Clear_Callback(hObject, eventdata, handles);ClearFigure_Callback(hObject, eventdata, handles);delete(s);set(handles 1,Value,0);set(handles 2,Value,0);set(handles 3,Value,0);set(handles 4,Value,0);set(handles

28、 1,Value,0);set(handles 1,BackgroundColor,default);set(handles 2,BackgroundColor,default);set(handles 3,BackgroundColor,default);set(handles 4,BackgroundColor,default);set(handles.Warning,Visible,off);set(handles.Current_Temperature,string,Current Temperature:);% -打开-function Open_ClickedCallback(hO

29、bject, eventdata, handles)file,path = uigetfile ( *.jpg;*.png;*.bmp;*.tif,Read Figure);if isequal(file,0) | isequal(path,0) return;endf=fullfile(path,file);I = imread( f);imshow(I);% -保存数据 -function SaveReceivedString_ClickedCallback(hObject, eventdata, handles)global ReceivedFromMcu;file,path = uip

30、utfile ( *.mat,Save Figure);if isequal(file,0) | isequal(path,0) return;endf=fullfile(path,file);save (f,ReceivedFromMcu);补充说明:演示视频中与此软件通信的是STC单片机系统。单片机系统中有温度传感器DS18b20,计算机串口电平转换芯片MAX232等外部电路。单片机系统通过一条串口线或USB转串口线与PC连接。单片机主要代码如下:main()unsigned int i=0;unsigned char temp_serial5=0,0,.,0, ; init_serial() ; /初始化串口while(1)i=ReadTemperature(); /读取当前温度temp_serial0=mun_char_tablei/100;temp_serial1=mun_char_tablei%100/10;temp_serial3=mun_char_tablei%10; for(i=0;i5;i+) /向串口发送数据 SBUF =temp_seriali;while(TI=0);TI=0; delay_1ms(50);14 / 14

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号