Matlab串口函数
% % H = SERIAL1 returns the handle to a new SERIAL1 or the handle to % the existing singleton*. % % SERIAL1('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in SERIAL1.M with the given input arguments. % % SERIAL1('Property','Value',...) creates a new SERIAL1 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before serial1_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to serial1_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
% --- Executes just before serial1 is made visible. function serial1_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 serial1 (see VARARGIN) global s; handles.mycom = s; % Choose default command line output for serial1 handles.output = hObject;
fopen(obj1); %打开串口 fopen(obj2); k=1; datas=zeros([1,100]); while(k<101)
if obj1.BytesAvailable datas(1,k)=fread(obj1,1);
end k=k+1; plot(datas); %向测试串口中写一个数据数据 在 COM3 中就可以接受到该数据 fwrite(obj2,round(255*rand(1))); pause(1); end fclose(obj1);
功能描述: 1.选择串口,点击 START,即等待串口发送数据,数据缓存 4000bytes。接收缓存区满后绘 图,同时显示最大电压值和波形功率值。 2.File 菜单下有 Import Data 和 Export Data 功能,可将电压数据保存到 txt 文件中,同时可 读取 txt 中的数据并显示相关波形。 3.Image 菜单下有 Open Image 和 Save Image 功能,可将波形保存成图片格式,同时可打开 图片显示在绘图窗口。 Matlab 源代码: function varargout = serial1(varargin) % SERIAL1 M-file for serial1.fig % SERIAL1, by itself, creates a new SERIAL1 or raises the existing % singleton*.
fopen 打开串口对象,格式:fope n( s)
fread 读取串口数据,格式: fread(s)
fclose 关闭串口对象,格式:fclose(s)
free 解除 Matlab 对串口对象进行读写操作
delete 删除对象 s,格式:delete( s)
机。 Matlab 支持面向对象技术,用一个对象将计算机串口封装起来,只要创建串口对象,对串 口对象操作就是对串口操作,非常方便。使用 serial 函数就可创建串口对象,串口对象有很 多属性[4],通过定义串口对象的属性,能定义串口的通信模式,从串口对象属性也能了解 串口的状态。要想通过串口传输数据,必须先用 fopen 函数打开串口;数据传输结束后要用 fclose。函数关闭串口。表 1 列出了常用的与串口操作有关的函数。
% Update handles structure guidata(hObject, handles);
% UIWAIT makes serial1 wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = serial1_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)
clear 从工作空间中删除对象 s,格式:clear(s)
___________________________________________________
当建立了一个串口对象后,可以利用 Matlab 对其参数进行设置,包括对波特率、同步或异 步读取方式、读取或写数据的等待时间等。本文以波特率的设置为例来说明串口参数的设置 方法。 波特率的设置,一般有两种方法:(1)在创建串口对象时设定波特率:s= serial(`coml' ,'baudrate','9600'); (2)用 set 命令:set(s,'baudrate','9600')。
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else gui_mainfcn(gui_State, varargin{:});
end % End initialization code - DO NOT EDIT %global s selected_com selected_baud selected_databits selected_stopbits selected_parity selected_flowctrl
表 1 Matlab 串口函数介绍
_____________________________________________________
函数
说明
_____________________________________________________
serial 创建一个串口对象,格式:s = serial('coml' )
matlab 串口编程 2009 年 06 月 10 日 星期三 下午 11:43
function serialdwj %本程序使用的是虚拟串口软件 VSPD 虚拟的 COM2 和 COM3 来测试的 %使用 COM1 也可以使用 已经经过测试了 %如果要使用 请使用 COM1 口 就可以与下位机通信了 obj1=instrfind('Type','serial','Port','COM3','Tag',''); %COM2 为测试串口 obj2=instrfind('Type','serial','Port','COM2','Tag',''); if isempty(obj1)
delete(obj1); %删除测试串口 fclose(obj2); delete(obj2); end
%现在使用的 m 文件来显示的 %到后来有机会就使用 gui 界面来显示
MATLAB 接收串口数据 GUI 界面 2009-09-19 12:42 工作流程:AD7818 采样模拟信号,12bits 输出,串口将数据发送给 PC。串口发送波特率 57600Bd,数据位 8 位,停止位一位。每个数据分两次发送,高四位补零。设计 GUI 界面接 收串口数据并显示波形。 GUI 界面如下:
Matlab 串口函数 2009-09-25 21:22 Matlab 是数学计算的强大工具,它以矩阵作为数据操作的基本单位,广泛应用在以矩阵运 算为主要工作方式的数理统计、自动控制、数字信号处理、动态系统仿真等领域。Matlab 便捷灵活、操作简单、处理功能强大,因而利用 Matlab 对采 集到的 ABS 试验数据进行分析处理是非常合适的,关键问题是如何将数据从 ECU 传到计算