【问题标题】:Use serial port for real time signal logging in Matlab在 Matlab 中使用串行端口进行实时信号记录
【发布时间】:2014-08-30 12:29:46
【问题描述】:

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

S-function 'rtwindi' 在 'rt_test/Digital Input' 中报告的错误: “标准设备串行端口”板没有数字输入通道

【问题讨论】:

    标签: matlab real-time


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多