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 outPanelA0Click(Sender: TObject); procedure outPanelA1Click(Sender: TObject); procedure outPanelA2Click(Sender: TObject); procedure outPanelA3Click(Sender: TObject); procedure outPanelA4Click(Sender: TObject); procedure outPanelA5Click(Sender: TObject); procedure outPanelA6Click(Sender: TObject); procedure outPanelA7Click(Sender: TObject); procedure outPanelB0Click(Sender: TObject); procedure outPanelB1Click(Sender: TObject); procedure outPanelB2Click(Sender: TObject); procedure outPanelB3Click(Sender: TObject); procedure outPanelB4Click(Sender: TObject); procedure outPanelB5Click(Sender: TObject); procedure outPanelB6Click(Sender: TObject); procedure outPanelB7Click(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); 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 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; DIO_Configure(DeviceIndex, false, nil, nil); outData := 0; DIO_WriteAll(DeviceIndex, @outData); with instructions do begin 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; Caption := Caption + 'Clicking on the lights above the edit boxes will change the relays one at a time.'; end; 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; begin outMask := strtoint('$' + EditA.Text); DIO_Write8(DeviceIndex, 0, outMask); if outMask mod 2 = 1 then outPanelA0.Color := clLime else outPanelA0.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA1.Color := clLime else outPanelA1.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA2.Color := clLime else outPanelA2.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA3.Color := clLime else outPanelA3.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA4.Color := clLime else outPanelA4.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA5.Color := clLime else outPanelA5.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA6.Color := clLime else outPanelA6.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelA7.Color := clLime else outPanelA7.Color := clRed ; end; procedure TForm1.WriteBClick(Sender: TObject); var outMask : Byte; begin outMask := strtoint('$' + EditB.Text); DIO_Write8(DeviceIndex, 1, outMask); if outMask mod 2 = 1 then outPanelB0.Color := clLime else outPanelB0.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB1.Color := clLime else outPanelB1.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB2.Color := clLime else outPanelB2.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB3.Color := clLime else outPanelB3.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB4.Color := clLime else outPanelB4.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB5.Color := clLime else outPanelB5.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB6.Color := clLime else outPanelB6.Color := clRed ; outMask := outMask shr 1; if outMask mod 2 = 1 then outPanelB7.Color := clLime else outPanelB7.Color := clRed ; 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; {**************************************************** ** 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); begin if outPanelA0.Color = ClLime then begin DIO_Write1(DeviceIndex, 0, false); //0 is the bit index we are writing to outPanelA0.Color := clRed; //valid values output bits for this board end // are 0 through 15 else begin DIO_Write1(DeviceIndex, 0, true); outPanelA0.Color := clLime; end; end; procedure TForm1.outPanelA1Click(Sender: TObject); begin if outPanelA1.Color = ClLime then begin DIO_Write1(DeviceIndex, 1, false); outPanelA1.Color := clRed; end else begin DIO_Write1(DeviceIndex, 1, true); outPanelA1.Color := clLime; end; end; procedure TForm1.outPanelA2Click(Sender: TObject); begin if outPanelA2.Color = ClLime then begin DIO_Write1(DeviceIndex, 2, false); outPanelA2.Color := clRed; end else begin DIO_Write1(DeviceIndex, 2, true); outPanelA2.Color := clLime; end; end; procedure TForm1.outPanelA3Click(Sender: TObject); begin if outPanelA3.Color = ClLime then begin DIO_Write1(DeviceIndex, 3, false); outPanelA3.Color := clRed; end else begin DIO_Write1(DeviceIndex, 3, true); outPanelA3.Color := clLime; end; end; procedure TForm1.outPanelA4Click(Sender: TObject); begin if outPanelA4.Color = ClLime then begin DIO_Write1(DeviceIndex, 4, false); outPanelA4.Color := clRed; end else begin DIO_Write1(DeviceIndex, 4, true); outPanelA4.Color := clLime; end; end; procedure TForm1.outPanelA5Click(Sender: TObject); begin if outPanelA5.Color = ClLime then begin DIO_Write1(DeviceIndex, 5, false); outPanelA5.Color := clRed; end else begin DIO_Write1(DeviceIndex, 5, true); outPanelA5.Color := clLime; end; end; procedure TForm1.outPanelA6Click(Sender: TObject); begin if outPanelA6.Color = ClLime then begin DIO_Write1(DeviceIndex, 6, false); outPanelA6.Color := clRed; end else begin DIO_Write1(DeviceIndex, 6, true); outPanelA6.Color := clLime; end; end; procedure TForm1.outPanelA7Click(Sender: TObject); begin if outPanelA7.Color = ClLime then begin DIO_Write1(DeviceIndex, 7, false); outPanelA7.Color := clRed; end else begin DIO_Write1(DeviceIndex, 7, true); outPanelA7.Color := clLime; end; end; procedure TForm1.outPanelB0Click(Sender: TObject); begin if outPanelB0.Color = ClLime then begin DIO_Write1(DeviceIndex, 8, false); outPanelB0.Color := clRed; end else begin DIO_Write1(DeviceIndex, 8, true); outPanelB0.Color := clLime; end; end; procedure TForm1.outPanelB1Click(Sender: TObject); begin if outPanelB1.Color = ClLime then begin DIO_Write1(DeviceIndex, 9, false); outPanelB1.Color := clRed; end else begin DIO_Write1(DeviceIndex, 9, true); outPanelB1.Color := clLime; end; end; procedure TForm1.outPanelB2Click(Sender: TObject); begin if outPanelB2.Color = ClLime then begin DIO_Write1(DeviceIndex, 10, false); outPanelB2.Color := clRed; end else begin DIO_Write1(DeviceIndex, 10, true); outPanelB2.Color := clLime; end; end; procedure TForm1.outPanelB3Click(Sender: TObject); begin if outPanelB3.Color = ClLime then begin DIO_Write1(DeviceIndex, 11, false); outPanelB3.Color := clRed; end else begin DIO_Write1(DeviceIndex, 11, true); outPanelB3.Color := clLime; end; end; procedure TForm1.outPanelB4Click(Sender: TObject); begin if outPanelB4.Color = ClLime then begin DIO_Write1(DeviceIndex, 12, false); outPanelB4.Color := clRed; end else begin DIO_Write1(DeviceIndex, 12, true); outPanelB4.Color := clLime; end; end; procedure TForm1.outPanelB5Click(Sender: TObject); begin if outPanelB5.Color = ClLime then begin DIO_Write1(DeviceIndex, 13, false); outPanelB5.Color := clRed; end else begin DIO_Write1(DeviceIndex, 13, true); outPanelB5.Color := clLime; end; end; procedure TForm1.outPanelB6Click(Sender: TObject); begin if outPanelB6.Color = ClLime then begin DIO_Write1(DeviceIndex, 14, false); outPanelB6.Color := clRed; end else begin DIO_Write1(DeviceIndex, 14, true); outPanelB6.Color := clLime; end; end; procedure TForm1.outPanelB7Click(Sender: TObject); begin if outPanelB7.Color = ClLime then begin DIO_Write1(DeviceIndex, 15, false); outPanelB7.Color := clRed; end else begin DIO_Write1(DeviceIndex, 15, true); outPanelB7.Color := clLime; end; end; procedure TForm1.Timer1Timer(Sender: TObject); var inMask : Byte; begin DIO_Read8(DeviceIndex, 2, @inMask); //2 is the byte index for the read //valid values for this board are 2 and 3 if inMask mod 2 = 1 then inPanelA0.Color := clLime else inPanelA0.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA1.Color := clLime else inPanelA1.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA2.Color := clLime else inPanelA2.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA3.Color := clLime else inPanelA3.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA4.Color := clLime else inPanelA4.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA5.Color := clLime else inPanelA5.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA6.Color := clLime else inPanelA6.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelA7.Color := clLime else inPanelA7.Color := clRed ; DIO_Read8(DeviceIndex, 3, @inMask); if inMask mod 2 = 1 then inPanelB0.Color := clLime else inPanelB0.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB1.Color := clLime else inPanelB1.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB2.Color := clLime else inPanelB2.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB3.Color := clLime else inPanelB3.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB4.Color := clLime else inPanelB4.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB5.Color := clLime else inPanelB5.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB6.Color := clLime else inPanelB6.Color := clRed ; inMask := inMask shr 1; if inMask mod 2 = 1 then inPanelB7.Color := clLime else inPanelB7.Color := clRed ; end; end.