unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, Grids,
StrUtils, visa_dyn, session, visatype;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
StringGrid1: TStringGrid;
VisaSession: TVisaSession;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
WaitWaveform: boolean;
function VisaEventHandler(vi: ViSession; eventType: ViEventType; event: ViEvent; userHandle: ViAddr): ViStatus; stdcall;
implementation
{$R *.lfm}
{ TForm1 }
procedure PlotWaveForm(WaveformData: string);
var
i: integer;
plotdata: array of double;
dt : double = 10/1000;
plotindex: integer = -1;
numstring: string ='';
DataOffset: integer = 0;
begin
with Form1 do
begin
Label1.Caption := format('Plotting the waveform...%d samples received',[Length(WaveformData)]);
if LeftStr(WaveformData,1) <> '#' then
begin
form1.Label1.Caption := 'Received data is invalid';
exit;
end;
DataOffset := StrToInt(midstr(WaveformData,2,1)) + 3;
SetLength(plotdata,Length(WaveformData)-DataOffset);
plotindex := -1;
// StringGrid1.BeginUpdate; //This causes no improvement
StringGrid1.RowCount := high(WaveformData);
for i:=DataOffset to high(WaveformData)-1 do //Last value is always #10 and it is not a part of the data.
begin
inc(plotindex);
plotdata[plotindex] := Int8(WaveformData[i]);
// form1.Chart1LineSeries1.AddXY(plotindex,plotdata[plotindex]);
StringGrid1.Cells[0,plotindex] := inttostr(plotindex);
StringGrid1.Cells[1,plotindex] := floatToStr(plotdata[plotindex]);
form1.Label1.Caption := IntToStr(i);
sleep(1);
numstring := numstring + FloatToStr(plotdata[plotindex]) +',0,'+ #13#10;
end; //for
//StringGrid1.EndUpdate;
form1.Label1.Caption := 'Ready';
Button1.Enabled := true;
end; //with
end;
function VisaEventHandler(vi: ViSession; eventType: ViEventType; event: ViEvent; userHandle: ViAddr): ViStatus; stdcall;
var
WaveformData: AnsiString = '';
begin
if (WaitWaveform = true) then
begin
WaitWaveform := false;
WaveformData := Form1.VisaSession.query('CURV?');
PlotWaveForm(WaveformData);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WaitWaveform := false;
StringGrid1.FixedCols :=0;
StringGrid1.FixedRows :=0;
VisaSession.Active := true;
VisaSession.EnableEventHandler(@VisaEventHandler);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
myfs: TFormatSettings;
dt : double = 10/1000;
begin
Label1.Caption := '';
Button1.Enabled := false;
WaitWaveform:= false;
myfs.DecimalSeparator := '.';
VisaSession.Command('*RST;');
VisaSession.Command('*CLS;:HEADER OFF;:WFMO:ENC BIN'); //Default setup
// VisaSession.Command('CH1:POS -2.500000'); //Vertical offset
VisaSession.Command(':SEL:CH1 ON;:CH1:PRO:GAIN 1.00000;:CH1:COUP DC;SCA 0.500000;OFFS 0.000000;BAN TWE;DESK 0.000000;');
// probe 1X v/div
VisaSession.Command(':ACQ:STOPA SEQ'); //':ACQ:STOPA RUNST' //Disable/enable continuous acquisition
VisaSession.Command(':ACQ:MOD SAM;'); //Configures the common properties of the acquisition subsystem.
// //# samples
VisaSession.Command(':HOR:RECO 20000;:HOR:SEC ' + FormatFloat('0.000000',dt,myfs) + ';:HOR:DEL:MOD OFF;POS 2.000000'); //Configures the record properties of the acquisition subsystem.
VisaSession.Command(':TRIG:A:EDGE:SOU CH1;:TRIG:A:LEV 0.500000;:TRIG:A:EDGE:SLO RISE;:TRIG:A:EDGE:COUP DC;'); //Set the trigger and wait until it is triggerred
VisaSession.Command(':DATA:SOU CH1;:DATA:START 1.000000;:DATA:STOP 5000000.000000;'); //probably this sets the buffer size
VisaSession.Command('ACQUIRE:STATE OFF;ACQuire:STOPAfter SEQuence;SELECT:CH1 ON');
//
VisaSession.Command('DESE 1'); //Set registers to await an Operation Complete (OPC) event (bit 1) in the event queue. This event is summarized in the Event Status Bit(ESB) of the Status Byte Register.
VisaSession.Command('*ESE 1'); //Set the Event Status Bit (bit 5) to await a Service Request (SRQ)
VisaSession.Command('*SRE 0'); //Clear the event registers
VisaSession.Command('ACQUIRE:STATE ON');
sleep(50);//Wait(50);
VisaSession.Command('*SRE 16');
VisaSession.Command('*STB?'); // Handler should be called now since output buffer gets filled
sleep(50);//Wait(50);
Label1.Caption := 'Waiting for a waveform...';
VisaSession.Command('*OPC');
sleep(50);// Wait(50);
WaitWaveform:= True;
VisaSession.Command('*OPC?'); //Waits for the trigger
end;
end.