{ LEGOスキャナー 光センサーの入力に Poll を使ってるので取り込みが非常に遅い... (^_^; } unit MainUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OleCtrls, SPIRITLib_TLB, StdCtrls, Buttons, ExtCtrls; const DspStepY = 4; type TForm1 = class(TForm) Spirit1: TSpirit; Button1: TButton; Button2: TButton; ScanBtn: TButton; StopBtn: TSpeedButton; Panel1: TPanel; Image1: TImage; Button3: TButton; StaticText1: TStaticText; StaticText2: TStaticText; StaticText3: TStaticText; procedure FormCreate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure ScanBtnClick(Sender: TObject); procedure StopBtnClick(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private 宣言 } AbortReq,CloseReq: Boolean; LineBuf: array [1..1000] of Integer; function GetPsVal: Integer; procedure GoLeftEnd; procedure GoRightEnd; function GetLineDat: Integer; procedure StepMove; public { Public 宣言 } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin CloseReq := False; Spirit1.InitComm; Spirit1.SetSensorType(0,1); //タッチセンサー設定 Spirit1.SetSensorType(2,1); Spirit1.SetSensorMode(0,1,0); Spirit1.SetSensorMode(2,1,0); Spirit1.SetSensorType(1,3); //光センサー設定 Spirit1.SetSensorMode(1,4,0); end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin AbortReq := True; CloseReq := True; Spirit1.Off('012'); //出力ポートオフ Spirit1.SetSensorType(1,0); //センサーオフ Spirit1.CloseComm; end; function TForm1.GetPsVal: Integer; begin Result := Spirit1.Poll(12,1); end; procedure TForm1.GoLeftEnd; begin AbortReq := False; Spirit1.SetFwd('0'); Spirit1.On_('0'); while (not AbortReq) and (not CloseReq) do begin Application.ProcessMessages; if Spirit1.Poll(12,2) < 1000 then Break; end; Spirit1.Off('0'); end; procedure TForm1.GoRightEnd; begin AbortReq := False; Spirit1.SetRwd('0'); Spirit1.On_('0'); while (not AbortReq) and (not CloseReq) do begin Application.ProcessMessages; if Spirit1.Poll(12,0) < 1000 then Break; end; Spirit1.Off('0'); end; procedure TForm1.Button1Click(Sender: TObject); begin GoLeftEnd; end; procedure TForm1.Button2Click(Sender: TObject); begin GoRightEnd; end; function TForm1.GetLineDat: Integer; var ct: Integer; begin GoLeftEnd; Spirit1.SetRwd('0'); Spirit1.On_('0'); ct := 0; while (not AbortReq) and (not CloseReq) do begin Application.ProcessMessages; if Spirit1.Poll(12,0) < 1000 then Break; if ct < High(LineBuf) then begin Inc(ct); LineBuf[ct] := GetPsVal; end else Break; end; Spirit1.Off('0'); Result := ct; end; procedure TForm1.StopBtnClick(Sender: TObject); begin AbortReq := True; end; procedure TForm1.StepMove; var t1,t2: Integer; begin Spirit1.On_('2'); t1 := GetTickCount; repeat t2 := GetTickCount; until (t2 < t1) or (t1+380 < t2); Spirit1.Off('2'); end; procedure TForm1.ScanBtnClick(Sender: TObject); var w,bx1,bx2,by,ct,i,j: Integer; begin Spirit1.SetRwd('2'); AbortReq := False; w := Image1.ClientWidth; by := 0; while not AbortReq do begin Application.ProcessMessages; ct := GetLineDat; if ct > 0 then begin bx1 := 0; for i := 1 to ct do begin bx2 := w*i div ct; with Image1.Canvas do begin j := Round((770-LineBuf[i])*2.2); Brush.Color := RGB(j,j,j); FillRect(Rect(bx1,by,bx2,by+DspStepY)); end; bx1 := bx2; end; StepMove; Inc(by,DspStepY); end; if by >= Image1.ClientHeight then Break; end; end; procedure TForm1.Button3Click(Sender: TObject); begin AbortReq := False; while not AbortReq do begin Application.ProcessMessages; StaticText1.Caption := Format('%4d',[Spirit1.Poll(12,0)]); StaticText2.Caption := Format('%4d',[Spirit1.Poll(12,1)]); StaticText3.Caption := Format('%4d',[Spirit1.Poll(12,2)]); end; end; end.