Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi Loop help 1

Status
Not open for further replies.

vz2487

Programmer
Sep 24, 2007
5
0
0
US
Hi all,
I am working on a data acquisition project which will send and receive data from and external device. The external device is a DAC. Any way I have been working on this program for a while now and I believe that I have everything in place but the program is not able to repeat a certain number of time, and it also freezes after its run. I was advised to look at the variable type used in the variable, but I am unable to find anything wrong. Here is the code the ULSTAT is an operation to obtain data from the DAC

Code:
procedure TfrmAIn1.CVRunClick(Sender: TObject);

var     initial, final, step, vout,sr, vertex    :Single;
        i, j, nop, scan_rate_time, r, nor         :Integer;
        DADataValue                    :Word;
        DAEngUnits                     :Single;
begin
r :=0;
   Series1.Clear;
   Series2.Clear;
   RChart1.ClearGraf;
   nor:= StrToInt(NumIO1.Text); //number of repeats
   repeat
   begin
  chan:=1; //channel data in
  //DAChan:=0;

  //set to connected to D/a
  ULStat := cbAOut (BoardNum, DAChan, DARange, 0);
   If ULStat <> 0 then exit;

 //If RampOutCheckBox1.Checked = True then

   initial:= StrToFloat(IPNumIO1.Text);
   vertex:=StrToFloat(VPNumIO1.Text);
   Step:= StrToFloat(StepNumIO1.Text);
   Final:= StrToFloat(FPNumIO1.Text);  //pot in mV
   nor:= StrToInt(NumIO1.Text); //number of repeats

i:=0;
   sr:= StrToFloat(SRNumIO1.Text); //scan rate in fp
   scan_rate_time:= round(step*1000/sr);    //delay time for ms

 repeat
  Wait(scan_rate_time); //in ms

  vout:=Initial+i*Step;  //voltage out from DAC
   DAEngUnits:= vout/1000;  //converted to volt

   //data output,stepping up
   ULStat := cbFromEngUnits(BoardNum, DARange, DAEngUnits, DADataValue);
   If ULStat <> 0 then exit;
   ULStat := cbAOut (BoardNum, DAChan, DARange, DADataValue);
   If ULStat <> 0 then exit;

	//collect data on ADC 1
        ULStat := cbAIn(BoardNum, Chan, Range, DataValue);
        If ULStat <> 0 then exit;
        ULStat := cbToEngUnits (BoardNum, Range, DataValue, EngUnits);
        If ULStat <> 0 then exit;
        

        x[i]:= i*scan_rate_time;             // just put index or i*100/1000 as time in s
        y[i]:= EngUnits*1000;          // signal in mV
              NumLab2.Value:=x[i];
        Series1.AddXY(x[i],y[i],'',clTeeColor); //Plot Data TeeChart

   i:=i+1;
 until vout>=vertex; // try this > sign

 //continue collecting data ,  dont restart, reverse voltage
  j:=1;
 repeat
  Wait(Scan_rate_time);

   vout:=Vertex-j*Step;
   DAEngUnits:= vout/1000;  //converted to volt
   //out data, stepping down
   ULStat := cbFromEngUnits(BoardNum, DARange, DAEngUnits, DADataValue);
   If ULStat <> 0 then exit;
   ULStat := cbAOut (BoardNum, DAChan, DARange, DADataValue);
   If ULStat <> 0 then exit;
  //collect data
  i:=i+1;
     ULStat := cbAIn(BoardNum, Chan, Range, DataValue);
        If ULStat <> 0 then exit;
        ULStat := cbToEngUnits (BoardNum, Range, DataValue, EngUnits);
        If ULStat <> 0 then exit;

        x[i]:= i*scan_rate_time;            // index - this can be converted later
        y[i]:= EngUnits*1000;          // signal in mV

      NumLab2.Value:=x[i];
  Series1.AddXY(x[i],y[i],'',clTeeColor); //Plot Data TeeChart

    j:=j+1;
  until vout<=Final; //try this < sign

    //PLOT data while collecting
 i:= nop; //total number of points collected
 for i:=1 to nop do
 begin
//Start RPLOT Routine
     RChart1.MoveTo(x[i],y[i]);

 with Rchart1 do
   begin
      AutoRange(4);
      //DataColor:=clAqua;
       MarkAt(x[i],y[i],13);  //+--1,7,10,13small+,27,28(rounds)-shape of mark
       Drawto(x[i],y[i]);
      ShowGraf;
   end;
 end;

     ULStat := cbAOut (BoardNum, DAChan, DARange, 0);
   If ULStat <> 0 then exit;
  exit;
  end;
  r := r+1;
until r >= nor;
end;
 
As a first step, it might be useful to add a few debugging lines (writeln compiled as a console mode app, or lines added to a TMemo control) within the loops to indicate passage (which loop is endless?) and to indicate what the loop control variables are doing.

Better yet, could this be done as a console mode app to debug it?
 
Just one observation,
The variable [blue]nop[/blue] I see is declared locally for this procedure and I do not see it being pulled from anywhere else.
However, right before your loop that uses [blue]nop[/blue] you assign [blue]i := nop[/blue].
Maybe that is backwards?
Then you do the for loop for i to nop.
It appears that you are doing a for loop to [blue]nop[/blue] but never actually assign anything to the variable.
That could be the cause of freeze.

Also, an insignificant observation, but still affects the program to the smallest (not noticeable probably) degree, at the beginning, you pull in [/blue]nor[/blue] twice from [blue]NumIO1.Text[/blue]. The second time is actually inside the [green]Repeat[/green] loop, which would indicate its being assigned every time you iterate through the loop. Since I cannot see its value being altered anywhere, perhaps assigning it inside of the loop is superfluous?

Hope that helps.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
IT WORKED. THANK YOU SOOOOOOOOOO MUCH.thank you. god bless you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top