Forum > Linux
Timer in Linux anything special
(1/1)
coradi:
In this code, the debug Breakpoint in timer, never increase, after click on button?!
--- Code: ---
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.
--- End code ---
dbannon:
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
tetrastes:
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.
Navigation
[0] Message Index