Re: wait for replay



Winbug wrote:

The problem is wich strategy to choose. There is one procedure
initiating the loop, by a button. Another function is processing
replys from the serial component. Somehow, the procedure have to be
notified when the replay arrives and the loop can continue.

Sounds very much like usual serial line communication protocol.

You did not mention which serial component you are using. It's years and
years back when I wrote some routines to control some serial device and
I used TApdComport component that comes in free Asynch Pro package.

You simply keep sending you strings to the line, and with each type of
line set a trigger byte for ApdComport component to wait. After any of
those triggers fires collect the appropriate data that come from the
line.

For instance my serial device sent back ACK, ETX, STX etc bytes, and
after that came the data. Here are some rip off snippets from my
original code, and there's no quarantee any of this will work as is.

const
STX = Chr($02);
ETX = Chr($03);
ACK = Chr($06);
NAK = Chr($15);

Var
FInputSt : String;
FHasGotETX : Boolean;
FHasGotTrigger2 : Boolean;


procedure TForm1.SendStringToModem(S: String);
// Send some string to the line and set a Trigger that
// the ApdComport component must watch.
begin
FInputSt := '';
FHasGotETX := False;
FTriggerETX := ApdComPort1.AddDataTrigger(ETX, False);
ApdComPort1.OutPut := S;
end;

procedure TForm1.ApdComPort1TriggerData(CP: TObject;
TriggerHandle: Word);
// This is 'OnTrgiggerData' Event oin TApdComport component.
// Watch what triggers are coming as answers from the line.
begin
// This first trigger was waiting for ETX = Chr($03) character
// from the line, and now it found one.
if (TriggerHandle = FTriggerETX)
then FHasGotETX := True
else if (TriggerHandle = FTrigger2)
then FHasGotTrigger2 := True;
etc...
end;

procedure TForm1.ApdComPort1TriggerAvail(CP: TObject; Count: Word);
// This is 'OnTrgiggerAvail' Event oin TApdComport component.
// This is the place to collect the data that has come from
// the line aftersome trigger has been fired.
var
i : Word;
begin
for i := 1 to Count do
begin
if FHasGotETX // These are chars that came after ETX trigger was
got
then
FInputSt := FInputSt + ApdComPort1.GetChar
else if FHasGotTrigger2 // These are chars that came after Trigger2
was got
FInputSt2 := FInputSt + ApdComPort1.GetChar
end;
end;

---
There are more examples in the TApdComport documentation. Yet my snippet
may be easier to get the basic idea what the component actually can do,
and how to get the answers and data collected:)
-ham
.


Quantcast