Recent

Author Topic: how does one write lazarus code to manipulate serial port??  (Read 7349 times)

4joey1

  • Newbie
  • Posts: 3
how does one write lazarus code to manipulate serial port??
« on: September 07, 2007, 06:36:01 pm »
Hallo!My names are Mwangi Joe Irungu.I am a 23 year old Kenyan citizen who is a Linux and Lazarus newbie.

Sometime back,I built a computerized thermometer project using Borland Delphi 7 in a Windows XP.The project includes an external thermometer circuit which is interfaced to the computer via the serial port.The programs works nicely in Windows XP.

My intention now is to replicate the project in Suse Linux 10.1 using Lazarus compiler.Unfortunately,Lazarus does not recognize the syntax  used to manipulate the serial port.Also,I have tried to use synapse but I do not know how to install it.My Delphi code is as  below.

unit temperature;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Timer1: TTimer;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button10: TButton;
    Timer2: TTimer;
    Edit1: TEdit;
    Edit2: TEdit;
    Label4: TLabel;
    Button11: TButton;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    tempLabel: TLabel;
    Button12: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    function  ReadPort(Port: word): byte;
    procedure Button11Click(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
    procedure Button12Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    temp,f,k:real;
    ticks:int64;
     Fport : integer;
     aDCb : tdcb;
     g,i,w,Q:integer;

  end;

var
  Form1: TForm1;
  potek:array[1..400000] of real;

implementation

{$R *.dfm}

function TForm1.ReadPort(Port: word): byte; assembler; register;
asm
    mov dx,Port
    cli
    in al,dx
    sti
end;

 function GetCPUTick: int64;
asm
   DB $0F,$31 // this is RDTSC command. Assembler, built in Delphi,
              // does not support it,
              // that is why one needs to overcome this obstacle.
end;

var
  CPUClock:extended;
   function CalibrateCPU:int64;
var
t:cardinal;
begin
t:=GetTickCount;
while t=GetTickCount do;
Result:=GetCPUTick;

while GetTickCount<(t+400)do;
Result:=GetCPUTick-result;
CPUClock:=2.5e-6*Result;
end;

function TicksToFloat(const Value:int64):real;
begin
Result:=Value/CPUClock;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
CalibrateCPU;
Label2.Caption:=Format('CPU clock = %f Mhz',[CPUClock]);
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
 if not timer2.Enabled then
  begin
   i:=1;
   button2.caption:='Stop';
   memo1.lines.Clear;
  timer2.enabled:=True;
  end
  else
   begin
  timer2.enabled:=False;
   button2.caption:='Log';
   
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  FPort:=CreateFile (pchar('COM1'),
                    GENERIC_READ or GENERIC_WRITE,0,NIl,
                    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
end;

procedure TForm1.Button4Click(Sender: TObject);
var us:real;
begin
ticks:=GetCPUTick-ticks;
us:=TicksToFloat(ticks);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
   EscapeCommFunction (FPort,CLRDTR)
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  FPort:=CreateFile (pchar('COM2'),
                    GENERIC_READ or GENERIC_WRITE,0,NIl,
                    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Button7.Caption:='Start';
  closeHandle (fPort);
end;







procedure TForm1.Button7Click(Sender: TObject);
var muc:dword;
    us:real;
    a,b: real;
begin
if Button7.Caption='Start' then Button7.Caption:='Stop'
else Button7.Caption:='Start';

while Button7.Caption='Stop' do begin
     EscapeCommFunction (FPort,SETDTR);
     ticks:=GetCPUTick;
     muc:=0;
while (muc =0) do begin

           GetCommModemStatus(Fport,muc);
           Application.ProcessMessages;
           end;
     ticks:=GetCPUTick-ticks;

us:=TicksToFloat(ticks);
//edit3.text:=FloatToStrF(us,ffGeneral,8,8);
EscapeCommFunction (FPort,CLRDTR);

label3.caption:=IntToStr(round(us));
{
upor:=0.0178*us/1000-0.424;
if upor>0 then begin
label1.caption:='R = '+IntTOStr(Round(upor*1000))+' Ohm';

temp:=1/(K+C*ln(upor))-273.16;
 }
 a:=StrToFloat(edit1.text);
 b:=StrToFloat(edit2.text);

 temp:=a*1/ln(us)+b;
 f:=9*temp/5+32;
 k:=temp+273.15;
// label9.Caption:=FloatToStrF(f,ffFixed,8,2);
// label10.Caption:=FloatToStrF(k,ffFixed,8,2);

tempLabel.caption:=FloatToStrF(temp,ffFixed,8,2);
{end
else begin
label1.caption:='Error. R too low!';
beep;
end;}
sleep(500);
Application.ProcessMessages;
end;

end;


procedure TForm1.Button8Click(Sender: TObject);
begin
EscapeCommFunction (FPort,SETDTR);
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
print;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var muc:dword;
begin
GetCommModemStatus(Fport,muc);
//edit1.text:=IntToStr( (muc ));
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
  FPort:=CreateFile (pchar('COM2'),
                    GENERIC_READ or GENERIC_WRITE,0,NIl,
                    OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
potek:=temp;
Memo1.Lines.Add(FloatToStrF(temp,ffFixed,8,2));
inc(i);
end;

procedure TForm1.Button12Click(Sender: TObject);
begin
print;
end;

end.

Is it possible for you to convert this code for me such that it runs in Lazarus or give me pointers on how I can change the code?Please respond to my request as soon as possible with solutions to my problem.Thanks in advance.

antonio

  • Hero Member
  • *****
  • Posts: 605
RE: how does one write lazarus code to manipulate serial por
« Reply #1 on: September 07, 2007, 07:06:52 pm »
If you use the utility on the menu Tools > Convert Delphi Unit to Lazarus Unit/Convert Delphi Project to Lazarus Project, it will add a compiler directive to your project to understand Delphi syntax.

As you saw on your previous message, you probably must add a -RIntel compiler directive to your code because the FPC default is AT&T standard.

Good luck.