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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with loops

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
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;
---------------------------------------------------------
 
I don't know what component you're using for the comport, but I would advise to use Turbopower's APRO components. since jan '03 these tools became open source (so no expensive $$$ to pay!!!). here's the link :
with their comport component, detecting something like a CR, or whatever, is so easy that you won't return to any other component...



 
Tried to install APRO but D6 would not install as I got the following error
[Fatal Error] AfRegister.pas(51): File not found: 'DsgnIntf.dcu'

if I remove the AfRegister from the package, the package does not install properly. Any Ideas?
 
That's to be replaced with a different dcu on D6, I can't recall right now, and don't have D6 over here, but search for 'desig' in your D6 tree to find it (under {pf}\Borland\Delphi 6).

HTH
TonHu
 
looks like you tried to install a wrong package. just compile these 2 packages under the packages directory :

First the runtime packae : A406_R60.dpk
and then the design time package : A406_D60.dpk

that should do the trick

 
None of those packages are included in the zip file I downloaded.
 
How many times you collect the received string?

It looks like every time you send all the basic information (user, password etc..) I should make the initialization 1 time.


Steven van Els
SAvanEls@cq-link.sr
 
The problem I am having with my origanl problem seems to be with the package I am using insert additional characters, so I am going to try Apro. Can you point me in the right direction in using Apro to capture strings from a commport which are terminated by CR(#13).
 
this is an easy one :

drop a comport component on your form (point it to the right comport,baudrate,etc...)

-in your startup/init code add a trigger for CR
CRTrigger:=Comport.AddDataTrigger(CR,True);

-define a global string received_data and
make an Ontriggeravail event :


procedure TFrm_main.ComportTriggerAvail(CP: TObject; Count: Word);
var I : Word;
c : char;
s : string;
begin
s:='';
for I := 1 To Count do
begin
c:=Comport.GetChar;
s := s + c;
end;
Received_Data:=Received_Data+s;
end;


- define an OnTriggerData event :

procedure TFrm_main.ComportTriggerData(CP: TObject; TriggerHandle: Word);
begin
if Triggerhandle = CRtrigger then
begin
process_received_data;
received_data:='';
end;
end;



this is as easy as it gets, for more info make sure you read the help file of APRO which will give good explanation about the various components...


 
Sorry to bite the hand that feed me, there is an easier recommended way.
Drop a TApdDataPacket component on the form
set it's start_cond to any string
set it's end_cond to eCstring
set the edn_string to #13

double click on the Onstring Packet event and enter what is required in the resulting procedure.

code-----------------------------------------
procedure TMainForm.ApdDataPacket1StringPacket(Sender: TObject;
Data: String);
begin
sSearch := Data;
IncomingDataEdit.Lines.Add(Data);
ProcessSearch(sSearch);
sSearch := '';
end;
-------------------------------------------

This was the way recommended in the manual, but it all seems to easy to me, although it's working. I would be interested to see how it works understress.
Anyhow thanks for the recommendation. I was just about to buy the other comms component.

I would be grateful if someone could point me in the right direction for changing the comport and confuring the comport.
 
I know that datapackets are easier to use, bit I'm using APRO for a long time now and one thin I learned, is not to use Datapackets (they have a buggy history). changing comport is not difficult :

Comport1.open:=false;
DelayTicks(18,false);// give comport threads time to close
try
Comport1.Comnumber:=2; // change to com2
Comport1.Baud:=38400; //change baudrate to 38400
Comport1.Parity:=Pnone; // change parity
....
Comport1.Open:=True; //open the comport
except
on E : exception do
showmessage(E.message); //exception if something went wrong
end;

greetings,

 
Is there not some sort of open.dialog form which can be used or use the windows comport setting?
 
no, at least not a complete one, there's only one small dialog that will allow you to change the comport (number). creating a small dialog with all the settings you want to allow to change shouldn't be to difficult though :))

[yawn]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top