Podstawowe pytanka (LPT i Delphi)

0

Poczytałem od podstaw elektroniki itp. Zrobiłem mini obwód czyli podłączyłem kabelek do portu LPT a dokładnie do pinu 2 (D0), następnie rezystor 700 ( będzie inny ale jes sobota i nigdzie nie dostałem innych) następnie diodę LED i kabelek do masy. Dioda świeciła, świeciła smile.gif Teraz w środowisku Delphi chciałbym sterować włączaniem i wyłączniem diody. Czytałem że ogólnie chodzi o wysyłanie danych. Czytałem także że trzeba wykorzystać bibliotekę zlportio. Mój problem polega że nie wiem jak wysyłać dane do portu LPT a dokładnie do D0 żeby dioda gasła i świciła. Ogólnie to aż tyle kodu mam biggrin.gif

KOD
unit diody;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
if ZLIOStarted then
Label1.Caption := 'Sterownik załadowany poprawnie.'
else
Label1.Caption := 'Nie można załadować sterownika.';
portwriteb($378,$55); // wpisanie wartości 01010101 (bin)
end;

procedure TForm1.Button1Click(Sender: TObject); // włączanie diody
begin

end;

procedure TForm1.Button2Click(Sender: TObject); // wyłączanie diody
begin

end;

end.

Kod oczywiście później usprawnię żeby na przycisk zaświeć można było raz kliknąć itp.

Wiem że pytanie pewnie banalne ale nie wiem po prostu jak wysyłać i co decyduje że dioda świeci bądź też nie.

Dziękuje z góry
Tomek

0

inpou32.dll

tu masz gotowce
http://www.rafal.waw.pl/wynalazki/programy/

ale oczywiście jest psikus, będziesz musiał przetłumaczyć sobie z VB na Delphi
baaaaaaardzo trudne zadanie =]

0

No tak ale mi chodzi na Delphi a nie na VB

0

" baaaaaaardzo trudne zadanie =] "

... normalnie taaaakie trudne że aż strach =] ściągnij sobie źródełka a zobaczysz jak trudne jest tłumaczenie z VB na Delphi, zwłaszcza w tych tam kodzikach gdzie praktycznie są tylko wywołania do dllki =]

0

portwriteb($378,$0); -> d0..d7 ustawione na 0
portwriteb($378,$ff); -> d0..d7 ustawione na 1
portwriteb($378,$1); -> d0 ustawione na 1

regula jest taka:
00000000(bin) = 0(hex) = d0..d7 ustawione na 0
11111111(bin) = ff(hex) = d0..d7 ustawione na 1
10101010(bin) = aa(hex) = d7 =1, d6 = 0, d5 = 1 itd

portwriteb(ADRES PORTU W HEX, WARTOŚĆ DO ZAPISANIA W HEX);

Źródło mego programu do zmieniania stanu pinów od d0 do d7 i ich monitorowania:

unit LPT_PS_fMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvExControls, JvLED, ZLPortIO, ExtCtrls, StdCtrls, Registry,
  JvComponentBase, JvFormMagnet;

type
  TfMain = class(TForm)
    L1: TJvLED;
    L2: TJvLED;
    L3: TJvLED;
    L4: TJvLED;
    L5: TJvLED;
    L6: TJvLED;
    L7: TJvLED;
    L0: TJvLED;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    procedure L0Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure Refresh;
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TState = (sHigh, sLow);
  AState = array[0..7] of TState;

type
  TCheckPort = class(TThread)
  protected
    procedure Execute; override;
  end;

var
  fMain: TfMain;
  CheckPort: TCheckPort;
  OldValue: Byte;

implementation

{$R *.dfm}

function StateToHex(State: array of TState): Byte;
const
  Values: array[0..7] of Byte = ($01, $02, $04, $08, $10, $20, $40, $80);
var
  Value: Byte;
  i: integer;
begin
  Value := $00;
  for i := 0 to 7 do
    if State[i] = sHigh then
      Value := Value + Values[i];
  Result := Value;
end;

procedure TfMain.L0Click(Sender: TObject);
var
  i: integer;
  State: AState;
begin
  TJvLED(Sender).Status := not TJvLed(Sender).Status;
  for i := 0 to 7 do
    if TJvLED(FindComponent('L' + IntToStr(i))).Status then
      State[i] := sHigh
    else
      State[i] := sLow;
  portwriteb($378, StateToHex(State));
  Refresh;
end;

function HexToState(Hex: Byte): AState;
var
  i: integer;
begin
  for i := 0 to 7 do
    if Odd(Hex shr I) then
      Result[i] := sHigh
    else
      Result[i] := sLow;
end;

procedure TfMain.Refresh;
var
  i: integer;
  State: AState;
begin
  State := HexToState(portreadb($378));
  for i := 0 to 7 do
    TJvLED(FindComponent('L' + IntToStr(i))).Status := (State[i] = sHigh);
end;

procedure TfMain.FormCreate(Sender: TObject);
var
  R: TRegistry;
begin
  Application.Title := Caption;
  if not ZLIOStarted then
  begin
    MessageBox(Handle, 'Błąd sterownika! Aplikacja zostanie zamknięta!', 'Błąd!', MB_OK + MB_ICONERROR);
    Application.Terminate;
    Exit;
  end;
  Show;
  Refresh;
  CheckPort := TCheckPort.Create(False);
  R := TRegistry.Create;
  try
    R.RootKey := HKEY_CURRENT_USER;
    R.OpenKey('Software\PortStatus\LPT', True);
    if R.ValueExists('Top') then
      Top := R.ReadInteger('Top');
    if R.ValueExists('Left') then
      Left := R.ReadInteger('Left');
  finally
    R.Free;
  end;
end;

procedure TfMain.Timer1Timer(Sender: TObject);
var
  i: integer;
begin
  for i := 1 to 30 do
    TJvLED(FindComponent('A' + IntToStr(i))).Status := not TJvLED(FindComponent('A' + IntToStr(i))).Status;
end;

{ TCheckPort }

procedure TCheckPort.Execute;
var
  NewValue: Byte;
begin
  inherited;
  while not (Application.Terminated) or Terminated do
  begin
    NewValue := portreadb($378);
    if NewValue <> OldValue then
    begin
      OldValue := NewValue;
      fMain.Refresh;
    end;
    Sleep(1);
  end;
end;

procedure TfMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
  R: TRegistry;
begin
  R := TRegistry.Create;
  try
    R.RootKey := HKEY_CURRENT_USER;
    R.OpenKey('Software\PortStatus\LPT', True);
    R.WriteInteger('Top', Top);
    R.WriteInteger('Left', Left);
  finally
    R.Free;
  end;
end;

end.

Może się przyda ;]

1 użytkowników online, w tym zalogowanych: 0, gości: 1