Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 22nd, 2004, 07:13 PM
lloydie-t lloydie-t is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Location: UK
Posts: 523 lloydie-t User rank is Private First Class (20 - 50 Reputation Level)lloydie-t User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 17 h 3 m 13 sec
Reputation Power: 7
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;

Reply With Quote
  #2  
Old January 23rd, 2004, 12:07 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Click here for more information.
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,713 Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 11 h 21 m 11 sec
Reputation Power: 1179
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

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

Reply With Quote
  #3  
Old January 23rd, 2004, 09:04 AM
lloydie-t lloydie-t is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Location: UK
Posts: 523 lloydie-t User rank is Private First Class (20 - 50 Reputation Level)lloydie-t User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 17 h 3 m 13 sec
Reputation Power: 7
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?

Reply With Quote
  #4  
Old January 24th, 2004, 06:27 AM
lloydie-t lloydie-t is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Location: UK
Posts: 523 lloydie-t User rank is Private First Class (20 - 50 Reputation Level)lloydie-t User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 17 h 3 m 13 sec
Reputation Power: 7
Turns out I should of been detecting LF instaed of CR. Thanks for your help though.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > More problems with for loops


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT