STM32
直播中

廖阿朋

8年用户 1294经验值
私信 关注
[问答]

如何对stm32与matlab进行通讯实验呢

如何对STM32matlab进行通讯实验呢?
串口通信matlab代码该怎样去编写呢?

回帖(2)

李正茂

2021-11-18 14:33:31
  小白第一篇文章见谅!
  stm32与matlab通讯实验效果:
  
  2.串口通信matlab代码
  %与stm32串口通信实验
  delete(instrfindall);%注销系统之前已经打开了串口资源
  obj = serial('COM8','BaudRate',115200);%串口配置
  fopen(obj);%打开串口
  fprintf(obj,'hello12365'); %以ASCII格式发送
  大小 = 20;
  acceptingdata = fscanf(obj,'%s',size);%以ASCII格式接收
  data=sprintf('返回值=%s',acceptingdata);
  disp(data);%显示返回值
  2.stm32统一用的野火指南者实验代码
  3.matlab gui效果
  
  4.matlab gui 全部代码
  ni function varargout = untitled1(varargin)
  % UNTITLED1 MATLAB code for untitled1.fig
  % UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing
  % singleton*.
  %
  % H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle to
  % the existing singleton*.
  %
  % UNTITLED1(‘CALLBACK’,hObject,eventData,handles,。..) calls the local
  % function named CALLBACK in UNTITLED1.M with the given input arguments.
  %
  % UNTITLED1(‘Property’,‘Value’,。..) creates a new UNTITLED1 or raises the
  % existing singleton*. Starting from the left, property value pairs are
  % applied to the GUI before untitled1_OpeningFcn gets called. An
  % unrecognized property name or invalid value makes property application
  % stop. All inputs are passed to untitled1_OpeningFcn via varargin.
  %
  % *See GUI Options on GUIDE‘s Tools menu. Choose “GUI allows only one
  % instance to run (singleton)”。
  %
  % See also: GUIDE, GUIDATA, GUIHANDLES
  % Edit the above text to modify the response to help untitled1
  % Last Modified by GUIDE v2.5 19-Jun-2021 19:58:43
  % Begin initialization code - DO NOT EDIT
  gui_Singleton = 1;
  gui_State = struct(’gui_Name‘, mfilename, 。..
  ’gui_Singleton‘, gui_Singleton, 。..
  ’gui_OpeningFcn‘, @untitled1_OpeningFcn, 。..
  ’gui_OutputFcn‘, @untitled1_OutputFcn, 。..
  ’gui_LayoutFcn‘, [] , 。..
  ’gui_Callback‘, []);
  if nargin && ischar(varargin{1})
  gui_State.gui_Callback = str2func(varargin{1});
  end
  if nargout
  [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  else
  gui_mainfcn(gui_State, varargin{:});
  end
  % End initialization code - DO NOT EDIT
  % --- Executes just before untitled1 is made visible.
  function untitled1_OpeningFcn(hObject, eventdata, handles, varargin)
  % This function has no output args, see OutputFcn.
  % hObject handle to figure
  % eventdata reserved - to be defined in a future version of MATLAB
  % handles structure with handles and user data (see GUIDATA)
  % varargin command line arguments to untitled1 (see VARARGIN)
  % Choose default command line output for untitled1
  handles.output = hObject;
  % Update handles structure
  guidata(hObject, handles);
  % UIWAIT makes untitled1 wait for user response (see UIRESUME)
  % uiwait(handles.figure1);
  % --- Outputs from this function are returned to the command line.
  function varargout = untitled1_OutputFcn(hObject, eventdata, handles)
  % varargout cell array for returning output args (see VARARGOUT);
  % hObject handle to figure
  % eventdata reserved - to be defined in a future version of MATLAB
  % handles structure with handles and user data (see GUIDATA)
  % Get default command line output from handles structure
  varargout{1} = handles.output;
  function edit1_Callback(hObject, eventdata, handles)
  % hObject handle to edit1 (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 edit1 as text
  % str2double(get(hObject,’String‘)) returns contents of edit1 as a double
  % --- Executes during object creation, after setting all properties.
  function edit1_CreateFcn(hObject, eventdata, handles)
  % hObject handle to edit1 (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  function edit2_Callback(hObject, eventdata, handles)
  % hObject handle to edit2 (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 edit2 as text
  % str2double(get(hObject,’String‘)) returns contents of edit2 as a double
  % --- Executes during object creation, after setting all properties.
  function edit2_CreateFcn(hObject, eventdata, handles)
  % hObject handle to edit2 (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  % --- Executes on button press in pushbutton1.
  function pushbutton1_Callback(hObject, eventdata, handles)
  % hObject handle to pushbutton1 (see GCBO)
  % eventdata reserved - to be defined in a future version of MATLAB
  % handles structure with handles and user data (see GUIDATA)
  %与stm32串口通信实验
  % delete(instrfindall);%注销系统之前已经打开的串口资源
  % obj = serial(’COM8‘,’rate1‘,115200);%串口配置
  % fopen(obj);%打开串口
  global obj;
  data=get(handles.edit1,’string‘);
  fprintf(obj,data); %以ASCII格式发送
  size = 20;
  acceptingdata = fscanf(obj,’%s‘,size);%以ASCII格式接收
  data=sprintf(’%s‘,acceptingdata);
  set(handles.edit2,’string‘,data);
  % --- Executes on button press in pushbutton2.
  function pushbutton2_Callback(hObject, eventdata, handles)
  % hObject handle to pushbutton2 (see GCBO)
  % eventdata reserved - to be defined in a future version of MATLAB
  % handles structure with handles and user data (see GUIDATA)
  delete(instrfindall);%注销系统之前已经打开的串口资源
  %com=get(handles.serialcom,’string‘);
  global BD;
  global PD;
  global DD;
  global SD;
  global obj;
  global COMSERIAL;
  if(isempty(COMSERIAL))
  COMSERIAL=’COM8‘;
  end
  if(isempty(BD))
  BD=115200;
  end
  if(isempty(PD))
  PD=’none‘;
  end
  if(isempty(DD))
  DD=8;
  end
  if(isempty(SD))
  SD=1;
  end
  COM=COMSERIAL;
  COM
  BaudRate=BD;
  BaudRate
  Parity=PD;
  Parity
  DataBits=DD;
  DataBits
  StopBits=SD;
  StopBits
  obj = serial(COM);%串口配置
  obj.BaudRate =BD;
  obj.Parity =PD;
  obj.DataBits =DD;
  obj.StopBits =SD;
  fopen(obj);%打开串口
  fprintf(’nr%snr‘,’打开串口成功‘);
  function rate1_Callback(hObject, eventdata, handles)
  % hObject handle to rate1 (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 rate1 as text
  % str2double(get(hObject,’String‘)) returns contents of rate1 as a double
  global BD;
  vall=get(handles.rate1,’value‘);
  switch vall
  case 1
  BD=9600;
  case 2
  BD=19200;
  case 3
  BD=115200;
  end
  % --- Executes during object creation, after setting all properties.
  function rate1_CreateFcn(hObject, eventdata, handles)
  % hObject handle to rate1 (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  function serialcom_Callback(hObject, eventdata, handles)
  % hObject handle to serialcom (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 serialcom as text
  % str2double(get(hObject,’String‘)) returns contents of serialcom as a double
  global COMSERIAL;
  vall=get(handles.serialcom,’value‘);
  switch vall
  case 1
  COMSERIAL=’COM1‘;
  case 2
  COMSERIAL=’COM2‘;
  case 3
  COMSERIAL=’COM3‘;
  case 4
  COMSERIAL=’COM8‘;
  end
  % --- Executes during object creation, after setting all properties.
  function serialcom_CreateFcn(hObject, eventdata, handles)
  % hObject handle to serialcom (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  function parity_Callback(hObject, eventdata, handles)
  % hObject handle to parity (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 parity as text
  % str2double(get(hObject,’String‘)) returns contents of parity as a double
  global PD;
  vall=get(handles.parity,’value‘);
  switch vall
  case 1
  PD=’none‘;
  end
  % --- Executes during object creation, after setting all properties.
  function parity_CreateFcn(hObject, eventdata, handles)
  % hObject handle to parity (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  
举报

李恬恬

2021-11-18 14:34:12
function databit_Callback(hObject, eventdata, handles)
  % hObject handle to databit (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 databit as text
  % str2double(get(hObject,’String‘)) returns contents of databit as a double
  global DD;
  databits_data=get(handles.databit,’value‘);
  switch databits_data
  case 1
  DD=8;
  end
  % --- Executes during object creation, after setting all properties.
  function databit_CreateFcn(hObject, eventdata, handles)
  % hObject handle to databit (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  function stopbit_Callback(hObject, eventdata, handles)
  % hObject handle to stopbit (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 stopbit as text
  % str2double(get(hObject,’String‘)) returns contents of stopbit as a double
  global SD;
  stopbits_data=get(handles.stopbit,’value‘);
  switch stopbits_data
  case 1
  SD=1;
  end
  % --- Executes during object creation, after setting all properties.
  function stopbit_CreateFcn(hObject, eventdata, handles)
  % hObject handle to stopbit (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  % --- Executes on button press in pushbutton3.
  function pushbutton3_Callback(hObject, eventdata, handles)
  % hObject handle to pushbutton3 (see GCBO)
  % eventdata reserved - to be defined in a future version of MATLAB
  % handles structure with handles and user data (see GUIDATA)
  delete(instrfindall);
  fprintf(’nr%snr‘,’串口已关闭‘);
  function kj2_Callback(hObject, eventdata, handles)
  % hObject handle to kj2 (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 kj2 as text
  % str2double(get(hObject,’String‘)) returns contents of kj2 as a double
  % --- Executes during object creation, after setting all properties.
  function kj2_CreateFcn(hObject, eventdata, handles)
  % hObject handle to kj2 (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
  % --- Executes on button press in kj1.
  function kj1_Callback(hObject, eventdata, handles)
  % hObject handle to kj1 (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 kj1
  value=get(handles.kj1,’value‘);
  set(handles.kj2,’string‘,num2str(value));
  function brate_Callback(hObject, eventdata, handles)
  % hObject handle to brate (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 brate as text
  % str2double(get(hObject,’String‘)) returns contents of brate as a double
  global BD;
  bdrate_data=get(handles.brate,’value‘);
  switch bdrate_data
  case 1
  BD=9600;
  case 2
  BD=19200;
  case 3
  BD=115200;
  end
  % --- Executes during object creation, after setting all properties.
  function brate_CreateFcn(hObject, eventdata, handles)
  % hObject handle to brate (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 && isequal(get(hObject,’BackgroundColor‘), get(0,’defaultUicontrolBackgroundColor‘))
  set(hObject,’BackgroundColor‘,’white‘);
  end
举报

更多回帖

发帖
×
20
完善资料,
赚取积分