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)=EolnChar
then begin
//CR detected .... do something
CollectReceivedString;
//ReceiveString:='';
continue;
end
//else accumulate string ...
else ReceiveString:=ReceiveString+PChar(Buffer);
end;
---------------------------------------------------------
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)=EolnChar
then begin
//CR detected .... do something
CollectReceivedString;
//ReceiveString:='';
continue;
end
//else accumulate string ...
else ReceiveString:=ReceiveString+PChar(Buffer);
end;
---------------------------------------------------------