En/na John Samperi ha escrit:
Also is there any serial port component available? I have been
using Cport with D6.
I've been successfully using synaser (http://www.ararat.cz/synapse/)
with lazarus. I made some changes to make it work (and changed the
behaviour of connect not to change the name of the device: it's better
with usb serial port). Note that I developed my program under linux, but
it should work fine in windows too.
My changes are attached, I forgot to send them upstream (I'll do it asap).
Bye
--
Luca Olivetti
Wetron Automatización S.A. http://www.wetron.es/
Tel. +34 93 5883004 Fax +34 93 5883007
diff --unified --recursive source.orig/synaser.pas source/synaser.pas
--- source.orig/synaser.pas 2004-08-25 19:32:28.000000000 +0200
+++ source/synaser.pas 2005-11-02 09:37:10.082676918 +0100
@@ -91,12 +91,12 @@
Types,
{$ENDIF}
{$ELSE}
- Windows, Classes, registry,
+ Windows, registry,
{$IFDEF FPC}
winver,
{$ENDIF}
{$ENDIF}
- SysUtils, synautil;
+ SysUtils, synautil, Classes;
const
CR = #$0d;
@@ -271,7 +271,7 @@
FATResult: Boolean;
FAtTimeout: integer;
FInterPacketTimeout: Boolean;
- FComNr: integer;
+ //FComNr: integer;
{$IFNDEF LINUX}
FPortAddr: Word;
function CanEvent(Event: dword; Timeout: integer): boolean;
@@ -287,7 +287,7 @@
function GetCarrier: Boolean; virtual;
function GetRing: Boolean; virtual;
procedure DoStatus(Reason: THookSerialReason; const Value: string);
virtual;
- procedure GetComNr(Value: string); virtual;
+ //procedure GetComNr(Value: string); virtual;
function PreTestFailing: boolean; virtual;{HGJ}
function TestCtrlLine: Boolean; virtual;
{$IFDEF LINUX}
@@ -313,6 +313,9 @@
{:Returns a string containing the version number of the library.}
class function GetVersion: string; virtual;
+
+ {:Returns a string with the standard device name corresponding to the
device number.}
+ class function GetDeviceName(DeviceNum:integer): string;
{:Destroy handle in use. It close connection to serial port.}
procedure CloseSocket; virtual;
@@ -355,6 +358,7 @@
- when you connect to a modem device, then is best to test it by an empty
AT command. (call ATCommand('AT'))}
procedure Connect(comport: string); virtual;
+ procedure Connect(comport: integer); virtual;
{:Set communication parameters from the DCB structure (the DCB structure is
simulated under Linux).}
@@ -712,7 +716,7 @@
FRaiseExcept := false;
FHandle := INVALID_HANDLE_VALUE;
FDevice := '';
- FComNr:= PortIsClosed; {HGJ}
+ //FComNr:= PortIsClosed; {HGJ}
FInstanceActive:= false; {HGJ}
Fbuffer := '';
FRTSToggle := False;
@@ -763,7 +767,7 @@
FInstanceActive:= false
end;
Fhandle := INVALID_HANDLE_VALUE;
- FComNr:= PortIsClosed;
+ //FComNr:= PortIsClosed;
SetSynaError(sOK);
DoStatus(HR_SerialClose, FDevice);
end;
@@ -799,6 +803,7 @@
end;
{$ENDIF}
+{
procedure TBlockSerial.GetComNr(Value: string);
begin
FComNr := PortIsClosed;
@@ -807,6 +812,8 @@
if pos('/DEV/TTYS', uppercase(Value)) = 1 then
FComNr := StrToIntdef(copy(Value, 10, Length(Value) - 9), PortIsClosed -
1);
end;
+}
+
procedure TBlockSerial.SetBandwidth(Value: Integer);
begin
@@ -867,6 +874,20 @@
SetCommState;
end;
+class function TBlockSerial.GetDeviceName(DeviceNum: integer):string;
+begin
+ {$IFDEF LINUX}
+ Result:='/dev/ttyS' + IntToStr(DeviceNum-1);
+ {$ELSE}
+ Result:='\\.\COM' + IntToStr(DeviceNum);
+ {$ENDIF}
+end;
+
+procedure TBlockSerial.Connect(comport: integer);
+begin
+ Connect(GetDeviceName(comport));
+end;
+
procedure TBlockSerial.Connect(comport: string);
{$IFNDEF LINUX}
var
@@ -881,7 +902,7 @@
end; {HGJ}
FBuffer := '';
FDevice := comport;
- GetComNr(comport);
+ //GetComNr(comport);
{$IFNDEF LINUX}
SetLastError (sOK);
{$ELSE}
@@ -892,8 +913,8 @@
{$ENDIF}
{$ENDIF}
{$IFDEF LINUX}
- if FComNr <> PortIsClosed then
- FDevice := '/dev/ttyS' + IntToStr(FComNr);
+ //if FComNr <> PortIsClosed then
+ // FDevice := '/dev/ttyS' + IntToStr(FComNr);
// Comport already owned by another process? {HGJ}
if FLinuxLock then
if not cpomComportAccessible then
@@ -910,8 +931,8 @@
if FLastError <> sOK then
Exit;
{$ELSE}
- if FComNr <> PortIsClosed then
- FDevice := '\\.\COM' + IntToStr(FComNr + 1);
+ //if FComNr <> PortIsClosed then
+ // FDevice := '\\.\COM' + IntToStr(FComNr + 1);
FHandle := THandle(CreateFile(PChar(FDevice), GENERIC_READ or GENERIC_WRITE,
0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, 0));
SerialCheck(integer(FHandle));
@@ -938,7 +959,7 @@
cpomReleaseComport; {HGJ}
{$ENDIF} {HGJ}
Fhandle := INVALID_HANDLE_VALUE; {HGJ}
- FComNr:= PortIsClosed; {HGJ}
+ //FComNr:= PortIsClosed; {HGJ}
end
else
begin
diff --unified --recursive source.orig/synautil.pas source/synautil.pas
--- source.orig/synautil.pas 2004-06-27 21:25:22.000000000 +0200
+++ source/synautil.pas 2005-11-02 09:37:04.186238282 +0100
@@ -120,11 +120,11 @@
function DecodeRfcDateTime(Value: string): TDateTime;
{:Return current system date and time in UTC timezone.}
-function GetUTTime: TDateTime;
+//function GetUTTime: TDateTime;
{:Set Newdt as current system date and time in UTC timezone. This function work
only if you have administrator rights!}
-function SetUTTime(Newdt: TDateTime): Boolean;
+//function SetUTTime(Newdt: TDateTime): Boolean;
{:Return current value of system timer with precizion 1 millisecond. Good for
measure time difference.}
@@ -672,82 +672,82 @@
{==============================================================================}
-function GetUTTime: TDateTime;
-{$IFNDEF LINUX}
-{$IFNDEF FPC}
-var
- st: TSystemTime;
-begin
- GetSystemTime(st);
- result := SystemTimeToDateTime(st);
-{$ELSE}
-var
- st: SysUtils.TSystemTime;
- stw: Windows.TSystemTime;
-begin
- GetSystemTime(stw);
- st.Year := stw.wYear;
- st.Month := stw.wMonth;
- st.Day := stw.wDay;
- st.Hour := stw.wHour;
- st.Minute := stw.wMinute;
- st.Second := stw.wSecond;
- st.Millisecond := stw.wMilliseconds;
- result := SystemTimeToDateTime(st);
-{$ENDIF}
-{$ELSE}
-var
- TV: TTimeVal;
- TZ: Ttimezone;
-begin
- TZ.tz_minuteswest := 0;
- TZ.tz_dsttime := 0;
- gettimeofday(TV, TZ);
- Result := UnixDateDelta + (TV.tv_sec + TV.tv_usec / 1000000) / 86400;
-{$ENDIF}
-end;
-
-{==============================================================================}
-
-function SetUTTime(Newdt: TDateTime): Boolean;
-{$IFNDEF LINUX}
-{$IFNDEF FPC}
-var
- st: TSystemTime;
-begin
- DateTimeToSystemTime(newdt,st);
- Result := SetSystemTime(st);
-{$ELSE}
-var
- st: SysUtils.TSystemTime;
- stw: Windows.TSystemTime;
-begin
- DateTimeToSystemTime(newdt,st);
- stw.wYear := st.Year;
- stw.wMonth := st.Month;
- stw.wDay := st.Day;
- stw.wHour := st.Hour;
- stw.wMinute := st.Minute;
- stw.wSecond := st.Second;
- stw.wMilliseconds := st.Millisecond;
- Result := SetSystemTime(stw);
-{$ENDIF}
-{$ELSE}
-var
- TV: TTimeVal;
- d: double;
- TZ: Ttimezone;
-begin
- Result := false;
- TZ.tz_minuteswest := 0;
- TZ.tz_dsttime := 0;
- gettimeofday(TV, TZ);
- d := (newdt - UnixDateDelta) * 86400;
- TV.tv_sec := trunc(d);
- TV.tv_usec := trunc(frac(d) * 1000000);
- Result := settimeofday(TV, TZ) <> -1;
-{$ENDIF}
-end;
+//function GetUTTime: TDateTime;
+//{$IFNDEF LINUX}
+//{$IFNDEF FPC}
+//var
+// st: TSystemTime;
+//begin
+// GetSystemTime(st);
+// result := SystemTimeToDateTime(st);
+//{$ELSE}
+//var
+// st: SysUtils.TSystemTime;
+// stw: Windows.TSystemTime;
+//begin
+// GetSystemTime(stw);
+// st.Year := stw.wYear;
+// st.Month := stw.wMonth;
+// st.Day := stw.wDay;
+// st.Hour := stw.wHour;
+// st.Minute := stw.wMinute;
+// st.Second := stw.wSecond;
+// st.Millisecond := stw.wMilliseconds;
+// result := SystemTimeToDateTime(st);
+//{$ENDIF}
+//{$ELSE}
+//var
+// TV: TTimeVal;
+// TZ: Ttimezone;
+//begin
+// TZ.tz_minuteswest := 0;
+// TZ.tz_dsttime := 0;
+// gettimeofday(TV, TZ);
+// Result := UnixDateDelta + (TV.tv_sec + TV.tv_usec / 1000000) / 86400;
+//{$ENDIF}
+//end;
+
+{==============================================================================}
+
+//function SetUTTime(Newdt: TDateTime): Boolean;
+//{$IFNDEF LINUX}
+//{$IFNDEF FPC}
+//var
+// st: TSystemTime;
+//begin
+// DateTimeToSystemTime(newdt,st);
+// Result := SetSystemTime(st);
+//{$ELSE}
+//var
+// st: SysUtils.TSystemTime;
+// stw: Windows.TSystemTime;
+//begin
+// DateTimeToSystemTime(newdt,st);
+// stw.wYear := st.Year;
+// stw.wMonth := st.Month;
+// stw.wDay := st.Day;
+// stw.wHour := st.Hour;
+// stw.wMinute := st.Minute;
+// stw.wSecond := st.Second;
+// stw.wMilliseconds := st.Millisecond;
+// Result := SetSystemTime(stw);
+//{$ENDIF}
+//{$ELSE}
+//var
+// TV: TTimeVal;
+// d: double;
+// TZ: Ttimezone;
+//begin
+// Result := false;
+// TZ.tz_minuteswest := 0;
+// TZ.tz_dsttime := 0;
+// gettimeofday(TV, TZ);
+// d := (newdt - UnixDateDelta) * 86400;
+// TV.tv_sec := trunc(d);
+// TV.tv_usec := trunc(frac(d) * 1000000);
+// Result := settimeofday(TV, TZ) <> -1;
+//{$ENDIF}
+//end;
{==============================================================================}