MATLAB环境下地串口通信软件.doc
word某某电子科技大学第二十一届“星火杯大学生课外学术科技作品竞赛科技发明制作类说明书作品名称:MATLAB环境下的串口通信软件作品类别:计算机软件开发和设计类作品作品编号:_作品功能简介与使用说明书MATLAB环境下的串口通信软件,图形化操作界面。 用于对计算机的串口进展设置,数据收发,数据处理。可实现串口开关,串口属性设置,串口状态检测。 可通过本软件对下位机发送指令。 根据回传的数据,绘制实时数据曲线,可切换显示模式,曲线图,回传数据可保存。可根据回传数据的变化情况发送相应的指令。演示视频中为此软件作为温度监测系统上位机软件的应用。此软件可作为通用的串口通信调试器。编译后生成EXE文件,可脱离MATLAB环境运行。程序主界面:使用说明:光盘中有功能演示视频串口选择区如串口成功打开,按钮变为绿色,并弹出提示框串口设置区波特率设置 等待时间与奇偶校验设置 可选择或编辑波特率 默认为无奇偶校验串口状态检测按钮 点击显示串口名, 串口开关状态, 波特率 ,记录状态数据发送区点击Send Now按钮将编辑框中的数据通过串口发往下位机数据接收区文本框中为从计算机从串口接收到的数据。点击Clear按钮清空接收区。数据可点击工具栏中的相应按钮保存。温度控制局部 曲线显示模式选择 设置报警温度 局部显示按钮与整体显示按钮温度超过X围将提醒并发送相应指令代码到下位机数据曲线图曲线图显示模式可选择,图片,数据可另存。点击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_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 = get(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.BytesAvailableF=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 contents 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='Baudrate=' 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'), get(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% SelectBaundRateglobal 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% 选择等待时间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; 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,'defaultUicontrolBackgroundColor') 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 val 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.RecordStatus;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 isempty(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%converting 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 ya,b=size(y);if(b>0) 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,'YLim',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 b>50 slope=y(b)-y(b-49);end set(Myhandles.Warning,'Visible','on'); set(Myhandles.Warning,'String','WARNING:Temperature is going up quickly!');elseif slope<0 set(Myhandles.Warning,'Visible','on'); set(Myhandles.Warning,'String','WARNING:Temperature is decling!');else set(Myhandles.Warning,'Visible','off');end%Clear the received zone when it's fullSizeOfReceived=size(ReceivedFromMcu);if SizeOfReceived(2)>500 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.');%曲线显示模式选择% - Executes 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);else 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.DisplayMode=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;user_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(hObject,'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='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=frame2im(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 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(hObject, 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 = uiputfile ( '*.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;i<5;i+) /向串口发送数据 SBUF =temp_seriali;while(TI=0);TI=0; delay_1ms(50);14 / 14