Difference between revisions of "Hardware Access/de"

From Lazarus wiki
Jump to navigationJump to search
Line 19: Line 19:
 
Hilfe ist willkommen, speziell zu MacOS.
 
Hilfe ist willkommen, speziell zu MacOS.
  
==Using inpout32.dll for Windows==
+
==Verwendung von inpout32.dll für Windows==
Windows has different ways to access hardware devices on the 9x series and on the NT series, and this can be really problematic. The 9x series (95, 98, Me) allow the programs to access ports directly, just like they did on DOS. Windows NT, however, completely prevents this, and only allows access to the ports throught device drivers. This is a secutiry mechanism, but can be anoying as a device driver may be too complex for a small project that need to access a parallel port.
+
Windows hat verschiedene Wege für die Ansteuerung von Hardware Geräten bei der 9x Serie und bei der NT Serie, und dies kann wirklich problematisch sein. Die 9x Serie (95, 98, Me) erlaubt den Prgrammen direkten Zugriff auf die Ports, genau wie unter DOS. Windows NT, jedoch, verhindert dies komplett, und erlaubt nur den Zugriff durch die Gerätetreiber auf die Ports. Dies ist ein Sicherheitsmechanismus, der aber nervig sein kann, wenn ein Gerätetreiber zu komplex für ein kleines Projekt ist, das Zugriff auf den parallelen Port benötigt.
  
Happily there is a library that carryes a device driver inside it. If Windows NT is detected, it decompresses the device and install the Hwinterface.sys kernel device driver. If Windows 9x is detected it simply uses assembler opcodes to access the hardware.
+
Glücklicherweise gibt es eine Bibliothek, die einen Gerätetreiber enthält. Wenn Windows NT erkannt wird, dann dekomprimiert sie das Gerät und installiert den Hwinterface.sys Kernel Gerätetreiber. Wenn Windows 9x erkannt wird, dann benutzt sie einfach Assembler Opcodes für den Zugriff auf die Hardware.
  
But how to use the library? Simple! It only has two functions, Inp32 and Out32, and their use is quite intuitive.
+
Aber wie benutzt man die Bibliothek? Einfach! Sie hat nur zwei Funktionen, Inp32 und Out32, und deren Benutzung ist ganz intuitiv.
  
We will be loading the library dynamically, so let's define both functions first:
+
Wir werden die Bibliothek dynamisch laden, so lassen sie uns zuerst beide Funktionen definieren:
  
 
<code>
 
<code>
Line 34: Line 34:
 
</code>
 
</code>
  
* Address represents the address of the port you desire to access. Out32 sends Data to the port you specify by Address. Inp32 returns a byte from the port you specify by Address.
+
* Address representiert die Adresse des Ports, auf den sie zugreifen wollen. Out32 sendet Daten zu dem Port, den sie mit Adress spezifiziert haben. Inp32 gibt ein Byte zurück vom Port, den sie mit Address spezifiziert haben.
  
Now we can load the library. This can be implemented in a place like the OnCreate method of your program's main form:
+
Nun können wir die Bibliothek laden. Dies kann implementiert werden an einer Stelle wie der OnCreate Methode des Haupformulars ihres Programms:
  
 
<code>
 
<code>
Line 66: Line 66:
 
</code>
 
</code>
  
If you load the library on OnCreate just don't forget to unload it in OnDestroy:
+
Wenn sie die Bibliothek bei OnCreate laden, vergessen sie nicht, sie bei OnDestroy zu entladen:
  
 
<code>
 
<code>
Line 77: Line 77:
 
</code>
 
</code>
  
Here is a simple example of how to use Inp32 function:
+
Hier ist ein einfaches Beispiel, wie man die Inp32 Funktion benutzt:
  
 
<code>
 
<code>
Line 85: Line 85:
 
</code>
 
</code>
  
This code was tested with a custom ISA card on port $0220, using Lazarus 0.9.10 on Windows XP. Of course you will need to have Windows on your uses clause in order for this code to run. For deployment you only need to include "inpout32.dll" in the same directory of our application.
+
Dieser Code wurde getestet mit einer maßgeschneiderten ISA Karte auf Port $0220, unter Verwendung von Lazarus 0.9.10 mit Windows XP. Natürlich müssen sie Windows in ihrem uses Abschnitt haben damit dieser Code läuft. Für den Einsatz müssen sie nur "inpout32.dll" in das selbe Verzeichnis wie ihre Anwendung einfügen.
  
