How to log data from COM port without timestamp jitter?

1 view (last 30 days)
I am trying to log data from a NI USB-6008 as well as from a device sending data to a COM port. I am using the data acquisition toolbox. The USB-6008 is recording at 1000 Hz and the COM port device sends data across RS-232 at a rate of ~15Hz. I can log the data from both sources, however only the NI-DAQ data is accurately time stamped. There is significant jitter in the recorded timestamps for the COM port device. I have attached below the time stamps and the time gaps between them (they should be consistent at around 0.064s, we know the device is sending data at this rate). The recording is from 14:24:53 to 14:24:54.
142453.329 0.067
142453.416 0.087
142453.466 0.05
142453.539 0.073
142453.59 0.051
142453.665 0.075
142453.796 0.131
142453.812 0.016
142453.844 0.032
142453.918 0.074
142453.98 0.062
142454.053 0.073
142454.106 0.053
142454.168 0.062
142454.23 0.062
The code I am using is:
listener = addlistener(s,'DataAvailable',@LogData);
s.Rate = 1000;
s.IsContinuous = true;
s.NotifyWhenDataAvailableExceeds = 1000;
numberOfReadings = 10000;
numberOfBytes = 16; % Size of data packet
ser = serial('COM4'); % Create serial input and specify COM port
set(ser,'BaudRate',19200,'Parity','none'); % Serial input settings
fopen(ser); % Open COM port
out = zeros(numberOfReadings,numberOfBytes); % Create output array
s.startBackground();
i = 0;
while s.IsRunning
i = i + 1;
for byte = 1:numberOfBytes
out(i,byte) = fread(ser,1,'uint8');
end
[y,m,d,h,mi,sec] = datevec(now);
hms = h*10000+mi*100+sec;
out(i,1) = hms;
dlmwrite('uds.csv',[out(i,:)],'delimiter',',','precision', 16,'-append');
pause(0.001);
end
end
With an @LogData file:
[y,m,d,h,mi,s] = datevec(now);
hms = h*10000+mi*100+s;
t1Array = event.TimeStamps*0 + hms;
dlmwrite('test.csv',[t1Array,event.TimeStamps,event.Data],'delimiter',',','precision', 16,'-append');

Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!