#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13

    More problems with for loops


    I am having a problem getting my loops to work correctly. My application monitors a comm port and when a carriage return is detected (#13) it then matches the contents(ReceiveString) of that line against a MySQL database and then waits for CR again. When the next CR is presented the line is completely blank? Where do I blank the contents (ReceiveString) so it does not affect the next line and where (if I have to) continue the for loop?
    Please find minimised version of code.

    Code:
     procedure TMainForm.CollectReceivedString;
    var
    i : integer;
    areacode : string;
    stdsearch : Pchar;
    QueryCharge : Pchar;
    CallInsert : Pchar;
    
    begin
        LB_query.Lines.Clear;
        Edit1.Clear; // seems to stop here on second CR
    With CollexQuery do
      Begin
      Hostname:='localhost';
      UserName:='user';
      Password:='pass';
      Connect;
      Utility.PrepareSelectDatabase('itouch','Use Database');
      Execute;
      end;
    
         for i := 9 downto 1 do
         //Check dialled number is a match with DB
         //knocking one digit of rightside until a match
         begin
         CollexQuery.Query.Reset;
         areacode := copy(dest,0,i);
         stdsearch := PChar('Select * FROM stdcodes WHERE areacode = '''+areacode+'''');
         CollexQuery.Query.Prepare(stdsearch, 'select');
         CollexQuery.Execute;
    
    //if there is a match, run calculations
    if  CollexQuery.Query.DataCount > 0 then
    Begin
    //add returned results to richedit 
    LB_query.Lines.Add(CollexQuery.Query.Data(0,0)+': '+CollexQuery.Query.Data(0,1));
    break;
    end;
    CollexQuery.Query.Reset;
    end;
    end;
    
    
    
    //look for CR
    
    procedure TMainForm.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
      Received: Cardinal);
      var i:integer;
    begin
       for i:=0 to Received-1 do
        if PChar(Buffer)[i]=EolnChar
          then begin
            //CR detected .... do something
            CollectReceivedString;
            //ReceiveString:='';
            continue;
          end
           //else accumulate string ...
           else ReceiveString:=ReceiveString+PChar(Buffer)[i];
    
    end;
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Like this perhaps?
    Code:
    procedure TMainForm.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
      Received: Cardinal);
      var i:integer;
    begin
       ReceiveString := '';
       for i:=0 to Received-1 do
        if PChar(Buffer)[i]=EolnChar
          then begin
            //CR detected .... do something
            CollectReceivedString;
            ReceiveString := ''; // Reset received string
          end
           //else accumulate string ...
           else ReceiveString:=ReceiveString+PChar(Buffer)[i];
    
    end;
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    Hi Scorpions,
    Just to upset every I have changed the code so that there is some threading in it (picked up along the way), But I am getting the same problem.

    The first line is processed properly. The next line is not. It seems as though an additional empty line is included when I print to a richedit.
    The information I recieve on the com port is
    A201 --> N01 23/01/04 12:57 00:00:05 0 ST I 07966323260...............
    A201 --> N01 23/01/04 12:58 00:00:11 0 ST I 0800282212................

    The first line is present to the sql query as:
    --------------------------------------------------
    A201 --> N01 23/01/04 12:57 00:00:05 0 ST I 07966323260...............

    The next line is presented as:
    --------------------------------------------------
    //note the additional line
    A201 --> N01 23/01/04 12:58 00:00:11 0 ST I 0800282212................


    All other lines are presented the same way afterwards. It seems as though CR is added to the beginning of the line to be processed.
    How can I get rid of the additional line?

    This is the code affecting it.

    Code:
    procedure TMainForm.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
      Received: Cardinal);
    var
      i : integer;
    begin
      bFillingBuffer := True;
      for i := 0 to Received-1 do
      begin
        if iCharCount >= sizeof(LargeBuffer) then
          // does nothing. buffer full. loose of received chars
        else
        begin
          // there are space left in buffer
          LargeBuffer[iTail] := PChar(Buffer)[i]; // save char in buffer
          Inc(iTail);                             // move pointer forward
          Inc(iCharCount);                        // count char in buffer
          if iTail = sizeof(LargeBuffer) then iTail := 0; // WRAP
        end;
      end;
      bFillingBuffer := False;
    end;
    
    procedure TMainForm.Timer1Timer(Sender: TObject);
    var
      I, N : Integer;
      C : Char;
    begin
      if iCharCount <= 0 then Exit; // nothing to do
      while bFillingBuffer do ; // wait if processing receive buffer (synchronize)
      N := iCharCount;
      for I := 0 to N-1 do
      begin
        C := LargeBuffer[iHead]; // peak a buffered char
        Inc(iHead);              // move pointer forward
        if iHead = sizeof(LargeBuffer) then iHead := 0; // WRAP
        if C <> #13 then
          sSearch := sSearch + C // concatenated the character
        else
        begin
          Richedit1.Lines.Add(sSearch);
          ProcessSearch(sSearch); // create a procedure to do the database task
                                  // keep in mind the time wasted there
          sSearch := ''; // clear for next string
        end;
      end;
      while bFillingBuffer do ; // wait if processing receive buffer (synchronize)
      iCharCount := iCharCount - N; // free space in buffer
    end;
    WhaDaYaTink?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Location
    UK
    Posts
    563
    Rep Power
    13
    Turns out I should of been detecting LF instaed of CR. Thanks for your help though.

IMN logo majestic logo threadwatch logo seochat tools logo