Recent

Author Topic: Timer in Linux anything special  (Read 1160 times)

coradi

  • Full Member
  • ***
  • Posts: 185
Timer in Linux anything special
« on: August 15, 2025, 12:39:05 am »
In this code, the debug Breakpoint in timer, never increase, after click on button?!
Code: [Select]

Unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    ButtonStart: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure ButtonStartClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
    FSerialFile: Text;
    FPortOpen: Boolean;
    FDevice: string;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

function MakePrintable(const S: string): string;
var i: Integer;
begin
  Result := '';
  for i := 1 to Length(S) do
    if S[i] in [#32..#126, #10, #13] then
      Result := Result + S[i]
    else
      Result := Result + '.'; // ersetzt ungültige Bytes
end;

function ToHex(const S: string): string;
var i: Integer;
begin
  Result := '';
  for i := 1 to Length(S) do
    Result := Result + IntToHex(Ord(S[i]), 2) + ' ';
end;

procedure TForm1.ButtonStartClick(Sender: TObject);
begin
   Memo1.Lines.Add('Hallo Welt');
   label1.Caption:='test';
   ButtonStart.caption:='test';

   sleep(500);
  FDevice := '/dev/ttyUSB0';

  // Seriellen Port öffnen
  AssignFile(FSerialFile, FDevice);
  {$I-}
  Reset(FSerialFile);
  {$I+}
  if IOResult <> 0 then
  begin
    ShowMessage('Fehler: Konnte ' + FDevice + ' nicht öffnen.');
    Exit;
  end;

  FPortOpen := True;
  Timer1.Interval := 100;  // 100 ms Intervall
  Timer1.Enabled := True;

  Memo1.Lines.Add('Port ' + FDevice + ' geöffnet, lese kontinuierlich...');
end;



procedure TForm1.Timer1Timer(Sender: TObject);
var
  Line: string;
begin
    ButtonStart.caption:='123';
  if FPortOpen then
  begin
    {$I-}
        ButtonStart.caption:='123';
    while not Eof(FSerialFile) do
    begin

     Readln(FSerialFile, Line);
    line:='123';
     Memo1.Lines.Add(tohex(Line)); // oder ToHex(Line)
      Memo1.Lines.Add(Line);
      Memo1.SelStart := Memo1.GetTextLen;  // automatisch nach unten scrollen
    end;
    {$I+}
    if IOResult <> 0 then
    begin
      Memo1.Lines.Add('Fehler beim Lesen.');
      Timer1.Enabled := False;
      FPortOpen := False;
      CloseFile(FSerialFile);
    end;
  end;
end;

end.
                 
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

dbannon

  • Hero Member
  • *****
  • Posts: 3678
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Timer in Linux anything special
« Reply #1 on: August 15, 2025, 01:43:41 am »
coradi, are you saying that a breakpoint set at the top of Timer1Timer() is never reached ?

If you are in debugmode, its because the timer never starts or Tmer1Timer() is not set as the callback function. Work back from there.

A hint : sometimes, especially with timing specific problems, the old fashioned debugging model is more useful. Put eg writeln('Start of Timer1Timer()') type statements all through the relevant pars of your code. Makes it very easy to see what is, and what is not happening. And it feels great removing them when its fixed !

Davo 
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

tetrastes

  • Hero Member
  • *****
  • Posts: 739
Re: Timer in Linux anything special
« Reply #2 on: August 15, 2025, 11:37:25 am »
https://forum.lazarus.freepascal.org/index.php/topic,72046.msg563307.html#msg563307

In fact the compiled program hangs in ReadLn if something is received from port, or in Eof, if nothing is received.

 

TinyPortal © 2005-2018