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 repitition problem

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. It use to freeze every time it would run but i have fixed that. Here is the code the ULSTAT is an operation to obtain data from the DAC (Data acquisition card)

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
 nop:= i; //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;

yes i have made a very similar post but i closed it by accident. i didn't realize that i did not solve the repeating process.
 
Two things (again =D ).
The first is that, just by slapping all that code into my code editor, it appears the the term [blue]final[/blue] is a reserved word. You are using it as a Single there and as a loop limiter. It might depend on your version of Delphi. I am using the BDS 2006. But that would probably be worth checking out. If it is a reserved word, that would explain why it isn't looping properly.

Thing 2, you are still assigning [blue]nor[/blue] both before your first Repeat loop and inside of it. Yet you never change its value otherwise. You could remove the assignment of [blue]nor[/blue] from inside the Repeat loop to save tiny amounts of processing time. =)

~
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.
 
thanks for your reply. I am using BD pro 6, thus i believe that final is not a reserved word. but anyway I have changed final to "finalp" and i have commented out the second nor statement. But the process i would like to repeat is still not repeating. I have looked through the code a few times but i could not find what's wrong.
 
i find the code very hard to read.
i see repeats inside repeats.
if you can, try breaking your code down into seperate procedures.

Aaron
 
alright I have fixed it. It was the last exit. that was stopping it from repeating the loop. but one PROLEM

the graph graphs the data on top of the existing graph. thus i canont see that another set of data have been plotted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top