文章目录
- 前言
- 一、项目需要
- 二、使用步骤
- 1.查找串口填写到查找列表
- 2.发送函数
- 3. 接收函数
- 4.检测串口按钮
- 5.选择串口号
- 总结
前言
提示:这里可以添加本文要记录的大概内容:
项目需要:
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目需要
MATLAB 和串口通信为了进一步实现STM32 等单片机通信
二、使用步骤
1.查找串口填写到查找列表
添加初始化函数
代码如下(示例):
% Code that executes after component creation
function startupFcn(app)
comlist=serialportlist;
app.DropDown.Items=comlist;
end
2.发送函数
点击发送按钮,将数据发送出去, TextArea里面的数据是cell格式,要串口发送要转换成字符数据,这里用了 data=cell2mat(dataToSend);
代码如下(示例):
% Button pushed function: Button_5
function 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 ports
app.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 value
case 'COM1'
fprintf(' com1 \n');
case 'COM2'
fprintf('com2 \n');
case 'COM3'
fprintf('com3 \n');
end
end
完整程序
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
TextArea_2 matlab.ui.control.TextArea
Label_2 matlab.ui.control.Label
Button_5 matlab.ui.control.Button
TextArea matlab.ui.control.TextArea
Label matlab.ui.control.Label
Button_4 matlab.ui.control.Button
Button_3 matlab.ui.control.Button
DropDown matlab.ui.control.DropDown
DropDownLabel matlab.ui.control.Label
Button_2 matlab.ui.control.Button
Image2 matlab.ui.control.Image
Image matlab.ui.control.Image
Button matlab.ui.control.Button
end
properties (Access = public)
% Property % Description
image1
ports
Serial_Obj
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function 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_2
function open_image2(app, event)
app.Image2.ImageSource=app.image1;
end
% Button pushed function: Button_3
function check_serial(app, event)
% global ports
app.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: DropDown
function 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')
% end
end
% Value changed function: DropDown
function chose_com_v(app, event)
value = app.DropDown.Value;
% fprintf('com %s \n',value);
switch value
case 'COM1'
fprintf(' com1 \n');
case 'COM2'
fprintf('com2 \n');
case 'COM3'
fprintf('com3 \n');
end
end
% Button pushed function: Button_4
function 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;
% 尝试打开串口
try
app.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);
end
end
% Button pushed function: Button_5
function 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
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 737 525];
app.UIFigure.Name = 'MATLAB App';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @open_image, true);
app.Button.Position = [98 409 100 24];
app.Button.Text = {'打开图像'; ''};
% Create Image
app.Image = uiimage(app.UIFigure);
app.Image.Position = [524 275 200 251];
% Create Image2
app.Image2 = uiimage(app.UIFigure);
app.Image2.Position = [525 97 210 200];
% Create Button_2
app.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 DropDownLabel
app.DropDownLabel = uilabel(app.UIFigure);
app.DropDownLabel.HorizontalAlignment = 'right';
app.DropDownLabel.Position = [71 275 66 22];
app.DropDownLabel.Text = 'Drop Down';
% Create DropDown
app.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_3
app.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_4
app.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 Label
app.Label = uilabel(app.UIFigure);
app.Label.HorizontalAlignment = 'right';
app.Label.Position = [278 390 29 22];
app.Label.Text = '发送';
% Create TextArea
app.TextArea = uitextarea(app.UIFigure);
app.TextArea.Position = [322 354 150 60];
% Create Button_5
app.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_2
app.Label_2 = uilabel(app.UIFigure);
app.Label_2.HorizontalAlignment = 'right';
app.Label_2.Position = [279 301 29 22];
app.Label_2.Text = '接收';
% Create TextArea_2
app.TextArea_2 = uitextarea(app.UIFigure);
app.TextArea_2.Position = [324 269 150 60];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
总结
学习使人快乐!
音乐使人愉悦!
日积月累使人充实和自信!