how to stop data being skipped when recording over long intervals?

2 次查看(过去 30 天)
Hi, I'm using an arduino to log the temperature from ten PT100s in real time. My code works fine over short intervals (e.g. 10 minutes), but over longer intervals (e.g. 2-7 hours), specifically on the 63rd temperature logged, Matlab doesn't pick up the full string from the serial print once every 4 pieces of data. It doesn't matter what sample rate i set it at (I have tried 2.5 mins and 5 mins), it always stops working correctly at the 63rd piece of data. (The output length mismatch error has been added so that the logger won't fail when it fails to pick up the full serial print from the arduino)
Capture1.PNG
Capture.PNG
Does anyone have any idea why this might be happening? My code is included below
clear all;
clc;
delete(instrfindall); %pre-emptively close all ports
b = serial('COM7','BaudRate',1000000); % COM is the COM port of the Arduino
%flush input buffer
flushinput(b);
fopen(b); % initiate arduino communication
%% Create a figure window to monitor the live data
Tmax = 24*60*60; % Total time for data collection (s)
figure,
grid on,
xlabel ('Time (s)'), ylabel('Tempeature (K)'),
%axis([0 Tmax+1]),
%% Read and plot the data from Arduino
Ts = 150; % Sampling time (s)
ii = 0;
dataa = 0;
datab = 0;
t = 0;
tic % Start timer
mData = [];
while toc <= Tmax
ii = ii + 1;
%% Read buffer data
datab = fscanf(b);
y = strsplit(datab, ',')
if(length(str2double(y))<10)
disp("Output length mismatch");
y = NaN(10,1);
t1(ii,1) = str2double(y(1));
t2(ii,1) = str2double(y(2));
t3(ii,1) = str2double(y(3));
t4(ii,1) = str2double(y(4));
t5(ii,1) = str2double(y(5));
t6(ii,1) = str2double(y(6));
t7(ii,1) = str2double(y(7));
t8(ii,1) = str2double(y(8));
t9(ii,1) = str2double(y(9));
t10(ii,1) = str2double(y(10));
else
t1(ii,1) = str2double(y(1));
t2(ii,1) = str2double(y(2));
t3(ii,1) = str2double(y(3));
t4(ii,1) = str2double(y(4));
t5(ii,1) = str2double(y(5));
t6(ii,1) = str2double(y(6));
t7(ii,1) = str2double(y(7));
t8(ii,1) = str2double(y(8));
t9(ii,1) = str2double(y(9));
t10(ii,1) = str2double(y(10));
%sTemp = smooth(data,25);
%% Read time stamp
% If reading faster than sampling rate, force sampling time.
% If reading slower than sampling rate, nothing can be done. Consider
% decreasing the set sampling time Ts
t(ii) = toc;
if ii > 1
T = toc - t(ii-1);
while T < Ts
T = toc - t(ii-1);
end
end
t(ii) = toc;
grid on
grid minor
%% Plot live data
if ii > 1
x = [t(ii-1) t(ii)];
%the two values output by the arduino when the sensor is not
%attached are 38.98 and 1261.79 - changed these values to 0 using
%logic
y1 = [(t1(ii-1)*(t1(ii-1)~=30.98)*(t1(ii-1)~=1261.79)) (t1(ii)*(t1(ii)~=30.98)*(t1(ii)~=1261.79))];
y2 = [(t2(ii-1)*(t2(ii-1)~=30.98)*(t2(ii-1)~=1261.79)) (t2(ii)*(t2(ii)~=30.98)*(t2(ii)~=1261.79))];
y3 = [(t3(ii-1)*(t3(ii-1)~=30.98)*(t3(ii-1)~=1261.79)) (t3(ii)*(t3(ii)~=30.98)*(t3(ii)~=1261.79))];
y4 = [(t4(ii-1)*(t4(ii-1)~=30.98)*(t4(ii-1)~=1261.79)) (t4(ii)*(t4(ii)~=30.98)*(t4(ii)~=1261.79))];
y5 = [(t5(ii-1)*(t5(ii-1)~=30.98)*(t5(ii-1)~=1261.79)) (t5(ii)*(t5(ii)~=30.98)*(t5(ii)~=1261.79))];
y6 = [(t6(ii-1)*(t6(ii-1)~=30.98)*(t6(ii-1)~=1261.79)) (t6(ii)*(t6(ii)~=30.98)*(t6(ii)~=1261.79))];
y7 = [(t7(ii-1)*(t7(ii-1)~=30.98)*(t7(ii-1)~=1261.79)) (t7(ii)*(t7(ii)~=30.98)*(t7(ii)~=1261.79))];
y8 = [(t8(ii-1)*(t8(ii-1)~=30.98)*(t8(ii-1)~=1261.79)) (t8(ii)*(t8(ii)~=30.98)*(t8(ii)~=1261.79))];
y9 = [(t9(ii-1)*(t9(ii-1)~=30.98)*(t9(ii-1)~=1261.79)) (t9(ii)*(t9(ii)~=30.98)*(t9(ii)~=1261.79))];
y10 = [(t10(ii-1)*(t10(ii-1)~=30.98)*(t10(ii-1)~=1261.79)) (t10(ii)*(t10(ii)~=30.98)*(t10(ii)~=1261.79))];
%replace zero values with nan so they will not be plotted
y1(y1==0) = nan;
y2(y2==0) = nan;
y3(y3==0) = nan;
y4(y4==0) = nan;
y5(y5==0) = nan;
y6(y6==0) = nan;
y7(y7==0) = nan;
y8(y8==0) = nan;
y9(y9==0) = nan;
y10(y10==0) = nan;
line(x, y1, 'Color', 'red')
line(x, y2, 'Color', 'blue')
line(x, y3, 'Color', 'black')
line(x, y4, 'Color', 'magenta')
line(x, y5, 'Color', 'green')
line(x, y6, 'Color', 'blue')
line(x, y7, 'Color', 'red')
line(x, y8, 'Color', 'cyan')
line(x, y9, 'Color', 'black')
line(x, y10, 'Color', 'magenta')
legend ('T1','T2','T3','T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10')
drawnow
end
end
end
fclose(b);

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by