在Matlab中使用串口进行实时信号记录

时间:2014-08-30 12:29:47

标签: matlab real-time

我想使用MATLAB实时窗口目标执行实时信号记录。如何定义输入块以实时使用串行端口从我自己创建的数据数据采集板接收数据。每次我尝试定义一个COM端口,我收到此错误:

'rt_test /数字输入'中S函数'rtwindi'报告的错误: 电路板“标准设备串行端口”没有数字输入通道

1 个答案:

答案 0 :(得分:-1)

maybe this program help you

%% Vincent Chow
% chow@college.harvard.edu
%
% CS50 Project - Plots accelerometer data from serial port in real time
%
% Configuration:
% - Place Arduino USB cable in correct port
% - Close serial port if interrupted. Use fclose() and
% delete() in command window.
%
% Diagnostic tools:
% - instrfind returns open serial ports
%
% References:
% Template provided by http://billwaa.wordpress.com/2013/07/10/matlab-real-time-serial-data-logger/
%% Configuration
% set correct serial port address
port = '/dev/tty.usbmodem1411';
% variables for data and plotting
time = 0;
x_data = 0;
y_data = 0;
z_data = 0;
index = 0;
scroll_width = 10;
delay = 0.010;
min = 200;
max = 800;
loop = 0;
% initalize plots for x, y, and z data
graph1 = plot(time, x_data, 'm-');
hold on;
graph2 = plot(time, y_data, 'c-');
hold on;
graph3 = plot(time, z_data, 'g-');
% labeling axes
xlabel('Time');
ylabel('Accelerometer Data');
legend('X Data', 'Y Data', 'Z Data');
title('ADXL345 Accelerometer Data');
% setup serial port
s = serial(port);
% since data is sent in a buffer of 10 bytes, our buffer is 2^10 in size
set(s, 'InputBufferSize', 1024);
% microcontroller controls data flow
set(s, 'FlowControl', 'hardware');
% chosen baudrate allows for realtime plotting without significant delay
set(s, 'BaudRate', 57600);
% no parity bit checking
set(s, 'Parity', 'none');
% set DataBits at 8 to allow transmission of binary data
set(s, 'DataBits', 8);
% set number of bits indicating end of data transmission to 1
set(s, 'StopBit', 1);
% set timeout to 10 seconds
set(s, 'Timeout', 10);
% open serial port as file for read access
fopen(s);
% start time
tic;
%% Plotting
% plots data while graphs are open
while ishandle(graph1)
% obtain data from serial port
stream = fscanf(s, '%f %f %f')';
% loops to plot every 10th data point - reduces delay
if (loop < 10)
loop = loop + 1;
continue;
elseif (loop == 10)
loop = 0;
end
% plot as long as data stream is not empty
if(~isempty(stream) && numel(stream) == 3)
% increment index
index = index + 1;
% increment time
time(index) = toc;
% take data from stream into respective data arrays
x_data(index) = stream(1, 1);
y_data(index) = stream(1, 2);
z_data(index) = stream(1, 3);
% plots data after initial start
% creates a scrolling window for data
if(scroll_width > 0)
selindex = time > time(index) - scroll_width;
set(graph1,'XData', time(selindex), 'YData', x_data(selindex));
set(graph2,'XData', time(selindex), 'YData', y_data(selindex));
set(graph3,'XData', time(selindex), 'YData', z_data(selindex));
axis([time(index) - scroll_width time(index) min max]);
else
% initializes plot
set(graph1,'XData', time, 'YData', x_data);
set(graph2, 'XData', time, 'YData', y_data);
set(graph3, 'XData', time, 'YData', z_data);
axis([0 time(index) min max]);
end
% delay to allow data to process
pause(delay);
end
end
% close serial port
fclose(s);
% delete serial port to allow plotting for subsequent sessions
delete(s);