1

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


yaser kh
  • 11
  • 2
  • "*What should I do to fix this problem?*" -- Binary data should not be sent raw (without a packet frame) nor without an integrity check (e.g. a checksum or CRC). Consider appending a third byte to your packet, and use a scheme similar to https://stackoverflow.com/questions/16177947/identification-of-packets-in-a-byte-stream/16180135#16180135 – sawdust Nov 01 '22 at 08:26
  • 1
    Don’t plot within the loop that reads data. Plotting takes a lot of time. Read all your data, then plot it. And pre-allocate `sig`, growing an array in this way is also slow. – Cris Luengo Nov 01 '22 at 13:35
  • 1
    also for a (near) real time plot update, do not use the high level `plot()` function. Rather, use `hp=plot(...)` the first time outside of the loop, then for each iteration only update the `XData` and `YData` of the plot object `hp`. Also, if you're receiving samples very fast, I would not necessarily update the graph at each iteration (each new sample), you can collect a number of them and then update the plot in batch. – Hoki Nov 01 '22 at 15:22
  • For your 20ms delay problem, instead of trying to _pull_ the data from MATLAB by reading the serial port incessantly, set a comfortable receive buffer length, then trigger the MATLAB side by using the `ByteAvalaibleFcn`. This way you limit the need for perfect synchronisation between MATLAB and your stm32. MATLAB will only try to read data when new data are present in the buffer. – Hoki Nov 01 '22 at 15:36
  • Take a look at this code: https://www.mathworks.com/matlabcentral/fileexchange/31958-serialdatastream – mhopeng Nov 02 '22 at 06:12

0 Answers0