Read data continuously from accelerometer through serial COM
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2021-6-29
编辑:Walter Roberson
2021-6-29
%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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 National Instruments Frame Grabbers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!