unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DetectUnit, Buttons, StdCtrls, ExtCtrls, aiousb; type TForm1 = class(TForm) Label1: TLabel; Label3: TLabel; EditA: TEdit; WriteButton: TButton; outPanelA3: TPanel; outPanelA2: TPanel; outPanelA1: TPanel; inPanelA7: TPanel; inPanelA6: TPanel; inPanelA5: TPanel; inPanelA3: TPanel; inPanelA2: TPanel; inPanelA1: TPanel; GroupBox1: TGroupBox; Panel29: TPanel; Panel30: TPanel; outPanelA0: TPanel; inPanelA0: TPanel; inPanelA4: TPanel; Label5: TLabel; Label6: TLabel; instructions: TLabel; PollTimer: TTimer; procedure FormCreate(Sender: TObject); procedure WriteButtonClick(Sender: TObject); procedure outPanelA0Click(Sender: TObject); procedure EditAKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure PollTimerTimer(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; DeviceIndex : LONGWORD; implementation {$R *.DFM} var InPanel, OutPanel: array of TPanel; procedure TForm1.FormCreate(Sender: TObject); var DetectForm: TDetectForm; Status : DWord; outData : DWord; begin DeviceIndex := diOnly; Status := QueryDeviceInfo(DeviceIndex, nil, nil, nil, nil, nil); if Status <> ERROR_SUCCESS then begin DetectForm := TDetectForm.Create(Self); if DetectForm.ShowModal = mrOK then begin DeviceIndex := DetectForm.DeviceIndex; DetectForm.Free; end else begin DetectForm.Free; Application.Terminate; Exit; end; end; SetLength(InPanel, 8); InPanel[0] := InPanelA0; InPanel[1] := InPanelA1; InPanel[2] := InPanelA2; InPanel[3] := InPanelA3; InPanel[4] := InPanelA4; InPanel[5] := InPanelA5; InPanel[6] := InPanelA6; InPanel[7] := InPanelA7; SetLength(OutPanel, 4); OutPanel[0] := OutPanelA0; OutPanel[1] := OutPanelA1; OutPanel[2] := OutPanelA2; OutPanel[3] := OutPanelA3; outData := 0; DIO_WriteAll(DeviceIndex, @outData); PollTimer.Enabled := True; end; {********************************************** ** The following procedure writes a byte to the card ** based on the input the user puts in the edit box. ***********************************************} procedure TForm1.WriteButtonClick(Sender: TObject); var outMask: Byte; BitIndex: Integer; begin outMask := StrToInt('$' + EditA.Text); DIO_Write8(DeviceIndex, 0, outMask); for BitIndex := 0 to 3 do if (OutMask and (1 shl BitIndex)) <> 0 then OutPanel[BitIndex].Color := clLime else OutPanel[BitIndex].Color := clRed ; end; {*************************************************** ** The following procedure makes it so that when the ** user presses enter in the edit box the effect is the ** same as clicking on the appropriate write button ***************************************************} procedure TForm1.EditAKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_RETURN then WriteButtonClick(nil) ; end; {**************************************************** ** The following are all the click event handlers for ** the individual output bits. Every one follows the same ** basic pattern *****************************************************} procedure TForm1.outPanelA0Click(Sender: TObject); var BitIndex: Integer; begin //the panels' tags have been set to the indices of their //bits, so we get the bit index from the tag BitIndex := TPanel(Sender).Tag; //valid output bit indices for this board are 0 through 3 if OutPanel[BitIndex].Color = clLime then begin OutPanel[BitIndex].Color := clRed; DIO_Write1(DeviceIndex, BitIndex, False); end else begin OutPanel[BitIndex].Color := clLime; DIO_Write1(DeviceIndex, BitIndex, True); end; end; procedure TForm1.PollTimerTimer(Sender: TObject); var BitIndex: Integer; InMask: Byte; begin //1 is the byte index for the read DIO_Read8(DeviceIndex, 1, @InMask); //valid values for this board are //0 (to read back the outputs) and //1 (to read the inputs) for BitIndex := 0 to 7 do if (InMask and (1 shl BitIndex)) <> 0 then InPanel[BitIndex].Color := clLime else InPanel[BitIndex].Color := clRed ; end; end.