Difference between revisions of "Hardware Access/ja"

From Lazarus wiki
Jump to navigationJump to search
m
Line 144: Line 144:
 
あなたのLinuxプログラム上で "[[doc:rtl/ports|ports]]" ユニットを呼び出すためにはrootで実行する必要があり、またIOPermを呼び出すためにはポートにアクセスするための権限が必要です。 あなたは"[[doc:rtl/ports|ports]]"ユニットについての情報を[http://www.freepascal.org/docs-html/rtl/ports/index.html ここ]で手に入れることができます。
 
あなたのLinuxプログラム上で "[[doc:rtl/ports|ports]]" ユニットを呼び出すためにはrootで実行する必要があり、またIOPermを呼び出すためにはポートにアクセスするための権限が必要です。 あなたは"[[doc:rtl/ports|ports]]"ユニットについての情報を[http://www.freepascal.org/docs-html/rtl/ports/index.html ここ]で手に入れることができます。
  
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.
+
最初にやることは、(g)libcをリンクして、IOPermをコールすることです。すべての(g)libc(の関数)にリンクするユニットは、free pascal上にありますが、このユニットをアプリケーションから直接リンクすることは問題をひきおこします。(g)libcライブラリ全体を静的リンクをおこなう、ということはあまり良い方法とはいえません、なぜなら、それはバージョンの間で、しばしば変更されるからです。iopermといった関数はしかしながら、変更することはあまりないと思います。
  
 
<code>
 
<code>

Revision as of 07:53, 13 August 2006

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)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

概要

このページはLazarusでハードウェアデバイスにアクセスを行うためのチュートリアルです。ここにはISA、PCI、USB、パラレルポート、シリアルポートの各デバイスについて書かれていますが、完全ではありません。

ハードウェアデバイスへのアクセスはFree PascalランタイムライブラリやLCLをもってしても完全な移植性をもつことはできません。このチュートリアルは多くのプラットホームに適用できるハードウェアへアクセスの手法の基本的な部分をカバーします。コードを違った環境でコンパイルするためには以下のような条件付きコンパイルが必要となるでしょう。

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

x86プロセッサ上で動作するMac OS Xでのハードウェアアクセスが可能かどうかは現時点では不明です。

パラレルおよびシリアル接続

ISAカード、PCIカードおよびパラレルポートでの通信ではコンピュータはパラレルプロトコルを使用します。シリアルポートやUSBデバイスではシリアルプロトコルを使用します。このようにプロトコルを使い分けるのは、プロセッサやプログラミング言語はすべてデータにパラレルでアプローチするため、より簡単にするためにプロトコルはソフトウェア側で実装してやる必要があるのです。 例えばあなたがInteger変数にアクセスする時、それは一つのコマンドで行う事ができます。しかしそれは連続した手順によって一度に一ビットずつデータを取得しているだけなのです。ですから、その断片化したデータをくっつけてやる必要があります。

シリアル通信を直接行う事は難しいですが、仮実装されたコンポーネントを使用する事により簡単に行う事もできます。また、ハードウェア側ではそれすらできないので、多くの場合それを実行するのに特化した装置が必要であり、ICやマイクロコントローラを使用する必要があります。

ハードウェアアクセスプロトコルの簡潔な比較を以下に提示します。

速度 実装の難易度
シリアルポート 非常に遅い (< E5 bit/s) 普通
パラレルポート 遅い (~ E6 bit/s) 簡単
ISAカード 普通 (~ E7 bit/s) 普通
USB 普通 (~ E7 bit/s) 難しい
PCIカード 非常に速い (> E9 bit/s) 非常に難しい

パラレル通信

Windows上でinpout32.dllを使用する

Windowsではハードウェアデバイスへのアクセスを行う手順に9x系とNT系では違いがあります。9x系(95,98,Me)ではプログラムはDOSのように直接ハードウェアに対してアクセスすることができます。NT系(Windows NT,2000,XP)ではそのような方法は使えません。すべてのハードウェアポートはデバイスドライバによって通信する事ができます。これはセキュリティ上の問題からそうなっていますが、ドライバを作るためには時間とお金が必要で小さいプロジェクトは難しいでしょう。

しかし、幸せな事にこの問題を解決するライブラリがあります。WIndowsNTが検出されるとそれはHWInterface.sysカーネルデバイスドライバを解凍して、インストールします。Window9xが検出されると、それはハードウェアデバイスにアクセスするのに単にアセンブラオペコードを使用します。

でもそのライブラリをどうやって使ったらいいのでしょうか?簡単です!このライブラリはInp32とOut32という2つの関数しか持っていませんし、その使用は非常に直感的です。

ライブラリを動的にロードすると、関数は次のように定義されます

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

わたしたちはこれでライブラリをロードする事ができます。あなたのプログラムのメインフォームのOnCreateメソッドのような場所でこれを実行する事が可能です:

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;

もしOnCreateでライブラリをロードしたなら、OnDestroyでアンロードする事を忘れないで下さい:

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

これはInp32関数を使う単純な例です:

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

このコードはカスタムISAカードのポート$0200と、WindowsXP上のLazarus 0.9.10でテストされました。もちろんこのコードを実行するためにはuses節にWindowsが必要です。また、自分のアプリケーションと同じディレクトリに"input32.dll"も必要です。

このホームページのライブラリはこちら: www.logix4u.net/inpout32.htm

Linuxでiopermを使用する

Linuxでハードウェアにアクセスする最適な方法はThroughtデバイスドライバを使用することですが、ドライバを作成するタスクは複雑なので整理されたメソッドを使えば楽にそれを行うことができます。

あなたのLinuxプログラム上で "ports" ユニットを呼び出すためにはrootで実行する必要があり、またIOPermを呼び出すためにはポートにアクセスするための権限が必要です。 あなたは"ports"ユニットについての情報をここで手に入れることができます。

最初にやることは、(g)libcをリンクして、IOPermをコールすることです。すべての(g)libc(の関数)にリンクするユニットは、free pascal上にありますが、このユニットをアプリケーションから直接リンクすることは問題をひきおこします。(g)libcライブラリ全体を静的リンクをおこなう、ということはあまり良い方法とはいえません、なぜなら、それはバージョンの間で、しばしば変更されるからです。iopermといった関数はしかしながら、変更することはあまりないと思います。

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

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

[2]