任务平台网站建设,潍坊网站关键词,百度搜索关键词优化方法,室内设计可以去哪些公司文章目录 前言一、项目需要二、使用步骤1.查找串口填写到查找列表2.发送函数3. 接收函数4.检测串口按钮5.选择串口号 总结 前言
提示#xff1a;这里可以添加本文要记录的大概内容#xff1a;
项目需要#xff1a; 提示#xff1a;以下是本篇文章正文内容#xff0c;下面… 文章目录 前言一、项目需要二、使用步骤1.查找串口填写到查找列表2.发送函数3. 接收函数4.检测串口按钮5.选择串口号 总结 前言
提示这里可以添加本文要记录的大概内容
项目需要 提示以下是本篇文章正文内容下面案例可供参考
一、项目需要
MATLAB 和串口通信为了进一步实现STM32 等单片机通信
二、使用步骤
1.查找串口填写到查找列表
添加初始化函数 代码如下示例 % Code that executes after component creationfunction startupFcn(app)comlistserialportlist;app.DropDown.Itemscomlist;end2.发送函数
点击发送按钮将数据发送出去 TextArea里面的数据是cell格式要串口发送要转换成字符数据这里用了 datacell2mat(dataToSend);
代码如下示例 % Button pushed function: Button_5function send_serial(app, event)dataToSendapp.TextArea.Value;disp(dataToSend)
% dataToSend(uint8)dataTo;datacell2mat(dataToSend);% fprintf(com3 %s \n,dataToSend); write(app.Serial_Obj, data,uint8); end3. 接收函数
新建接收函数 输入名称点击公共函数 自动生成代码修改函数名后添加函数输入参数
参考代码 methods (Access public)function results recive(app,src,envent)receivedData read(app.Serial_Obj, 5, char);app.TextArea_2.ValuereceivedData; receivedDataStr char(receivedData); % 显示接收到的数据 disp(Received Data:); disp(receivedDataStr); end4.检测串口按钮
% Button pushed function: Button_3
function check_serial(app, event)% global portsapp.ports serialportlist; % 检查是否有可用的串口 if isempty(app.ports) disp(没有检测到任何串口设备。); else % 显示串口信息 for i 1:length(app.ports) fprintf(Port %d: %s\n, i, app.ports(i)); end end% 假设 handles.popupmenu1 是已经创建的 popupmenu 控件 % 创建一个包含多个字符串的单元格数组 % options {Oon 1, Option 2, Option 3}; % 将这个单元格数组设置为 popupmenu 的 String 属性 app.DropDown.Itemsapp.ports;end5.选择串口号 function chose_com_v(app, event)value app.DropDown.Value;
% fprintf(com %s \n,value); switch valuecase COM1fprintf( com1 \n); case COM2fprintf(com2 \n); case COM3fprintf(com3 \n); endend完整程序
classdef app1 matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access public)UIFigure matlab.ui.FigureTextArea_2 matlab.ui.control.TextAreaLabel_2 matlab.ui.control.LabelButton_5 matlab.ui.control.ButtonTextArea matlab.ui.control.TextAreaLabel matlab.ui.control.LabelButton_4 matlab.ui.control.ButtonButton_3 matlab.ui.control.ButtonDropDown matlab.ui.control.DropDownDropDownLabel matlab.ui.control.LabelButton_2 matlab.ui.control.ButtonImage2 matlab.ui.control.ImageImage matlab.ui.control.ImageButton matlab.ui.control.Buttonendproperties (Access public)
% Property % Descriptionimage1portsSerial_Objend% Callbacks that handle component eventsmethods (Access private)% Button pushed function: Buttonfunction open_image(app, event)app.image1imread(img6_1.jpg);
% imshow(image1);% iimread(1.jpg);app.Image.ImageSourceapp.image1;end% Button pushed function: Button_2function open_image2(app, event)app.Image2.ImageSourceapp.image1;end% Button pushed function: Button_3function check_serial(app, event)% global portsapp.ports serialportlist; % 检查是否有可用的串口
if isempty(app.ports) disp(没有检测到任何串口设备。);
else % 显示串口信息 for i 1:length(app.ports) fprintf(Port %d: %s\n, i, app.ports(i)); end
end% 假设 handles.popupmenu1 是已经创建的 popupmenu 控件
% 创建一个包含多个字符串的单元格数组
% options {Oon 1, Option 2, Option 3}; % 将这个单元格数组设置为 popupmenu 的 String 属性
app.DropDown.Itemsapp.ports;end% Drop down opening function: DropDownfunction chose_com(app, event)% value app.DropDown.Value;% x0:0.01:5;
% ysin(x);
% switch value
% case 红色
% plot(app.UIAxes,x,y,r)
% case 绿色
% plot(app.UIAxes,x,y,g)
% case 黄色
% plot(app.UIAxes,x,y,y)
% endend% Value changed function: DropDownfunction chose_com_v(app, event)value app.DropDown.Value;
% fprintf(com %s \n,value); switch valuecase COM1fprintf( com1 \n); case COM2fprintf(com2 \n); case COM3fprintf(com3 \n); endend% Button pushed function: Button_4function open_seiral(app, event)% 获取所有可用的串口端口号
% portNames {app.ports(var)}; % 这是一个单元数组 portNamesapp.DropDown.Value;% 将单元数组转换为字符串数组如果需要 portNamesStr string(portNames); % 在 MATLAB R2016b 及更高版本中可用 % 显示端口号 disp(portNamesStr);% 创建并打开串口 serialComName portNamesStr;serialBaudrate 9600;serialDataBit 8;serialCheckBit none;serialStopBit 1;% 尝试打开串口tryapp.Serial_Objserialport(serialComName,serialBaudrate,Parity,serialCheckBit,DataBits,serialDataBit,StopBits,serialStopBit,Timeout,1);text1 串口打开成功;disp(text1)dataToSend Hello, Serial Port!; write(app.Serial_Obj, dataToSend, uint8); catch% 串口打开失败text 串口打开失败;disp(text)% 删除串口delete(app.Serial_Obj);endend% Button pushed function: Button_5function send_serial(app, event)dataToSendapp.TextArea.Value;disp(dataToSend)
% dataToSend(uint8)dataTo;datacell2mat(dataToSend);% fprintf(com3 %s \n,dataToSend); write(app.Serial_Obj, data,uint8); endend% Component initializationmethods (Access private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure uifigure(Visible, off);app.UIFigure.Position [100 100 737 525];app.UIFigure.Name MATLAB App;% Create Buttonapp.Button uibutton(app.UIFigure, push);app.Button.ButtonPushedFcn createCallbackFcn(app, open_image, true);app.Button.Position [98 409 100 24];app.Button.Text {打开图像; };% Create Imageapp.Image uiimage(app.UIFigure);app.Image.Position [524 275 200 251];% Create Image2app.Image2 uiimage(app.UIFigure);app.Image2.Position [525 97 210 200];% Create Button_2app.Button_2 uibutton(app.UIFigure, push);app.Button_2.ButtonPushedFcn createCallbackFcn(app, open_image2, true);app.Button_2.Position [99 354 100 24];app.Button_2.Text 打开图像2;% Create DropDownLabelapp.DropDownLabel uilabel(app.UIFigure);app.DropDownLabel.HorizontalAlignment right;app.DropDownLabel.Position [71 275 66 22];app.DropDownLabel.Text Drop Down;% Create DropDownapp.DropDown uidropdown(app.UIFigure);app.DropDown.DropDownOpeningFcn createCallbackFcn(app, chose_com, true);app.DropDown.ValueChangedFcn createCallbackFcn(app, chose_com_v, true);app.DropDown.Position [152 275 100 22];% Create Button_3app.Button_3 uibutton(app.UIFigure, push);app.Button_3.ButtonPushedFcn createCallbackFcn(app, check_serial, true);app.Button_3.Position [99 191 100 24];app.Button_3.Text 查找串口;% Create Button_4app.Button_4 uibutton(app.UIFigure, push);app.Button_4.ButtonPushedFcn createCallbackFcn(app, open_seiral, true);app.Button_4.Position [99 120 100 24];app.Button_4.Text 打开串口;% Create Labelapp.Label uilabel(app.UIFigure);app.Label.HorizontalAlignment right;app.Label.Position [278 390 29 22];app.Label.Text 发送;% Create TextAreaapp.TextArea uitextarea(app.UIFigure);app.TextArea.Position [322 354 150 60];% Create Button_5app.Button_5 uibutton(app.UIFigure, push);app.Button_5.ButtonPushedFcn createCallbackFcn(app, send_serial, true);app.Button_5.Position [100 49 100 24];app.Button_5.Text 发送;% Create Label_2app.Label_2 uilabel(app.UIFigure);app.Label_2.HorizontalAlignment right;app.Label_2.Position [279 301 29 22];app.Label_2.Text 接收;% Create TextArea_2app.TextArea_2 uitextarea(app.UIFigure);app.TextArea_2.Position [324 269 150 60];% Show the figure after all components are createdapp.UIFigure.Visible on;endend% App creation and deletionmethods (Access public)% Construct appfunction app app1% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)if nargout 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end总结
学习使人快乐 音乐使人愉悦 日积月累使人充实和自信