unit MainUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Gauges; const AM_TAKEDATA = WM_USER + $99; type TMainForm = class(TForm) Ch0Gauge: TGauge; MaxLabel: TLabel; MidLabel: TLabel; MinLabel: TLabel; Ch1Gauge: TGauge; Ch2Gauge: TGauge; StartButton: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Ch3Gauge: TGauge; Label4: TLabel; Ch0Label: TLabel; Ch1Label: TLabel; Ch2Label: TLabel; Ch3Label: TLabel; procedure StartButtonClick(Sender: TObject); procedure StopButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); private procedure AMTakeData(var Msg: TMessage); message AM_TAKEDATA; public { Public declarations } end; var MainForm: TMainForm; implementation {$R *.DFM} uses AIO16; var ChGauge: array[0..3] of TGauge; ChLabel: array[0..3] of TLabel; Buf: array[0..4096] of Single; DisTresh, DisCount: LongWord; Stopped: Boolean = True; DOff, DMul: Real; Rep: Boolean = True; function ErrorText(ErrorCode: LongWord): String; var WindowsError: PChar; begin FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER + FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrorCode, 0, PChar(@WindowsError), 0, nil); Result := WindowsError; LocalFree(Cardinal(WindowsError)); end; procedure TMainForm.FormCreate(Sender: TObject); var NumCards: LongWord; ADCBip, ADC5V: LongWord; Max, Min: Integer; Mid: Real; begin NumCards := AIO16_GetNumCards; if NumCards = 0 then begin Application.MessageBox( 'No 104-AIO16-16 found. If one is installed, please use the Add New Hardware wizard to install its driver.', 'Card Not Found', 0 ); Application.ShowMainForm := False; PostMessage(Handle, WM_CLOSE, 0, 0); Exit; end; AIO16_Init(0, nil); AIO16_GetSettings(0, nil, @ADCBip, @ADC5V, nil, nil); if ADC5V <> 0 then Max := 5 else Max := 10; if ADCBip <> 0 then Min := -Max else Min := 0; Mid := (Max + Min) / 2; MaxLabel.Caption := IntToStr(Max) + 'V'; MidLabel.Caption := FloatToStr(Mid) + 'V'; MinLabel.Caption := IntToStr(Min) + 'V'; DOff := -Min; DMul := 4096 / (Max - Min); ChGauge[0] := Ch0Gauge; ChGauge[1] := Ch1Gauge; ChGauge[2] := Ch2Gauge; ChGauge[3] := Ch3Gauge; ChLabel[0] := Ch0Label; ChLabel[1] := Ch1Label; ChLabel[2] := Ch2Label; ChLabel[3] := Ch3Label; end; procedure TMainForm.StartButtonClick(Sender: TObject); var Err, RateHz: LongWord; begin RateHz := 5000; Err := AIO16_ADC_Acquire(0, @RateHz, 0, 0, 3, 4096, 0, @Buf[0]); if Err <> ERROR_SUCCESS then begin Application.MessageBox( PChar('Error starting acquisition: ' + ErrorText(Err)), 'AIO16_ADC_Acquire Error', 0 ); Exit; end; DisTresh := LongWord(-4); DisCount := 0; Stopped := False; PostMessage(Handle, AM_TAKEDATA, 0, 0); StartButton.Caption := 'Stop Taking Data'; StartButton.OnClick := StopButtonClick; end; procedure TMainForm.AMTakeData(var Msg: TMessage); var dCt, Err, RateHz: LongWord; I: LongWord; begin if Stopped then Exit; dCt := AIO16_ADC_Read(0); if dCt <> 0 then begin Inc(DisCount, dCt); if DisCount - DisTresh >= 7 then begin DisTresh := (DisCount - 3) and $FFFFFFFC; for I := 0 to 3 do begin ChGauge[I].Visible := True; ChGauge[I].Progress := Round((Buf[DisTresh + I] + DOff) * DMul); ChLabel[I].Visible := True; ChLabel[I].Caption := Format('%.2f', [Buf[DisTresh + I]]); end; end; if DisCount >= 4096 then begin RateHz := 5000; Err := AIO16_ADC_Acquire(0, @RateHz, 0, 0, 3, 4096, 0, @Buf[0]); if Err <> ERROR_SUCCESS then begin Application.MessageBox( PChar('Error taking data: ' + ErrorText(Err)), 'AIO16_ADC_Acquire Error', 0 ); Exit; end; DisTresh := LongWord(-4); DisCount := 0; Stopped := False; end; end; if Rep then begin Application.ProcessMessages; PostMessage(Handle, AM_TAKEDATA, 0, 0); end; end; procedure TMainForm.StopButtonClick(Sender: TObject); var I: Integer; begin AIO16_ADC_Stop(0); Stopped := True; for I := 0 to 3 do begin ChGauge[I].Visible := False; ChLabel[I].Visible := False; end; StartButton.Caption := 'Take Data'; StartButton.OnClick := StartButtonClick; end; end.