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!