Difference between revisions of "Hardware Access/fr"

From Lazarus wiki
Jump to navigationJump to search
Line 30: Line 30:
 
|-
 
|-
 
!
 
!
! Speed
+
! Vitesse
! Hardware implementation difficulty
+
! Difficulté d'implémentation matérielle
  
 
|-
 
|-
! Serial Port
+
! Port série
| align="center" | Very slow (< E5 bit/s)
+
| align="center" | Très lent  (< E5 bit/s)
| align="center" | Medium
+
| align="center" | Milieu
  
 
|-
 
|-
! Parallel Port
+
! Port parallèle
| align="center" | Slow (~ E6 bit/s)
+
| align="center" | Lent  (~ E6 bit/s)
| align="center" | Easy
+
| align="center" | Facile
  
 
|-
 
|-
  
! ISA Card
+
! Carte ISA  
| align="center" | Medium (~ E7 bit/s)
+
| align="center" | Milieu  (~ E7 bit/s)
| align="center" | Medium
+
| align="center" | Milieu
  
 
|-
 
|-
 
! USB
 
! USB
| align="center" | Medium (~ E7 bit/s)
+
| align="center" | Milieu  (~ E7 bit/s)
| align="center" | Hard
+
| align="center" | Dur
  
 
|-
 
|-
! PCI Card
+
! Carte PCI  
| align="center" | Very Fast (> E9 bit/s)
+
| align="center" | Très rapide  (> E9 bit/s)
| align="center" | Very Hard
+
| align="center" | Très dur
  
 
|}
 
|}

Revision as of 22:08, 17 June 2007

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)

Vue d'ensemble

Cette page est le début d'un tutoriel sur la manière d'accéder aux périphériques matériels de Lazarus. Ces dispositifs incluent les suivant , mais ne sont pas limités à eux: ISA, PCI, USB, port parallèle , porte série .

l'accès aux périphériques matériels d'une façon complètement multiplate-forme n'est pas implementé par la bibliothèque d'exécution de Free Pascal ou par la LCL, ainsi ce tutoriel va fondamentalement couvrir des méthodes d'accès matérielles sur différentes plate-formes. Le code peut être compilé sur différents environnements en employant la compilation conditionnelle, comme ceci :

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

On ne sait pas encore , actuellement , si Mac OS X/x86 permettra des accès HW. Il peut le rejetter, bien que je suppose dans ce cas , à temps , des pilotes de périphériques comme io.dll vont apparaître.

Comparaison parallèle et série

Les cartes ISA, Les cartes PCI et le port parallèle communique avec l'ordinateur en utilisant un protocole parallèle . Le port série et les périphériques USB travaillent avec un protocole serie. Puisque le processeur et de ce fait les langages de programmation travaillent tous dans une approche parallèle au données, accéder à ces types de protocoles est plus simple à être implémenté du coté logiciel. Quand vous accédez à une variable entière, par exemple, vous pouvez accéder à sa valeur par une commande simple. Avec un protocole série, cependant ,vous pouvez seulement connaître un bit à la fois, et vous devez coller les pièces ensemble pour comprendre les données.

La communication série est difficile à être implémenté directement, mais ce peut être assez facile si vous employez un composant pré-fait. C'est également plus dur du côté matériel, aussi plusieurs périphériques utiliser des circuits intégrés spécialisés ou même des Microcontrôleurs pour l'implémenter.

Maintenant une brève comparaison des protocoles d'accès au matériel sera donné:

Vitesse Difficulté d'implémentation matérielle
Port série Très lent (< E5 bit/s) Milieu
Port parallèle Lent (~ E6 bit/s) Facile
Carte ISA Milieu (~ E7 bit/s) Milieu
USB Milieu (~ E7 bit/s) Dur
Carte PCI Très rapide (> E9 bit/s) Très dur

Parallel Communication

Using inpout32.dll for Windows

