当前位置: 首页 > news >正文

MATLAB2021B APP seriallist 串口通信

文章目录

  • 前言
  • 一、项目需要
  • 二、使用步骤
    • 1.查找串口填写到查找列表
    • 2.发送函数
    • 3. 接收函数
    • 4.检测串口按钮
    • 5.选择串口号
  • 总结


前言

提示:这里可以添加本文要记录的大概内容:

项目需要:


提示:以下是本篇文章正文内容,下面案例可供参考

一、项目需要

MATLAB 和串口通信为了进一步实现STM32 等单片机通信

二、使用步骤

1.查找串口填写到查找列表

添加初始化函数

在这里插入图片描述

在这里插入图片描述

代码如下(示例):

        % Code that executes after component creationfunction startupFcn(app)comlist=serialportlist;app.DropDown.Items=comlist;end

2.发送函数

点击发送按钮,将数据发送出去, TextArea里面的数据是cell格式,要串口发送要转换成字符数据,这里用了 data=cell2mat(dataToSend);
在这里插入图片描述

代码如下(示例):

        % Button pushed function: Button_5function send_serial(app, event)dataToSend=app.TextArea.Value;disp(dataToSend)
%             dataToSend=(uint8)dataTo;data=cell2mat(dataToSend);%             fprintf('com3  %s \n',dataToSend); write(app.Serial_Obj, data,"uint8"); end

3. 接收函数

新建接收函数
输入名称,点击+公共函数

在这里插入图片描述

自动生成代码,修改函数名后,添加函数输入参数

参考代码

  methods (Access = public)function results = recive(app,src,envent)receivedData = read(app.Serial_Obj, 5, 'char');app.TextArea_2.Value=receivedData;                receivedDataStr = char(receivedData);                % 显示接收到的数据  disp('Received Data:');  disp(receivedDataStr);         end

在这里插入图片描述

4.检测串口按钮

% 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.Items=app.ports;end

5.选择串口号

在这里插入图片描述

        function chose_com_v(app, event)value = app.DropDown.Value;
%             fprintf('com %s  \n',value); switch valuecase 'COM1'fprintf('    com1 \n'); case 'COM2'fprintf('com2  \n'); case 'COM3'fprintf('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.image1=imread('img6_1.jpg');
%             imshow(image1);%              i=imread('1.jpg');app.Image.ImageSource=app.image1;end% Button pushed function: Button_2function open_image2(app, event)app.Image2.ImageSource=app.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.Items=app.ports;end% Drop down opening function: DropDownfunction chose_com(app, event)%             value = app.DropDown.Value;%             x=0:0.01:5;
%             y=sin(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 'COM1'fprintf('    com1 \n'); case 'COM2'fprintf('com2  \n'); case 'COM3'fprintf('com3  \n'); endend% Button pushed function: Button_4function open_seiral(app, event)% 获取所有可用的串口端口号  
%                 portNames = {app.ports(var)}; % 这是一个单元数组  portNames=app.DropDown.Value;% 将单元数组转换为字符串数组(如果需要)  portNamesStr = string(portNames); % 在 MATLAB R2016b 及更高版本中可用  % 显示端口号  disp(portNamesStr);% 创建并打开串口  serialComName = portNamesStr;serialBaudrate = 9600;serialDataBit = 8;serialCheckBit = 'none';serialStopBit = 1;% 尝试打开串口tryapp.Serial_Obj=serialport(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)dataToSend=app.TextArea.Value;disp(dataToSend)
%             dataToSend=(uint8)dataTo;data=cell2mat(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

总结

学习使人快乐!
音乐使人愉悦!
日积月累使人充实和自信!

http://www.lryc.cn/news/505374.html

相关文章:

  • 【Python爬虫系列】_033.Scrapy_分布式爬虫
  • 2025erp系统开源免费进销存系统搭建教程/功能介绍/上线即可运营软件平台源码
  • Android实战经验篇-busybox小工具
  • 上海艾一公司-运维工程师知识点备战
  • 【网络安全】Web安全基础- 第一节:web前置基础知识
  • 数仓开发那些事_番外(2)
  • Linux常用指令-----下
  • MySQL通过binlog日志进行数据恢复
  • 【AIGC】与模型对话:理解与预防ChatGPT中的常见误解
  • 字符2
  • 25年宁德时代社招在职晋升Verify测评SHL题库:语言理解+数字推理考什么?
  • 数据转换:连接数据孤岛,释放信息价值
  • 提升PHP技能:18个实用高级特性
  • MySQL基础操作(2)
  • Windows环境 (Ubuntu 24.04.1 LTS ) 国内镜像,用apt-get命令安装RabbitMQ
  • web网页前后端交互方式
  • LN61C 高精度 低功耗 小封装 电压检测芯片
  • 自动驾驶控制与规划——Project 2: 车辆横向控制
  • Bootstrap-HTML(五)图像基础样式
  • bain.js(十二):RNN神经网络实战教程 - 音乐乐谱生成 -人人都是作曲家~
  • Endnote | 查看文献所在分组
  • DateRangePickerDialog组件的用法
  • 数据库合并操作:深入理解 MERGE INTO 语句
  • 联发科MTK8788_MT8788安卓核心板安兔兔跑分_安卓主板方案商
  • 计算机网络技术基础:6.数据传输方式
  • 免费开源了一个图床工具 github-spring-boot-starter
  • Mysql之YUM安装时GPG 密钥报错问题处理
  • Hw亮度省电
  • 【信息系统项目管理师-论文真题】2015下半年论文详解
  • django的model中定义【记录修改次数】的这个字段该用什么类型