Einzelnen Beitrag anzeigen

hirnstroem

Registriert seit: 21. Sep 2005
297 Beiträge
 
Delphi 2006 Professional
 
#2

Re: String auf mehrere Teilstrings durchsuchen

  Alt 24. Jan 2007, 11:29
So scheint es zu funktionieren:

Delphi-Quellcode:
procedure TIWServerController.ComPortRxChar(Sender: TObject; Count: Integer);
var
  InputBuffer, InputString: String;
  i, StartPos, EndPos: Integer;
begin
  ComPort.ReadStr(InputBuffer, Count);
  Trim(InputBuffer); // deletes control characters
  if (Pos('T', InputBuffer) <> 0) and (Length(InputBuffer) > 10)then
  begin
    while Pos('T', InputBuffer) <> 0 do
    begin
      StartPos := Pos('T', InputBuffer) + 1;
      EndPos := PosEx('T', InputBuffer, StartPos) - 1;
      if EndPos <= 0 then
      begin
        InputString := Copy(InputBuffer, StartPos, 25); // no more messages - copy from T to the max. message length
        CANCommunication.WriteDataIntoBuffer(InputString);
        Exit;
      end
      else
      begin
        InputString := Copy(InputBuffer, StartPos, EndPos - StartPos); // copy the message to the InputString
        InputBuffer := Copy(InputBuffer, EndPos , Length(InputBuffer) - StartPos); // delete the message from InputBuffer
        CANCommunication.WriteDataIntoBuffer(InputString);
      end;
    end;
  end;
end;
inde deus abest
  Mit Zitat antworten Zitat