Windows has different ways to access hardware devices on the 9x series and on the NT series. On the 9x series (95, 98, Me) programs can access the hardware directly, just like they did on DOS. The NT series (Windows NT and XP), however, don't allow this approach. On this architecture, all communication with hardware ports must be throught a device driver. This is a security mechanism, but developing a driver can cost too much in terms of time and money for small projects.

Happily there is a library that solves this problem. If Windows NT is detected, it decompresses HWInterface.sys kernel device driver and installs it. If Windows 9x is detected, it simply uses assembler opcodes to access the hardware.

But how do I use the library? Simple! It has only two functions, Inp32 and Out32, and their use is quite intuitive.

We will load the library dynamically, so let's define both functions first:

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

  • 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

Now we can load the library. This can be implemented in a place like the OnCreate method of your program's main form:

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
    // needs overtyping, plain Delphi's @Inp32 = GetProc... leads to compile errors
    Inp32 := TInp32(GetProcAddress(Inpout32, 'Inp32'));
    if (@Inp32 = nil) then Caption := 'Error';
    Out32 := TOut32(GetProcAddress(Inpout32, 'Out32'));
    if (@Out32 = nil) then Caption := 'Error';
  end
  else Caption := 'Error';
{$ENDIF}
end;

If you load the library on OnCreate just don't forget to unload it in OnDestroy:

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

Here is a simple example of how to use Inp32 function:

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

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.

This is the homepage for the library: www.logix4u.net/inpout32.htm *see discussion*

Using assembler on Windows 9x

On Windows 9x you can also use assembler code. Suppose you wish to write $CC to the $320 port. The following code will do it:

{$ASMMODE ATT}
...
   asm
       movl $0x320, %edx
       movb $0xCC, %al
       outb %al, %dx
   end ['EAX','EDX'];

Troubleshooting on Windows

One possible source of trouble using parallel hardware that does not support Plug And Play on Windows is that Windows may assign the port utilized by your hardware to another device. You can find instructions on the URL below about how to tell Windows not to assign the address of your device to Plug And Play devices:

http://support.microsoft.com/kb/135168

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 (g)libc and call IOPerm. A unit that links to the entire (g)libc exists on free pascal, but this unit gives problems when used directly by application and linking statically to the entire (g)libc library is not a very good idea because it changes often between version in an incompatible manner. Functions like ioperm, however, are unlikely to change.

{$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 IOPerm 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

General UNIX Hardware Access

{$IFDEF Unix}
Uses Clib;   // retrieve libc library name.
{$ENDIF}

{$IFDEF Unix}
function ioperm(from: Cardinal; num: Cardinal; turn_on: Integer): Integer; cdecl; external clib;
{$ENDIF}


Note that FPC provides an abstraction for ioperm called "fpioperm" in unit x86, and also defines out and inport functions. These functions are currently implemented for Linux/x86 and FreeBSD/x86.

It is not recommended to link to libc unless absolutely necessary due to possible deployment and portability functions. Also manual linking to libc (by declaring ad hoc libc imports for functions that are available elsewhere) like done above is not recommended (e.g. the above libc import line will unnecessarily fail if the standard C lib is not called libc, like e.g. libroot on BeOS, or on platforms with a non standard C symbol mangling).

Note 2 Using _unit_ libc is not recommended under any circumstances other than Kylix compability. This because the unit is relatively unportable (due to excessive exposure of structures and other private symbols) and must only be modified as little as possible out of Kylix compability issues.

Serial Communication

The External Links section has UNIX and Windows serial port tutorials.

External Links

Communication Protocols speed comparison:

  1. http://en.wikipedia.org/wiki/Serial_port#Speed
  2. http://www.lvr.com/jansfaq.htm - Jan Axelson's Parallel Port FAQ
  3. http://en.wikipedia.org/wiki/USB#Transfer_Speed
  4. http://en.wikipedia.org/wiki/PCI#Conventional_PCI_bus_specifications

Serial Communication Links:

  1. On UNIX: [1]
  2. On Windows: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
  3. Synaser component: http://synapse.ararat.cz/
  4. Comport Delphi package: http://sourceforge.net/projects/comport/

ISA Digital Oscilloscope - A example of hardware access with full source included:

[2]