This is the homepage for the library: [http://www.logix4u.net/inpout32.htm www.logix4u.net/inpout32.htm]
+
Dies ist die Homepage für die Bibliothek: [http://www.logix4u.net/inpout32.htm www.logix4u.net/inpout32.htm]
  
 
==Using ioperm to access ports on Linux==
 
==Using ioperm to access ports on Linux==

Revision as of 19:00, 13 November 2005

Deutsch (de) English (en) español (es) français (fr) magyar (hu) 日本語 (ja) 한국어 (ko) polski (pl) português (pt) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

Überblick

Diese Seite ist der Beginn eines Tutorials über den Zugriff auf Hardware Geräte mit Lazarus. Diese Geräte enthalten, ohne darauf beschränkt zu sein: ISA, PCI, USB, paralleler und serieller Anschluß.

Der Zugriff auf Hardware Geräte auf einer komplett plattformunabhängigen Weise ist nicht implementiert durch die Free Pascal Runtime Library oder durch die LCL, daher umfasst dieses Tutorial grundsätzlich Hardware Zugriffsmethoden auf verschiedenen Plattformen. Der Code kann unter verschiedenen Umgebungen kompiliert werden unter Verwendung bedingter Kompilierung, etwa so:

uses
 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,
{$IFDEF WIN32}
  Windows;
{$ENDIF}
{$IFDEF Linux}
  ports;
{$ENDIF}

Hilfe ist willkommen, speziell zu MacOS.

Verwendung von inpout32.dll für Windows

Windows hat verschiedene Wege für die Ansteuerung von Hardware Geräten bei der 9x Serie und bei der NT Serie, und dies kann wirklich problematisch sein. Die 9x Serie (95, 98, Me) erlaubt den Prgrammen direkten Zugriff auf die Ports, genau wie unter DOS. Windows NT, jedoch, verhindert dies komplett, und erlaubt nur den Zugriff durch die Gerätetreiber auf die Ports. Dies ist ein Sicherheitsmechanismus, der aber nervig sein kann, wenn ein Gerätetreiber zu komplex für ein kleines Projekt ist, das Zugriff auf den parallelen Port benötigt.

Glücklicherweise gibt es eine Bibliothek, die einen Gerätetreiber enthält. Wenn Windows NT erkannt wird, dann dekomprimiert sie das Gerät und installiert den Hwinterface.sys Kernel Gerätetreiber. Wenn Windows 9x erkannt wird, dann benutzt sie einfach Assembler Opcodes für den Zugriff auf die Hardware.

Aber wie benutzt man die Bibliothek? Einfach! Sie hat nur zwei Funktionen, Inp32 und Out32, und deren Benutzung ist ganz intuitiv.

Wir werden die Bibliothek dynamisch laden, so lassen sie uns zuerst beide Funktionen definieren:

type
  TInp32 = function(Address: ShortInt): ShortInt; stdcall;
  TOut32 = procedure(Address: ShortInt; Data: ShortInt); stdcall;

  • Address representiert die Adresse des Ports, auf den sie zugreifen wollen. Out32 sendet Daten zu dem Port, den sie mit Adress spezifiziert haben. Inp32 gibt ein Byte zurück vom Port, den sie mit Address spezifiziert haben.

Nun können wir die Bibliothek laden. Dies kann implementiert werden an einer Stelle wie der OnCreate Methode des Haupformulars ihres Programms:

type
  TMyForm = class(TForm)
  .........
  private
    { private declarations }
    Inpout32: THandle;
    Inp32: TInp32;
    Out32: TOut32;
  .........
implementation
  .........
procedure TMyForm.FormCreate(Sender: TObject);
begin
{$IFDEF WIN32}
  Inpout32 := LoadLibrary('inpout32.dll');
  if (Inpout32 <> 0) then
  begin
    @Inp32 := GetProcAddress(Inpout32, 'Inp32');
    if (@Inp32 = nil) then Caption := 'Error';
    @Out32 := GetProcAddress(Inpout32, 'Out32');
    if (@Out32 = nil) then Caption := 'Error';
  end
  else Caption := 'Error';
{$ENDIF}
end;

Wenn sie die Bibliothek bei OnCreate laden, vergessen sie nicht, sie bei OnDestroy zu entladen:

procedure TMyForm.FormDestroy(Sender: TObject);
begin
{$IFDEF WIN32}
  FreeLibrary(Inpout32);
{$ENDIF}
end;

Hier ist ein einfaches Beispiel, wie man die Inp32 Funktion benutzt:

{$IFDEF WIN32}
  myLabel.Caption := IntToStr(Inp32($0220));
{$ENDIF}

Dieser Code wurde getestet mit einer maßgeschneiderten ISA Karte auf Port $0220, unter Verwendung von Lazarus 0.9.10 mit Windows XP. Natürlich müssen sie Windows in ihrem uses Abschnitt haben damit dieser Code läuft. Für den Einsatz müssen sie nur "inpout32.dll" in das selbe Verzeichnis wie ihre Anwendung einfügen.

Dies ist die Homepage für die Bibliothek: www.logix4u.net/inpout32.htm

Using ioperm to access ports on Linux

The best way to access the hardware on Linux is throught device drivers, but, due to the complexity of the task of creating a driver, sometimes a quick method is very useful.

In order to use the "ports" unit under linux your program must be run as root, and IOPerm must be called to set appropriate permissions on the port access. You can find documentation about the "ports" unit here.

The first thing to do is link to glibc and call IOPerm. A unit that links to the entire glibc exists on free pascal, but this unit gives problems when used directly by application and linking statically to the entire glibc library is not a very good idea for a multi-platform application. The use of platform-specific functions should be kept to a minimum.

{$IFDEF Linux}
function ioperm(from: Cardinal; num: Cardinal; turn_on: Integer): Integer; cdecl; external 'libc';
{$ENDIF}

  • "from" represents the first port to be accessed.
  • "num" is the number of ports after the first to be accessed, so ioperm($220, 8, 1) will give access for the program for all ports between and including $220 and $227.

After linking to OIPerm you can port[<Address>] to access the ports.

{$IFDEF Linux}
  i := ioperm($220, 8, 1);
  port[$220] := $00;
  myLabel.Caption := 'ioperm: ' + IntToStr(i);
  i := Integer(port[$220]);
  myOtherLabel.Caption := 'response: ' + IntToStr(i);
{$ENDIF}

This code was tested with a custom ISA card on port $0220, using Lazarus 0.9.10 on Mandriva Linux 2005 and Damn Small Linux 1.5