AL_USBMotor

Top  Previous  Next

unit AL_USBMotor;

 

interface

Uses Windows,Forms,Dialogs, Messages, SysUtils, Classes, Graphics, Controls,

    ExtCtrls, AL_USBUnit, Szamok;

 

 

Type

 

TChangeConnection = procedure(Sender: TObject; Connected: boolean) of object;

TReadData = procedure(Sender: TObject; Data: integer) of object;

 

TALUSBMotor = class(TComponent)

private

   fIsMachine: boolean;

   FUSBName: string;

   FChangeConnection: TChangeConnection;

   FErrorRiport: boolean;

   FUSBData: integer;

   FTReadData: TReadData;

   procedure Timer(Sender:TObject);

   procedure setIsMachine(const Value: boolean);

   function GetUSBCount: integer;

   procedure SetErrorRiport(const Value: boolean);

   procedure SetUSBData(const Value: integer);

protected

   dTimer : TTimer;

public

   USBNames    : Names;

   constructor Create(AOwner:TComponent);override;

   destructor Destroy;override;

   function InitUSB: boolean;

   procedure CloseUSB;

   function IsUSBModem: boolean;

   function GetUSBNames: boolean;

   function ReadUSB: integer;

   function WriteUSB(inout,data: byte): boolean;

   function SetUSBBit(bitNo: byte; Setted: boolean): boolean;

   property USBName: string read FUSBName write FUSBName;

   property USBCount: integer read GetUSBCount;

   property USBData : integer read FUSBData write SetUSBData;

published

   Property IsMachine: boolean read fIsMachine write setIsMachine default False;

   property ErrorRiport: boolean read FErrorRiport write SetErrorRiport;

   property OnChangeConnection: TChangeConnection read FChangeConnection

            write FChangeConnection;

   property OnReadData: TReadData read FTReadData write FTReadData;

end;

 

 

procedure Register;

 

implementation

 

procedure Register;

begin

RegisterComponents('AL',[TALUSBMotor]);

end;

 

 

{ TALUSBMotor }

 

constructor TALUSBMotor.Create(AOwner: TComponent);

begin

inherited;

ErrorRiport     := False;

dTimer          := TTimer.Create(Self);

dTimer.Interval := 1;

dTimer.OnTimer  := Timer;

FUSBData        := 0;

FIsMachine      := False;

IsMachine       := InitUSB;

end;

 

destructor TALUSBMotor.Destroy;

begin

dTimer.Free;

CloseUSB;

inherited;

end;

 

function TALUSBMotor.InitUSB: boolean;

begin

Result := False;

IF (USBCount>0) and (USBName='') then

    GetUSBNames;

    USBName := USBNames[1];

IF (USBCount>0) and (USBName<>'') then begin

    Result := Init_Controller(USBName);

    Passed := Result;

end;

end;

 

procedure TALUSBMotor.Timer(Sender:TObject);

begin

IsMachine := ReadUSB>-1;

end;

 

function TALUSBMotor.IsUSBModem: boolean;

begin

Result := GetUSBCount>0;

end;

 

procedure TALUSBMotor.setIsMachine(const Value: boolean);

begin

if fIsMachine <> Value then begin

    fIsMachine := Value;

    if not Value then CloseUSB;

end;

if not Passed then InitUSB;

if Assigned(FChangeConnection) then FChangeConnection(Self,Passed);

end;

 

// Reads a byte from USB Port

function TALUSBMotor.ReadUSB: integer;

var oke: boolean;

   data: byte;

begin

    oke := read_value(data,FUSBName);

    if oke then

       Result := data

    else

       Result := -1;

end;

 

function TALUSBMotor.WriteUSB(inout,data: byte): boolean;

begin

write_value(inout,data,FUSBName);

end;

 

// Stores the names of USB connections

function TALUSBMotor.GetUSBNames: boolean;

Var i : integer;

   Has_A : boolean;

begin

for i := 1 to 20 do USBNames[i] := '';

List_Devs(@USBNames);

FUSBName := USBNames[1];

end;

 

function TALUSBMotor.GetUSBCount: integer;

var

NoOfDevs: integer;

res : FT_Result;

begin

Try

res := GetFTDeviceCount;

if res <> Ft_OK then exit;

NoOfDevs := FT_Device_Count;

Result := NoOfDevs;

except

Result := 0;

end;

end;

 

procedure TALUSBMotor.SetErrorRiport(const Value: boolean);

begin

FErrorRiport := Value;

FT_Enable_Error_Report := Value;

end;

 

function TALUSBMotor.SetUSBBit(bitNo: byte; Setted: boolean): boolean;

var Enable,Data : byte;

begin

data := ReadUSB;

Enable := 0;

enable := enable or 240;

if Setted then

    SetBit(Data,bitNo,1)

else

    SetBit(Data,bitNo,0);

FUSBData := data;

write_value(Enable,data,FUSBName);

//  WriteUSB(Enable,Data);

end;

 

procedure TALUSBMotor.SetUSBData(const Value: integer);

begin

FUSBData := Value;

WriteUSB(240,FUSBData);

end;

 

procedure TALUSBMotor.CloseUSB;

begin

ClosePort;

Passed := False;

end;

 

End.