I have a problem about receiving 16-bit data in MATLAB.I cannot receive 16-bit data at high speed through UART in MATLAB software.
Using stm32, I divide a 16-bit data belonging to a sensor into two 8-bit data and send it to MATLAB through UART. And in MATLAB software, I combine these two 8-bit data together and 16-bit sensor data is obtained. In MATLAB software, at first a valid data is sent to stm32. until this data is not sent to stm32, stm32 does not send data, This is so that MATLAB gets the data order right, but it only happens once. With the test I did, I realized that the process of separating and combining the data is done correctly.
But when I want to receive sensor data, only the first data is stored in each loop (2530) and for example if I receive 1000 data, 1000 of the first data are stored (2530).
I received the sent data of the stm32 using a serial plotter software on the computer and the data is sent to the computer correctly.
When I put a delay of 20 milliseconds in the stm32, the problem is solved and I think that the code I wrote for MATLAB is not optimal and maybe there is a better code for this task. And the delay of 20 milliseconds is too much for my work . I need to send the data to MATLAB as fast as possible and the data will be ploted live in MATLAB.
When I delete the plot command from MATLAB, the performance of the program improves, but the problem is not solved
What should I do to fix this problem?
clear
close all;
load('fi4.mat');
clc
serialportObj = serialport("COM3",115200);
i=2
tim=0;
data=0;
w=0;
data_valid=0;
validate=11;
write(serialportObj, validate, "uint8");
while(1)
tic
data=read(serialportObj,2,"uint8");
res = double(typecast([uint8(data(2)), uint8(data(1))], 'uint16'))
%FilterdSignal=filter(Hd, res);
sig(i)=res;
w(i)=sig(i)+(0.95*w(i-1));
ff(i)=w(i)-w(i-1);
if i <=300
figure(2)
plot (sig);
else
figure(2)
plot(sig(end-300:end));
end
i=i+1;
c=toc;
tim=c+tim;
end