How to read serial data faster

63 Ansichten (letzte 30 Tage)
Seth Heerschap
Seth Heerschap am 28 Apr. 2022
Kommentiert: Walter Roberson am 23 Mär. 2024
I want to read streaming data from an arduino at 10kHz or as fast as is outputted by the microcontroller. Unfortunately I'm running into two problems when using the "readline()" function, firstly, it does not read data fast enough, secondly, it skips data. Here is my code reading out of COM6:
Obj = serialport('COM6',115200);
configureTerminator(Obj,'CR/LF');
flush(Obj);
j=50000; %Block size to read
data=zeros(j:2); %Create array for data
tic
start1=tic; %Starts the clock to time stamp the current iteration.
for n=1:j
s = str2double(readline(Obj)); %Read data and save as "s" var
data(n,1)=s; %Insert data into array
data(n,2)=toc; %Time stamp of data
end
toc
plot(data(:,2),data(:,1),'r-o');
RATE1=j/toc
delete(Obj)
To test data loss I used a 100Hz sine wave generator into the microcontroller and outputted the data as serial. MATLAB reads and plots the following:
This should be a nice sinusoidal pattern but it isn't and there seems to be data lost. Additionally, this only works when the data output from the microcontroller is slowed to about 4kHz, anything faster creates problems, for example, if I output data from the microcontroller at 10kHz I get an average of 1kHz data capture by MATLAB's readline() function:
I'm obviously doing something wrong if when the data output from the microcontroller increases, the MATLAB performance decreases. Running the serial data from the arduino IDE via the serial plotter I can observe good data capture without any loss for both the 4kHz output and 10kHz output. Here's the faster data output showing a 10kHz (as I measured) rate:
I figured since the Arduino serial monitor can recieve the data correctly and at faster rates, MATLAB should also be able to. What am I doing wrong? Are there any other serial read functions I can try?
Ultamately, I want to be able to continuously read millions of data poitns over several hours.
Thanks!

Antworten (1)

Tushar
Tushar am 25 Sep. 2023
Hi Seth,
I understand that you are trying to read streaming data from an arduino, and you are encountering issues with the limitations of 'readline' function.
You can try out these alternative methods:
  • 'fread' - Read data from a binary file. You can specify the number of bytes to read so that MATLAB can efficiently read a predefined amount of data at once. It can provide significant performance boost than reading line-by-line using 'readline'.
  • 'fscanf' - Read data from a text file. It allows you to specify the expected format of the data, making it faster than 'readline'. It requires the knowledge of the data format (e.g. comma-separated values).
For more information on 'fread' and 'fscanf' respectively, refer to the below documentation:
  1. https://www.mathworks.com/help/matlab/ref/fread.html
  2. https://www.mathworks.com/help/matlab/ref/fscanf.html
Hope this helps!

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware 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!

Translated by