Multithreading 如何在Matlab中连续读取串口数据?

Multithreading 如何在Matlab中连续读取串口数据?,multithreading,matlab,serial-port,Multithreading,Matlab,Serial Port,这是我的代码: serialPort = 'COM3'; s = serial(serialPort,'BaudRate',9600); if (s.Status == 'closed') s.BytesAvailableFcnMode = 'byte'; s.BytesAvailableFcnCount = 200; s.BytesAvailableFcn = @Serial_OnDataReceived; fopen(s); end

这是我的代码:

serialPort = 'COM3';
s = serial(serialPort,'BaudRate',9600);    
if (s.Status == 'closed')       
    s.BytesAvailableFcnMode = 'byte';
    s.BytesAvailableFcnCount = 200;
    s.BytesAvailableFcn = @Serial_OnDataReceived;

    fopen(s);
end
这是回调函数

function Serial_OnDataReceived(obj,event)
global s;
global i;
global endCheck;
global yVals;
global xVals;

if (s.Status == 'open')
    while s.BytesAvailable > 0 && endCheck ~= '1',
        data = str2num(fgetl(s));
        dlmwrite ('SensorsReading.csv', data, '-append');

        yVals = circshift(yVals,-1);
        yVals(end) = data(3);

        xVals = circshift(xVals,-1);
        i = i + 0.0125;
        xVals(end) = i;
    end

    figure(1);
    plot(xVals,yVals);

end    
结束

在执行
FOPEN
功能后,我立即收到以下警告:

BytesAvailableFcn正在被禁用。要启用回调属性 使用FOPEN连接到硬件或设置BytesAvailableFcn属性

回调函数
Serial\u OnDataReceived
中发生的逻辑是否在不同的线程上运行

是否有方法将参数传递给函数?我想从回调函数修改主脚本中的一个数组,它位于另一个文件中。最好的方法是什么?
我还尝试在更新值以显示某种动态动画时绘制这些值。

我不确定是什么产生了警告,但要回答您的另一个问题(如何将参数传递到回调以及如何更新绘图),更好的方法是在回调之外准备绘图,然后将句柄传递给回调,仅用于更新

nPointsInFigure = 10 ;  %// number of "sliding points" in your figure
step = 0.0125 ;         %// X points spacing
xVals = linspace(-(nPointsInFigure-1)*step,0,nPointsInFigure) ; %// prepare empty data for the plot
yVals = NaN(nPointsInFigure,1) ;

figure(1) ;
hp = plot( xVals , yVals ) ; %// Generate the plot (with empty data) it will be passed to the callback.

serialPort = 'COM3';
s = serial(serialPort,'BaudRate',9600);  
if (s.Status == 'closed')       
    s.BytesAvailableFcnMode = 'byte';
    s.BytesAvailableFcnCount = 200;
    s.BytesAvailableFcn = {@Serial_OnDataReceived,hp,step} ; %// note how the parameters are passed to the callback 
    fopen(s);
end
您的回拨将变成:

function Serial_OnDataReceived(obj,event,hp,step)

global endCheck; %// I don't know how you use that so I could not get rid of it.

xVals = get(hp,'XData') ; %// retrieve the X values from the plot
yVals = get(hp,'YData') ; %// retrieve the Y values from the plot

while obj.BytesAvailable > 0 && endCheck ~= '1',
    data = str2num(fgetl(s));
    dlmwrite ('SensorsReading.csv', data, '-append');

    yVals = circshift(yVals,-1);
    yVals(end) = data(3);

    xVals = circshift(xVals,-1);
    xVals(end) = xVals(end-1) + step ;
end

set( hp , 'XData',xVals , 'YData',yVals ) ; %// update the plot with the new values

还要注意:(1)您不需要在回调函数中检查串行端口的打开/关闭状态。如果事件已触发,则表示端口已打开。(2) 您不需要将
s
声明为全局,
obj
参数实际上是串行对象本身。

如果Hoki的建议不起作用,请尝试使用计时器

% The following line is useful for debugging 
mytimerCallback = @(~, ~) disp('Caught error For Tenzo Timer');

if isempty(timerXbee)
     timerXbee = timer('ExecutionMode','FixedRate','Period',0.1,...
                    'TimerFcn',{@storeDataFromSerial},'ErrorFcn', mytimerCallback);
     try
         start(timerXbee);  
     catch
         disp('Timer Xbee');
         disp '******** InstrumentSubscription ERROR *********'
         disp (exception.message);
         disp '***********************************************'
     end
end

% If you are using a polling approach, request data

sendRequest(cmd);
connectionRequested = true;
然后实现将在每个时段调用的计时器功能

function storeDataFromSerial(~,~,~)
    while (get(xbee, 'BytesAvailable')~=0)
        serialProtocol();                
    end
end  
当您想要断开设备并关闭通信时,不要忘记使用停止计时器

stop(timerXbee)

Matlab是单线程的。我想您的回调函数有问题。参数或类似参数数量错误,能否提供代码?^已添加。还添加了一个绘图问题我无法运行您的代码,因为我没有任何可能设置串行端口,但我确实确信您的函数会导致错误。当状态为“关闭”时,至少
s.Status==“打开”
抛出维度不匹配。使用strcmpi。对于所有的全球VAR,很难说是否还有更多的问题。尝试设置断点并调试回调函数。