作用效果:利用matlab的串口将信息发送到arduino端,并根据信息内容执行相应的程序。例如,matlab发送字符‘ssP’,arduino收到P时开始执行(相当于截止字符),提取字符串“ssP”的第一位,如果kk=第一位==‘s’,则开始闪烁灯。
matlab代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20clear;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代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47char 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的全部的代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163%%%%利用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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复