unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DetectUnit, Buttons, StdCtrls, ExtCtrls, aiousb; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; EditA: TEdit; EditB: TEdit; WriteA: TButton; WriteB: TButton; outPanelB7: TPanel; outPanelA7: TPanel; outPanelA6: TPanel; outPanelA5: TPanel; outPanelA4: TPanel; outPanelA3: TPanel; outPanelA2: TPanel; outPanelA1: TPanel; outPanelB6: TPanel; outPanelB5: TPanel; outPanelB4: TPanel; outPanelB3: TPanel; outPanelB2: TPanel; outPanelB1: TPanel; inPanelA7: TPanel; inPanelA6: TPanel; inPanelA5: TPanel; inPanelA3: TPanel; inPanelA2: TPanel; inPanelA1: TPanel; inPanelB7: TPanel; inPanelB6: TPanel; inPanelB5: TPanel; inPanelB4: TPanel; inPanelB3: TPanel; inPanelB2: TPanel; inPanelB1: TPanel; GroupBox1: TGroupBox; Panel29: TPanel; Panel30: TPanel; outPanelA0: TPanel; outPanelB0: TPanel; inPanelB0: TPanel; inPanelA0: TPanel; inPanelA4: TPanel; Label5: TLabel; Label6: TLabel; instructions: TLabel; Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure WriteAClick(Sender: TObject); procedure OutPanelClick(Sender: TObject); procedure EditAKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure WriteBClick(Sender: TObject); procedure EditBKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Timer1Timer(Sender: TObject); protected OutPanel: array[0..15] of TPanel; InPanel: array[0..15] of TPanel; private { Private declarations } public { Public declarations } end; var Form1: TForm1; DeviceIndex : LONGWORD; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var DetectForm: TDetectForm; Status : DWord; outData : DWord; begin InPanel[ 0] := inPanelA0; InPanel[ 1] := inPanelA1; InPanel[ 2] := inPanelA2; InPanel[ 3] := inPanelA3; InPanel[ 4] := inPanelA4; InPanel[ 5] := inPanelA5; InPanel[ 6] := inPanelA6; InPanel[ 7] := inPanelA7; InPanel[ 8] := inPanelB0; InPanel[ 9] := inPanelB1; InPanel[10] := inPanelB2; InPanel[11] := inPanelB3; InPanel[12] := inPanelB4; InPanel[13] := inPanelB5; InPanel[14] := inPanelB6; InPanel[15] := inPanelB7; OutPanel[ 0] := OutPanelA0; OutPanel[ 1] := OutPanelA1; OutPanel[ 2] := OutPanelA2; OutPanel[ 3] := OutPanelA3; OutPanel[ 4] := OutPanelA4; OutPanel[ 5] := OutPanelA5; OutPanel[ 6] := OutPanelA6; OutPanel[ 7] := OutPanelA7; OutPanel[ 8] := OutPanelB0; OutPanel[ 9] := OutPanelB1; OutPanel[10] := OutPanelB2; OutPanel[11] := OutPanelB3; OutPanel[12] := OutPanelB4; OutPanel[13] := OutPanelB5; OutPanel[14] := OutPanelB6; OutPanel[15] := OutPanelB7; 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; outData := $FFFFFFFF; DIO_WriteAll(DeviceIndex, @outData); Instructions.Caption := 'Clicking on the "Write Port A" and "Write Port B" will write the values entered into the edit boxes to the relays'#10#13 + 'Clicking on the lights above the edit boxes will change the relays one at a time.' ; end; {********************************************** ** The following two procedures write a byte to the card ** based on the input the user puts in the edit boxes. ***********************************************} procedure TForm1.WriteAClick(Sender: TObject); var OutMask: Byte; I: Integer; begin OutMask := StrToInt('$' + EditA.Text); DIO_Write8(DeviceIndex, 0, OutMask); for I := 0 to 7 do begin if (OutMask and 1) <> 0 then OutPanel[I].Color := clLime else OutPanel[I].Color := clRed ; OutMask := OutMask shr 1; end; end; procedure TForm1.WriteBClick(Sender: TObject); var outMask: Byte; I: Integer; begin OutMask := StrToInt('$' + EditB.Text); DIO_Write8(DeviceIndex, 1, OutMask); for I := 8 to 15 do begin if (OutMask and 1) <> 0 then OutPanel[I].Color := clLime else OutPanel[I].Color := clRed ; OutMask := OutMask shr 1; end; end; {*************************************************** ** The following two procedures make 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 WriteAClick(nil) ; end; procedure TForm1.EditBKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_RETURN then WriteBClick(nil) ; end; procedure TForm1.OutPanelClick(Sender: TObject); var I: Integer; begin I := (Sender as TPanel).Tag; if OutPanel[I].Color = ClLime then begin DIO_Write1(DeviceIndex, I, False); OutPanel[I].Color := clRed; end else begin DIO_Write1(DeviceIndex, I, True); OutPanel[I].Color := clLime; end; end; procedure TForm1.Timer1Timer(Sender: TObject); var InMask: Byte; I: Integer; begin DIO_Read8(DeviceIndex, 2, @InMask); //2 is the byte index for the first read //valid values for this board are 2 and 3 for I := 0 to 7 do begin if (InMask and 1) = 1 then InPanel[I].Color := clLime else InPanel[I].Color := clRed ; InMask := InMask shr 1; end; DIO_Read8(DeviceIndex, 3, @InMask); for I := 8 to 15 do begin if (InMask and 1) = 1 then InPanel[I].Color := clLime else InPanel[I].Color := clRed ; InMask := InMask shr 1; end; end; end.