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');
s = str2double(readline(Obj));
plot(data(:,2),data(:,1),'r-o');
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!