unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, StrUtils,
TAGraph, TASeries, visa_dyn, session, visatype,
TADrawUtils, TACustomSeries;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Chart1: TChart;
Chart1LineSeries1: TLineSeries;
Label1: TLabel;
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 Wait(Duration: QWord);
var
StartTick: QWord;
begin
StartTick := GetTickCount64;
repeat
Application.Processmessages;
until GetTickCount64 >= (StartTick + Duration);
end;
procedure PlotWaveForm(WaveformData: string);
var
i: integer;
plotdata: array of double;
dt : double = 10/1000;
plotindex: integer = -1;
numstring: string ='';
DataOffset: integer = 0;
begin
form1.Chart1LineSeries1.Clear;
form1.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)) + 2;
SetLength(plotdata,Length(WaveformData)-DataOffset);
plotindex := -1;
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]);
//wait(1); //TAChart does NOT crash if I enable this line, but execution is extremely slow. Even if Application.Processmessages; within Wait is commented, the app does NOT crash.
form1.Label1.Caption := IntToStr(i);
numstring := numstring + FloatToStr(plotdata[plotindex]) +',0,'+ #13#10;
end; //for
form1.Label1.Caption := 'Ready';
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;
if not LoadVisaLib then
begin
ShowMessage ('CannotLoadVisaLib');
exit;
end;
//VisaSession.Address := 'USB0::0x0699::......'; //Or set it as a property of the control
VisaSession.Active := true;
VisaSession.EnableEventHandler(@VisaEventHandler);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
myfs: TFormatSettings;
dt : double = 10/1000;
begin
Label1.Caption := '';
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');
Wait(50);
VisaSession.Command('*SRE 16');
VisaSession.Command('*STB?'); // Handler should be called now since output buffer gets filled
Wait(50);
Label1.Caption := 'Waiting for a waveform...';
VisaSession.Command('*OPC');
Wait(50);
WaitWaveform:= True;
VisaSession.Command('*OPC?'); //Waits for the trigger
end;
end.