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 ;]