ネタキリラボ
ネタキリラボ[ラボ]

Remote Stationサンプル

自作プログラムからRemote Stationを利用する

ここではDelphiからPC-OP-RS1-CONTROL.DLLを利用してRemote Stationを操作するサンプルを載せています。PC-OP-RS1-CONTROL.DLLに付属しているRubyサンプルを手本に書きました。

サンプルソース

PC-OP-RS1-CONTROL.DLL APIのラッパークラスを作成して利用しています。

フォーム

Remote Stationを接続したCOMポート番号を指定して「接続」後、「受信」ボタンを押してリモコンから信号を受信するとメモ欄にコードが表示されます。出力ポートを指定して「送信」を押すとリモコン信号が出力されます。

リモコンコード表記は、とりあえずコンマテキストの10進数にしましたが、ラッパーを少し弄れば、お好きなデータ形式に出来ると思います。

フォーム

COMポート番号が分からない場合は「デバイスマネージャー」の「ポート (COM と LPT)」を参照してください。

デバイスマネージャー

リスト1 サンプルプログラム

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RemoteStation, ComCtrls, StdCtrls, Spin, ExtCtrls;

type
  TForm1 = class(TForm)
    SpinEdit1: TSpinEdit;
    Label1: TLabel;
    Button1: TButton;
    Memo1: TMemo;
    StatusBar1: TStatusBar;
    Button2: TButton;
    Button3: TButton;
    SpinEdit2: TSpinEdit;
    Button4: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
    RS: TRemoteStation;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}                                               

procedure TForm1.FormCreate(Sender: TObject);
var
  maj, min: Cardinal;
begin
  RS := TRemoteStation.Create;
  RS.GetModuleVersion(maj, min);
  StatusBar1.SimpleText :=  Format('DLLバージョン : %d.%d', [maj, min]);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  RS.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if RS.Open(SpinEdit1.Value) then
    StatusBar1.SimpleText := '接続成功'
  else
    StatusBar1.SimpleText := '接続失敗';
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  if RS.Transmit(SpinEdit2.Value, Memo1.Text) then
    StatusBar1.SimpleText := '送信成功'
  else
    StatusBar1.SimpleText := '送信失敗';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if RS.Led then
    StatusBar1.SimpleText := 'コール成功'
  else
    StatusBar1.SimpleText := 'コール失敗';
end;

procedure TForm1.Button3Click(Sender: TObject);
var s: string;
begin           
  RS.SetTimeOut(10000);
  if RS.Receive(s) then
    StatusBar1.SimpleText := '受信成功'
  else
    StatusBar1.SimpleText := '受信失敗';
  Memo1.Text := s;
end;

end.

リスト2 DLLのラッパークラス

unit RemoteStation;

interface

uses SysUtils, Windows, Classes;

type
  TRemoteStation = class
  public
    destructor Destroy; override;
    function Open(PortNo: Integer): Boolean;
    function Close: Boolean;
    function Led: Boolean;
    function Receive(var RemoconCode: string): Boolean;
    function SetTimeOut(TimeoutTime: Cardinal): Boolean;
    function Transmit(Ch: Integer; RemoconCode: string): Boolean;
    function GetModuleVersion(var MajorVer: Cardinal; var 
      MinerVer: Cardinal): Boolean;
  end;

implementation

{//////////////////////////////////////////
PC-OP-RS1-CONTROL.DLL APIリファレンス

Project C3
http://hp.vector.co.jp/authors/VA007110/
/////////////////////////////////////////}
const
  RET_OK = 0;
  RET_ERR = 1;    
  RET_TIMEOUT = 2;

//ドライバのオープンを行う
function Open(port_no: Integer): Integer; 
  cdecl; external 'pc-op-rs1-control.dll';
//ドライバのクローズを行う
function Close: Integer; 
  cdecl; external 'pc-op-rs1-control.dll';
//PC-OP-RS1との送受信をする際のタイムアウト時間を設定する
function SetTimeOut(tmo_milliseconds: Cardinal): Integer; 
  cdecl; external 'pc-op-rs1-control.dll';
//PC-OP-RS1でリモコンデータを受信する
function Receive(premocon_code: PChar): Integer; 
  cdecl; varargs; external 'pc-op-rs1-control.dll';
//PC-OP-RS1からリモコンデータを送信する
function Transmit(ch: Integer; premocon_code: PChar): Integer; 
  cdecl; external 'pc-op-rs1-control.dll';
//PC-OP-RS1のACCESS LEDの点灯を行う
function LedFlash: Integer; 
  cdecl; external 'pc-op-rs1-control.dll';
//DLLのバージョンを取得する
function GetModuleVersion(var major_version: Cardinal; 
  var minor_version: Cardinal): Integer; cdecl; external 'pc-op-rs1-control.dll';

{ TRemoteStation }

function TRemoteStation.Open(PortNo: Integer): Boolean;
begin
  Result := RemoteStation.Open(PortNo) = RET_OK;
end;

destructor TRemoteStation.Destroy;
begin
   Close;
  inherited;
end;

function TRemoteStation.GetModuleVersion(var MajorVer,
  MinerVer: Cardinal): Boolean;
begin
  Result := (RemoteStation.GetModuleVersion(MajorVer, MinerVer) = RET_OK);
end;

function TRemoteStation.Led: Boolean;
begin
  Result := LedFlash = RET_OK;
end;

function TRemoteStation.Receive(var RemoconCode: string): Boolean;
var
  Buf: array[0..239] of char;
  i: Integer;
begin
  RemoconCode := '';
  Result := RemoteStation.Receive(Buf) = RET_OK;
  if not Result then Exit;
  for i := Low(Buf) to High(Buf) do begin
    RemoconCode := RemoconCode + IntToStr(Ord(Buf[i]));
    if i <> High(Buf) then RemoconCode := RemoconCode + ',';
  end;
end;

function TRemoteStation.SetTimeOut(TimeoutTime: Cardinal): Boolean;
begin
  Result := RemoteStation.SetTimeOut(TimeoutTime) = RET_OK;
end;

function TRemoteStation.Transmit(Ch: Integer;
  RemoconCode: string): Boolean;
var s: string;
  SL: TStringList;
  i: Integer;
begin
  SL := TStringList.Create;
  try
    SL.CommaText := RemoconCode;
    s := '';
    for i := 0 to SL.Count-1 do begin
      s := s + Char(StrToInt(SL[i]));
    end;
    Result := RemoteStation.Transmit(Ch, PChar(s)) = RET_OK;
  finally
    SL.Free;
  end;
end;

function TRemoteStation.Close: Boolean;
begin
  Result := RemoteStation.Close = RET_OK;
end;

end.

ダウンロード

ソースファイル [Down]
DLLダウンロード Project C3 Wiki


Copyright 2003-2010 yhira All Rights Reserved`
[Server] [Delphi Tips]
最終更新:2009/12/16 18:13:09