⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seril2.m

📁 matlab通过串口采集心电的GUI程序
💻 M
📖 第 1 页 / 共 2 页
字号:
global str_stopbit;
s_port.StopBit=str2num(str_stopbit(get(handles.popupmenu_StopBit,'value'),:));
% --- Executes during object creation, after setting all properties.
function slider_pic_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_pic (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_pic_Callback(hObject, eventdata, handles)
% hObject    handle to slider_pic (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global slider_value;
global i;
global f_sampal;
global max_dot;
global data;
global min_y;
global max_y;
slider_value=get(handles.slider_pic,'Value');
if(get(handles.togglebutton_openoff,'Value')<1)
    temp=round(slider_value*(i-max_dot))+1;
    axes(handles.axes1)
    axis([temp/f_sampal,(temp+max_dot-1)/f_sampal,min_y,max_y])
%     line((temp:(temp+max_dot-1))./f_sampal,data(temp:(temp+max_dot-1)),'Color',[1,0,0],'Marker','.','MarkerSize',3);
    set(handles.h_chen,'Xdata',(temp:(temp+max_dot-1))./f_sampal,'Ydata',data(temp:(temp+max_dot-1)));
    guidata(hObject,handles);%更新数据
end 
    

% --- Executes on button press in togglebutton_openoff.
function togglebutton_openoff_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton_openoff (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 togglebutton_openoff
temp=get(handles.togglebutton_openoff,'Value');
global s_port;
global f_sampal;
global i;
global slider_value;
global max_dot;
global data;
global a_coef;
global b_coef;
global min_y;
global max_y;
min_y=-1;
max_y=3.3;
slider_value=0.95;
set(handles.slider_pic,'Value',slider_value);
if(temp)   %push down
max_dot=512;
i=0;
t=0;
data=[];
[n_a,m_a]=size(a_coef);
[n_b,m_b]=size(b_coef);
x1=zeros(1,m_a);%保留m_a个数据
y1=zeros(1,m_b-1);%保留m_b-1个数据
temp_data=zeros(max_dot,1);
%打开串口
fopen(s_port);
  while(strcmp(s_port.Status,'closed')) 
  end 
set(handles.togglebutton_openoff,'String','close port');
  while(get(handles.togglebutton_openoff,'Value'))
    fwrite(s_port,170);
    %s_port.BytesAvailable
    if(s_port.BytesAvailable)
    b=fread(s_port,2,'uint8');
    f_sampal=b(1)*256+b(2);
    set(handles.edit_f,'String',num2str(f_sampal));
    break;
    end
  end
%声明使用第一个轴系统
axes(handles.axes1)
axis([-max_dot/f_sampal,-1/f_sampal,min_y,max_y]);
handles.h_chen=line(zeros(max_dot,1),zeros(max_dot,1),'Color',[1,0,0],'Marker','.','MarkerSize',3);
x_k=((-max_dot):(-1))./f_sampal;
  while(get(handles.togglebutton_openoff,'Value'))
    data_size=0;
    data_size=s_port.BytesAvailable
     if(mod(data_size,2))
           data_size=data_size-1;
        end
    if(data_size>0) 
     b=fread(s_port,data_size,'uint8');
  %   b=(b.*2)./51-5;%接收的数据
     b=b(1:2:end)*256+b(2:2:end);
     b=b.*(max_y-min_y)./1023+min_y;
     data_size=data_size/2;
%      %对数据进行滤波
%       x=b;
%      if(i<1)%首次进来
%        for count=1:data_size
%          if(count==1)
%              x1=x(1);
%              y1=b_coef(1)*x1;
%              data=[data,y1];
%          else 
%              if(count<(m_b+1))
%              x1=[x1,x(count)];
%              temp_filter=b_coef(1:count)*x1(end:(-1):1)'-a_coef(2:count)*y1(end:(-1):1)'; 
%              data=[data,temp_filter];
%              y1=[y1,temp_filter];
%              if(count==m_b)
%                  y1=y1(2:end);
%              end
%              else
%              x1=[x1(2:end),x(count)];    
%              temp_filter=b_coef(1:end)*x1(end:(-1):1)'-a_coef(2:end)*y1(end:(-1):1)';
%              data=[data,temp_filter];
%              y1=[y1(2:end),temp_filter];
%              end    
%           end
%         end
%      else
%         for  count=1:data_size
%              x1=[x1(2:end),x(count)];    
%              temp_filter=b_coef(1:end)*x1(end:(-1):1)'-a_coef(2:end)*y1(end:(-1):1)';
%              data=[data,temp_filter];
%              y1=[y1(2:end),temp_filter];
%         end
%      end
%      %滤波完毕
     %如果不滤波
     data=[data;b];
     %可选
     i=i+data_size;
     if(i<max_dot)
       temp_data((end-i+1):end)=data;
       x_k=x_k+data_size./f_sampal;
     else
       temp_data=data((end-max_dot+1):end);
       start=round(slider_value*(i-max_dot))+1;
       x_k=(start:(start+max_dot-1))./f_sampal;
     end
     axis([x_k(1),x_k(end),min_y,max_y])
     set(handles.h_chen,'Xdata',x_k,'Ydata',temp_data);
     title('心电图形');
      guidata(hObject,handles);
     drawnow;
  end
 end
else
set(handles.togglebutton_openoff,'String','open port');
fclose(s_port);
delete(s_port);
clear s_port;
end
% --- Executes during object creation, after setting all properties.
function edit_f_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_f (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit_f_Callback(hObject, eventdata, handles)
% hObject    handle to edit_f (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_f as text
%        str2double(get(hObject,'String')) returns contents of edit_f as a double


% --- Executes on button press in pushbutton_save.
function pushbutton_save_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global data;
%matlab 6.5的程序
[filename, pathname] = uiputfile( ...
       {'*.txt','txt file(*.txt)';...
        '*.*','All Files (*.*)'}, ...
        'Save as')
str=strcat(pathname,filename);
% fin=fopen(str,'w');
% fprintf(fin,'%5d',data);
% fclose(fin);
dlmwrite(str,data,' ');
%下面的程序为matlab 6.5以上的版本才能用的版本
% [filename, pathname] = uiputfile( ...
%        {'*.txt','txt file(*.txt)';...
%         '*.xls','Excel file(*.xls)';...
%         '*.*','All Files (*.*)'}, ...
%         'Save as')
% str=strcat(pathname,filename);
% fin=fopen(str,'w');
% fclose(fin);
% DT=datestr(now)
% if (length(filename)<5)
%     msgbox('save file error','save file error');
%     return;
% end
% switch filename((end-3):end)
% case '.txt'
%     fprintf(fin,'%5d',data);
% case '.xsl'
%     xlswrite(fin,data);
% otherwise
%    msgbox('save file error','save file error');
%    return; 
% end
% fclose(fin)

% --- Executes on button press in pushbutton_Cancel.
function pushbutton_Cancel_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_Cancel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc;
clear all;
close(gcf);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -