serial communication array storing problem

Thread Starter

yef smith

Joined Aug 2, 2020
235
Hello I have made the following code to plot the recieved data from serial comm
However at some point (different each time) it throws the index exceeds array size.
How its possible

Code:
clear all
sObject=serial('COM4','BaudRate',115200,'TimeOut',10,'Terminator','LF');
fopen(sObject);
i=1;

while(1)
   data=fscanf(sObject);
 
    plot(total);
    pause(0.05);
    total(i)=double(uint8(data(1)))+double(uint8(data(2)))+double(uint8(data(3)));
   i=i+1;
  
   flushinput(sObject);
end
fclose(sObject);
 

Thread Starter

yef smith

Joined Aug 2, 2020
235
Yes I increments inside endless loop.why its problem? Why it says exceed dimention error? the size of the array grows by one ,its not suppose to exceed.even when i made arra size 1000 at i=48 it threw this error.
total=zeros(1000)
 

BobaMosfet

Joined Jul 1, 2009
1,767
Yes I increments inside endless loop.why its problem? Why it says exceed dimention error? the size of the array grows by one ,its not suppose to exceed.even when i made arra size 1000 at i=48 it threw this error.
total=zeros(1000)
This is 'C'. Arrays don't dynamically grow. You are expected to create an array of a given size, and then work within that array. Welcome to a real computer language where you have to do everything.

The real way you solve this problem is you have one piece of code putting data into a circular array (called a 'queue') with a head & tail pointer, and you have a faster routine reading data out of it so the tail never meets the head. If it does, you signal to stop receiving until you clear some data out of the queue, and then re-signal to start sending again.

Beyond that, as others have stated- you need to learn how to declare and define variables and so forth. You have to do everything in C.

Get a basic book on C Programming (or find it on the web) and check these out:

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6

Title: Algorithms in C, 3rd Ed. [Parts 1-4, Fundamentals, Data Structures, Sorting, Searching]
Author: Robert Sedgewick
ISBN: 0-201-31452-5
 

John P

Joined Oct 14, 2008
1,897
Either this isn't C and I'm not understanding it, or it's very badly written C and that's why I'm not understanding it.
 
Top