Read data continuously from accelerometer through serial COM
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
i would like to read continuos data from an accelerometer.Before i was using hterm but it does not support long recording.
I was wondering if i can do that with matlab
baud 8200000 (13byes=1+4+2+2+2+2)
Can anyone help me with this?
Thanks a lot in advance!
6 Kommentare
Francesco Lucarelli
am 29 Jun. 2021
Bearbeitet: Francesco Lucarelli
am 29 Jun. 2021
Walter Roberson
am 29 Jun. 2021
Bearbeitet: Walter Roberson
am 29 Jun. 2021
%read a nominal second's worth of data at a time
%in this version, store it all at first -- it should be about 600 megabytes
blocksize = 13; %bytes
test_duration = 30*60; %seconds
target_sps = 26000;
bytes_per_second = target_sps * blocksize;
buffer = zeros(bytes_per_second, test_duration, 'uint8');
device = serialport("COM3",8200000);
full_cols = 0; partial = 0;
start_time = tic;
for K = 1 : test_duration
try
thisbuffer = read(device, bytes_per_second, 'uint8');
blen = length(thisbuffer);
buffer(1:blen,K) = thisbuffer;
if blen == bytes_per_second
full_cols = K;
partial = 0;
else
full_cols = K - 1;
partial = blen;
break;
end
catch ME
full_cols = K - 1;
partial = 0;
break
end
end
read_time = toc(start_time);
clear device
bytes_received = full_cols * bytes_per_second + partial;
bytes_per_second = bytes_received / read_time;
samples_per_second = bytes_received / blocksize / read_time;
fprintf('Actual throughput: %.3f bytes/second, %.3f samples/second\n', bytes_per_second, samples_per_second);
[fid, msg] = fopen('testlog.bin', 'w');
if fid < 0
fprintf('Could not open output file because: "%s"\n', msg);
fprintf('Data is still in buffer(1:%d)\n', bytes_received);
else
fwrite(fid, buffer(1:bytes_received), '*uint8');
fclose(fid);
end
Start with a shorter test.
If the logic works and the throughput is high enough, clone the function and rearrange slightly to fwrite() thisbuffer to the file as you go, and see whether the throughput drops more than you are willing to live with.
The more data you can record in memory before writing to disk, the more efficient writing will be -- but the more risk that everything will get lost if something goes wrong. For example, this code has no STOP logic in it, and if you control-C then it will not write to disk (but buffer will still exist). It would be more robust to write results to disk as you go, but you need to accumulate enough to be worth writing.
Generally speaking, operating systems need to be fed at least 4096 bytes to write efficiently, preferably 16K at least, 64 K is better.
Antworten (0)
Siehe auch
Kategorien
Mehr zu National Instruments Frame Grabbers finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!