我是靠谱客的博主 洁净季节,最近开发中收集的这篇文章主要介绍Matlab和Arduino的连接(四):在matlab中串口通信发送相关信息至arduino(记录用),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
作用效果:利用matlab的串口将信息发送到arduino端,并根据信息内容执行相应的程序。例如,matlab发送字符‘ssP’,arduino收到P时开始执行(相当于截止字符),提取字符串“ssP”的第一位,如果kk=第一位==‘s’,则开始闪烁灯。
matlab代码
clear;clc;
delete(instrfind({'Port'},{'COM6'}));%断开,释放串口对象占用空间
global s;%建立全局变量s
s = serial('COM6','BaudRate',9600,'DataBits',8);%设定相关的串口参数
fopen(s);
set(s,'BytesAvailableFcnMode','Terminator');
set(s,'Terminator','H'); %设立中断,当串口检测到字符H时触发中断
s.BytesAvailableFcn =@ReceiveCallback; %中断触发后处理回调的事
for k=1:1:3
fprintf(s,'lsH');
pause(0.3);%时间间隔不宜过小
end
function ReceiveCallback(obj,event) %执行回调的函数
global s;
a = fscanf(s);
disp(a);
I = 'I Received';
disp(I);
end
arduino代码
char function_1;
String Indata;
String kk;
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
if(Serial.available()>0){
function_1 = Serial.read();
Indata +=function_1;
//Serial.print("fggk");
if(function_1 == 'H'){
//Serial.print("jgf");
//Serial.print(Indata);
delay(20);
kk = Indata.substring(0,1);//调取字符串的第一个字符
Serial.print(kk);
Serial.print("H");
if(kk =="s")
{
for(int i=0;i<5;i++){
//Serial.print(kk);
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
Indata = "";
}
}
if(kk=="l"){
digitalWrite(13,HIGH);
delay(4000);
}
if(kk=="o"){
digitalWrite(13,LOW);
delay(4000);
digitalWrite(13,HIGH);
delay(4000);
Indata="";
}
}
}
}
matlab利用GUI发送串口信息:代码
Matlab GUI的全部的代码
%%%%利用matlab的GUI控制步进电机的运转方向,角度
function varargout = GUI_steppermotor(varargin)
% GUI_STEPPERMOTOR MATLAB code for GUI_steppermotor.fig
% GUI_STEPPERMOTOR, by itself, creates a new GUI_STEPPERMOTOR or raises the existing
% singleton*.
%
% H = GUI_STEPPERMOTOR returns the handle to a new GUI_STEPPERMOTOR or the handle to
% the existing singleton*.
%
% GUI_STEPPERMOTOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_STEPPERMOTOR.M with the given input arguments.
%
% GUI_STEPPERMOTOR('Property','Value',...) creates a new GUI_STEPPERMOTOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_steppermotor_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_steppermotor_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 GUI_steppermotor
% Last Modified by GUIDE v2.5 17-Apr-2021 13:42:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_steppermotor_OpeningFcn, ...
'gui_OutputFcn', @GUI_steppermotor_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 GUI_steppermotor is made visible.
function GUI_steppermotor_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 GUI_steppermotor (see VARARGIN)
% Choose default command line output for GUI_steppermotor
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_steppermotor wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_steppermotor_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
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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)
% --- 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(instrfind({'Port'},{'COM6'}));
% puerto_serial = serial('COM6');%查找通信接口对象并断开。
% puerto.serial.BaudRate = 9600;%设置波特率
% warning('off','MATLAB:serial:fscanf:unsuccessfulRead');%关闭警告信息
% fopen(puerto_serial);%打开串口'TimeOut',10
%fprintf('ff');
delete(instrfind({'Port'},{'COM6'}));
val_1 = get(handles.popupmenu1,'value');
angle_1 = num2str(handles.edit1.String);
switch(val_1)
case 2
dir_1 = 's';
case 3
dir_1 = 'l';
end
delete(instrfind({'Port'},{'COM6'}));%断开,释放串口对象占用空间
global s;%建立全局变量s
s = serial('COM6','BaudRate',9600,'DataBits',8);%设定相关的串口参数
fopen(s);
set(s,'BytesAvailableFcnMode','Terminator');
set(s,'Terminator','H'); %设立中断,当串口检测到字符H时触发中断
s.BytesAvailableFcn =@ReceiveCallback; %中断触发后处理回调的事
HH = [dir_1,angle_1,'H'];
disp(HH);
for k=1:1:3
fprintf(s,HH);
pause(0.3);%时间间隔不宜过小
end```
最后
以上就是洁净季节为你收集整理的Matlab和Arduino的连接(四):在matlab中串口通信发送相关信息至arduino(记录用)的全部内容,希望文章能够帮你解决Matlab和Arduino的连接(四):在matlab中串口通信发送相关信息至arduino(记录用)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复