Sign in Join
Wiki copytemporary

NI-Scope examples: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: NI-Scope examples

OldNewLine
1+## INTRODUCTION
2+
3+National Instruments produce a wide range of 8-,12-,14,16-,24-bit Oscilloscopes/Digitizers cards in PCI, PCIe, PXI and PXIe form factor with sample rate up to 12.5Gs/s. [1](http://www.ni.com/digitizers/)
4+
5+This cards are supported by NI-Scope driver. Only Linux 32-bit and Windows 32/64 bit are supported.
6+
7+**NI Scope for Linux**
8+
9+Hardware are supported by Ni-Scope 3.1 for Linux [(download)](http://www.ni.com/download/ni-scope-3.1/1633/en/). This driver works only on Linux distributions with kernel 2.6.x RHEL 5.x, 6.x, Scientific Linux 5.x, 6.x and some old for example Mandrake 10.1. Read readme file to find list of supported hardware.
10+
11+To install NI-Scope 3.1 properly under these distributions make this trick
12+
13+```text
14+ Install NI-DAQmx 8.0.2 after NI-Scope.
15+```
16+
17+[(NI-DAQmx8.0.2 download)](http://www.ni.com/download/ni-daqmx-8.0.2/2322/en/)
18+
19+To check device presents in your system type **nilsdev** in terminal. Output should be like this
20+
21+```text
22+ NI PXI-5122: "Dev2"
23+ NI PXI-5122: "Dev1"
24+```
25+
26+Two PXI-5122 cards are present in the system with aliases Dev1 and Dev2.
27+
28+**NI-Scope for Windows**
29+
30+Windows OSes are supported by latest NI-Scope 14.1 driver [(download)](http://www.ni.com/download/ni-scope-14.1/5000/en/).
31+
32+Read readme to find list of supported hardware.
33+
34+Use NIMAX utility to setup device alias, for example Dev1.
35+
36+## Library documentation
37+
38+Your can view or download latest documentation here [2](http://digital.ni.com/manuals.nsf/websearch/06669B07C3B83BFD86257CE0005962FA). Alternately NI-Scope installation copy it to your PC, for example in Linux PC /usr/local/natinst/niscope/doc.
39+
40+## PASCAL bindings
41+
42+You can download bindings for your operation system from this forum thread [3](http://forum.lazarus.freepascal.org/index.php/topic,26658.15.html).
43+
44+To use it in your application copy bindings to your project directory and add **niscope.pas** to USES section.
45+
46+## Code Examples
47+
48+There are some small differences between Pascal bindings for Windows and Linux - sometimes same function parameter is variable in Linux, but in Windows is a pointer, for example in Linux somefunction(sampleRate:double):longint; in Windows somefunction(sampleRate:Pdouble):longint;
49+
50+### Linux examples
51+
52+#### Autosetuped acquisition
53+
54+In this example by calling niScope_AutoSetup driver automatically try to setup acquisition parameters.
55+
56+``` pascal
57+
58+program autosetupacq;
59+
60+{$mode objfpc}{$H+}
61+
62+uses
63+ {$IFDEF UNIX}{$IFDEF UseCThreads}
64+ cthreads,
65+ {$ENDIF}{$ENDIF}
66+ Classes, niscope, visatype,sysutils
67+ { you can add units after this };
68+var
69+ resourceName:string='Dev1';
70+ vi:ViSession=VI_NULL;
71+ wfmNum:ViInt32;
72+ actualRecordLength:dword;
73+ actualSampleRate:viReal64;
74+ data:array of viReal64;
75+ channel:string = '0';
76+ wfmInfo:array of niScope_wfmInfo;
77+ i:longint;
78+procedure CheckError(AErrorCode: ViStatus; Avi: viSession);
79+var
80+ errorMessage:string='';
81+ bufferSize:ViStatus;
82+begin
83+ SetLength(errorMessage,MAX_ERROR_DESCRIPTION);
84+ if (AErrorCode<>VI_SUCCESS) then begin
85+ //due on unkown string length niScope_GetErrorMessage should call twice
86+ //get buffer size
87+ bufferSize:=niScope_GetError(Avi,@AErrorCode,0,@errorMessage[1]);
88+ //read error message
89+ SetLength(errorMessage,bufferSize);
90+ niScope_GetError(Avi,@AErrorCode,Length(errorMessage),@errorMessage[1]);
91+ writeln(errorMessage);
92+ if Avi<>VI_NULL then begin
93+ niScope_close(Avi);
94+ end;
95+ Halt;
96+ end;
97+ end;
98+begin
99+ //Init Session
100+ CheckError(niScope_init(PChar(@resourceName[1]),NISCOPE_VAL_FALSE,NISCOPE_VAL_FALSE,@vi),vi);
101+ writeln('Vi handler value (should be 1):' ,vi);
102+ //auto setup measurement
103+ CheckError(niScope_AutoSetup(vi),vi);
104+ //read the actual record length and number of waveforms
105+ //setuped by autosetup
106+ CheckError(niScope_ActualRecordLength(vi,@actualRecordLength),vi);
107+ CheckError(niScope_SampleRate(vi,@actualSampleRate),vi);
108+ CheckError(niScope_ActualNumWfms(vi,PChar(@channel[1]),@wfmNum),vi);
109+
110+ //set buffer for data (lenght of data array)
111+ SetLength(data,actualRecordLength*wfmNum);
112+ SetLength(wfmInfo,wfmNum);
113+
114+ CheckError(niScope_Read(vi,PChar(@channel[1]),5.0,actualRecordLength,@data[0],@wfmInfo[0]),vi);
115+ //shows first 10 samples
116+ writeln('----first ten samples of record------------------');
117+ for i:=Low(data) to Low(data)+9 do writeln('sample[',i,'] = ',data[i]:4:2);
118+ //show Info
119+ writeln('---------------------------------------------------');
120+ writeln('Record lenght: ',actualRecordLength);
121+ writeln('Sample rate: ',actualSampleRate);
122+ writeln('Number of waveforms: ',wfmNum);
123+ //show Info from wfmInfo record
124+ writeln('Actual samples: ',wfmInfo[0].actualSamples);
125+ writeln('X Increament: ',wfmInfo[0].xIncrement);
126+ writeln('Gain: ',wfmInfo[0].Gain);
127+ //close Session
128+ if vi<>VI_NULL then niScope_close(vi)
129+end.
130+```
131+
132+#### Binary acquisition
133+
134+Use an appropriate **niScope_FetchBinary8** or **niScope_FetchBinary16** or **niScope_FetchBinary32** function passed to your card to get right signal samples.
135+
136+``` pascal
137+
138+program binaryacq;
139+
140+{$mode objfpc}{$H+}
141+
142+uses
143+ {$IFDEF UNIX}{$IFDEF UseCThreads}
144+ cthreads,
145+ {$ENDIF}{$ENDIF}
146+ Classes,niscope,visatype
147+ { you can add units after this };
148+ var
149+ error:ViStatus = VI_SUCCESS;
150+ vi:ViSession;
151+ //Variables setuped hardware
152+ resourceName:string = 'Dev1';
153+ channelName:string = '0,1'; //measurement channel(s)
154+ verticalRange:ViReal64 = 10;//measurement range -5 - 5 V
155+ verticalOffset:ViReal64 = 0.0;
156+ minSampleRate:ViReal64 = 2e5;
157+ minRecordLength:ViInt32 = 1000;
158+ binaryDataType:ViInt32;
159+ numWaveform:ViInt32;
160+ actualRecordLength:ViInt32;
161+ //Default values
162+ triggerType:ViInt32 = 0; //Immediate trigger
163+ triggerCoupling:ViInt32 = NISCOPE_VAL_DC;
164+ triggerSlope:ViInt32 = NISCOPE_VAL_POSITIVE;
165+ triggerLevel:ViReal64 = 0.0;
166+ triggerHoldoff:ViReal64 = 0.0;
167+ triggerDelay:ViReal64 = 0.0;
168+ triggerSource:string = '0';//nastavit kanal pro trigger
169+ refPosition:ViReal64 = 50.0;
170+ timeout:ViReal64 = 5.0;
171+ stop:ViInt32;
172+ i,j:ViInt32;
173+
174+ wfmInfo:array of niScope_wfmInfo;
175+ scaledWfm:array of ViReal64;
176+ //NI-5122 is 14-bit digitizer
177+ binaryWfm:array of ViInt16;
178+procedure CheckError(AErrorCode: ViStatus; Avi: viSession);
179+var
180+ errorMessage:string='';
181+ bufferSize:ViStatus;
182+begin
183+ SetLength(errorMessage,MAX_ERROR_DESCRIPTION);
184+ if (AErrorCode<>VI_SUCCESS) then begin
185+ //due on unkown string length niScope_GetErrorMessage should call twice
186+ //get buffer size
187+ bufferSize:=niScope_GetError(Avi,@AErrorCode,0,@errorMessage[1]);
188+ //read error message
189+ SetLength(errorMessage,bufferSize);
190+ niScope_GetError(Avi,@AErrorCode,Length(errorMessage),@errorMessage[1]);
191+ writeln(errorMessage);
192+ if Avi<>VI_NULL then begin
193+ niScope_close(Avi);
194+ end;
195+ Halt;
196+ end;
197+ end;
198+begin
199+ //Open the NI_SCOPE instrument handler
200+ CheckError(niScope_init(Pchar(@resourceName[1]),NISCOPE_VAL_FALSE,NISCOPE_VAL_FALSE,@vi),vi);
201+
202+ //Configure the vertical parameters
203+ CheckError(niScope_ConfigureVertical(vi,PChar(@channelName[1]),verticalRange,verticalOffset,NISCOPE_VAL_DC,1.0,NISCOPE_VAL_TRUE),vi);
204+ //Configure the horizontal parameters
205+ //one record
206+
207+ CheckError(niScope_ConfigureHorizontalTiming(vi,minSampleRate,minRecordLength,refPosition,1,VI_TRUE),vi);
208+ //Configure the trigger
209+
210+ if triggerType<>0 //Edge
211+ then CheckError(niScope_ConfigureTriggerEdge(vi,PChar(@triggerSource[1]),triggerLevel,triggerSlope,triggerCoupling,triggerHoldoff,triggerDelay),vi)
212+ else CheckError(niScope_ConfigureTriggerImmediate(vi),vi);
213+
214+ //Get number of waveforms
215+ CheckError(niScope_ActualNumWfms(vi,PChar(@channelName[1]),@numWaveform),vi);
216+
217+ //Get record length
218+ CheckError(niScope_ActualRecordLength(vi,@ActualRecordLength),vi);
219+ writeln('Record length: ',ActualRecordLength);
220+
221+ //Set buffer for data and waveform Info
222+ SetLength(binaryWfm,ActualRecordLength*numWaveform);
223+ SetLength(scaledWfm,ActualRecordLength*numWaveform);
224+ SetLength(wfmInfo,numWaveform);
225+
226+ //signal acquisition 1 record
227+ stop:=NISCOPE_VAL_FALSE;
228+ while stop<>NISCOPE_VAL_TRUE do begin
229+ CheckError(niScope_InitiateAcquisition(vi),vi);
230+ CheckError(niScope_FetchBinary16(vi,PChar(@channelName[1]),timeout,actualRecordLength,@binaryWfm[0],@wfmInfo[0]),vi);
231+ stop:=NISCOPE_VAL_TRUE;
232+ end;
233+ niScope_Close(vi);
234+ //calculate scaled data from binary data
235+ for i:=0 to NumWaveform-1 do
236+ for j:=0 to ActualRecordLength-1 do scaledWfm[j+(i*ActualRecordLength-1)]:=binaryWfm[j+(i*ActualRecordLength-1)]*wfmInfo[i].Gain+wfmInfo[i].offset;
237+
238+ for i:=0 to NumWaveform-1 do begin
239+ writeln(' Waveform channel ',i+1);
240+ writeln(' First 10 samples ');
241+ writeln('----------------------');
242+ for j:=0 to 10 do writeln('value[',j,'] = ',scaledWfm[j+(i*ActualRecordLength-1)]:4:2);
243+ writeln('----------------------');
244+ end;
245+ writeln('program stoped');
246+end.
247+```
248+
249+### Windows examples
250+
251+#### Autosetuped acquisition
252+
253+In this example by calling niScope_AutoSetup driver automatically try to setup acquisition parameters.
254+
255+``` pascal
256+
257+program autosetupacq;
258+
259+{$mode objfpc}{$H+}
260+
261+uses
262+ {$IFDEF UNIX}{$IFDEF UseCThreads}
263+ cthreads,
264+ {$ENDIF}{$ENDIF}
265+ Classes, niscope, visatype,sysutils
266+ { you can add units after this };
267+var
268+ resourceName:string='Dev3';
269+ vi:ViSession=VI_NULL;
270+ wfmNum:ViInt32;
271+ actualRecordLength:dword;
272+ actualSampleRate:viReal64;
273+ data:array of viReal64;
274+ channel:string = '0';
275+ wfmInfo:array of niScope_wfmInfo;
276+ i:longint;
277+procedure CheckError(AErrorCode: ViStatus; Avi: viSession);
278+var
279+ errorMessage:string;
280+ bufferSize:ViStatus;
281+begin
282+ SetLength(errorMessage,MAX_ERROR_DESCRIPTION);
283+ if (AErrorCode<>VI_SUCCESS) then begin
284+ //should call twice because we don't know errorMessage length
285+ //get string length
286+ bufferSize:=niScope_GetErrorMessage(Avi,AErrorCode,0,@errorMessage[1]);
287+ //get Error message
288+ SetLength(errorMessage,bufferSize);
289+ niScope_GetErrorMessage(Avi,AErrorCode,Length(errorMessage),@errorMessage[1]);
290+ writeln(errorMessage);
291+ if Avi<>VI_NULL then begin
292+ niScope_close(Avi);
293+ end;
294+ Halt;
295+ end;
296+ end;
297+begin
298+ //Init Session
299+ CheckError(niScope_init(PChar(@resourceName[1]),NISCOPE_VAL_FALSE,NISCOPE_VAL_FALSE,@vi),vi);
300+ writeln('Vi handler value (should be 1):' ,vi);
301+ //auto setup measurement
302+ CheckError(niScope_AutoSetup(vi),vi);
303+ //read the actual record length and number of waveforms
304+ //setuped by autosetup
305+ CheckError(niScope_ActualRecordLength(vi,@actualRecordLength),vi);
306+ CheckError(niScope_SampleRate(vi,@actualSampleRate),vi);
307+ CheckError(niScope_ActualNumWfms(vi,PChar(@channel[1]),@wfmNum),vi);
308+
309+ //set buffer for data (lenght of data array)
310+ SetLength(data,actualRecordLength*wfmNum);
311+ SetLength(wfmInfo,wfmNum);
312+
313+ CheckError(niScope_Read(vi,PChar(@channel[1]),5.0,actualRecordLength,@data[0],@wfmInfo[0]),vi);
314+ //shows first 10 samples
315+ writeln('----first ten samples of record------------------');
316+ for i:=Low(data) to Low(data)+9 do writeln('sample[',i,'] = ',data[i]:4:2);
317+ //show Info
318+ writeln('---------------------------------------------------');
319+ writeln('Record lenght: ',actualRecordLength);
320+ writeln('Sample rate: ',actualSampleRate);
321+ writeln('Number of waveforms: ',wfmNum);
322+ //show Info from wfmInfo record
323+ writeln('Actual samples: ',wfmInfo[0].actualSamples);
324+ writeln('X Increament: ',wfmInfo[0].xIncrement);
325+ writeln('Gain: ',wfmInfo[0].Gain);
326+ //close Session
327+ if vi<>VI_NULL then niScope_close(vi)
328+end.
329+```
330+
331+#### Binary acquisition
332+
333+Use an appropriate **niScope_FetchBinary8** or **niScope_FetchBinary16** or **niScope_FetchBinary32** function passed to your card to get right signal samples.
334+
335+``` pascal
336+program binaryacq;
337+
338+{$mode objfpc}{$H+}
339+
340+uses
341+ {$IFDEF UNIX}{$IFDEF UseCThreads}
342+ cthreads,
343+ {$ENDIF}{$ENDIF}
344+ Classes,niscope,visatype
345+ { you can add units after this };
346+ var
347+ error:ViStatus = VI_SUCCESS;
348+ vi:ViSession;
349+ //Variables setuped hardware
350+ resourceName:string = 'Dev1';
351+ channelName:string = '0,1'; //measurement channel(s)
352+ verticalRange:ViReal64 = 10;//measurement range -5 - 5 V
353+ verticalOffset:ViReal64 = 0.0;
354+ minSampleRate:ViReal64 = 2e5;
355+ minRecordLength:ViInt32 = 1000;
356+ binaryDataType:ViInt32;
357+ numWaveform:ViInt32;
358+ actualRecordLength:ViInt32;
359+ //Default values
360+ triggerType:ViInt32 = 0; //Immediate trigger
361+ triggerCoupling:ViInt32 = NISCOPE_VAL_DC;
362+ triggerSlope:ViInt32 = NISCOPE_VAL_POSITIVE;
363+ triggerLevel:ViReal64 = 0.0;
364+ triggerHoldoff:ViReal64 = 0.0;
365+ triggerDelay:ViReal64 = 0.0;
366+ triggerSource:string = '0';//nastavit kanal pro trigger
367+ refPosition:ViReal64 = 50.0;
368+ timeout:ViReal64 = 5.0;
369+ stop:ViInt32;
370+ i,j:ViInt32;
371+
372+ wfmInfo:array of niScope_wfmInfo;
373+ scaledWfm:array of ViReal64;
374+ //NI-5122 is 14-bit digitizer
375+ binaryWfm:array of ViInt16;
376+procedure CheckError(AErrorCode: ViStatus; Avi: viSession);
377+var
378+ errorMessage:string='';
379+ bufferSize:ViStatus;
380+begin
381+ SetLength(errorMessage,MAX_ERROR_DESCRIPTION);
382+ if (AErrorCode<>VI_SUCCESS) then begin
383+ //due on unkown string length niScope_GetErrorMessage should call twice
384+ //get buffer size
385+ bufferSize:=niScope_GetError(Avi,@AErrorCode,0,@errorMessage[1]);
386+ //read error message
387+ SetLength(errorMessage,bufferSize);
388+ niScope_GetError(Avi,@AErrorCode,Length(errorMessage),@errorMessage[1]);
389+ writeln(errorMessage);
390+ if Avi<>VI_NULL then begin
391+ niScope_close(Avi);
392+ end;
393+ Halt;
394+ end;
395+ end;
396+begin
397+ //Open the NI_SCOPE instrument handler
398+ CheckError(niScope_init(Pchar(@resourceName[1]),NISCOPE_VAL_FALSE,NISCOPE_VAL_FALSE,@vi),vi);
399+
400+ //Configure the vertical parameters
401+ CheckError(niScope_ConfigureVertical(vi,PChar(@channelName[1]),verticalRange,verticalOffset,NISCOPE_VAL_DC,1.0,NISCOPE_VAL_TRUE),vi);
402+ //Configure the horizontal parameters
403+ //one record
404+
405+ CheckError(niScope_ConfigureHorizontalTiming(vi,minSampleRate,minRecordLength,refPosition,1,VI_TRUE),vi);
406+ //Configure the trigger
407+
408+ if triggerType<>0 //Edge
409+ then CheckError(niScope_ConfigureTriggerEdge(vi,PChar(@triggerSource[1]),triggerLevel,triggerSlope,triggerCoupling,triggerHoldoff,triggerDelay),vi)
410+ else CheckError(niScope_ConfigureTriggerImmediate(vi),vi);
411+
412+ //Get number of waveforms
413+ CheckError(niScope_ActualNumWfms(vi,PChar(@channelName[1]),@numWaveform),vi);
414+
415+ //Get record length
416+ CheckError(niScope_ActualRecordLength(vi,@ActualRecordLength),vi);
417+ writeln('Record length: ',ActualRecordLength);
418+
419+ //Set buffer for data and waveform Info
420+ SetLength(binaryWfm,ActualRecordLength*numWaveform);
421+ SetLength(scaledWfm,ActualRecordLength*numWaveform);
422+ SetLength(wfmInfo,numWaveform);
423+
424+ //signal acquisition 1 record
425+ stop:=NISCOPE_VAL_FALSE;
426+ while stop<>NISCOPE_VAL_TRUE do begin
427+ CheckError(niScope_InitiateAcquisition(vi),vi);
428+ CheckError(niScope_FetchBinary16(vi,PChar(@channelName[1]),timeout,actualRecordLength,@binaryWfm[0],@wfmInfo[0]),vi);
429+ stop:=NISCOPE_VAL_TRUE;
430+ end;
431+ niScope_Close(vi);
432+ //calculate scaled data from binary data
433+ for i:=0 to NumWaveform-1 do
434+ for j:=0 to ActualRecordLength-1 do scaledWfm[j+(i*ActualRecordLength-1)]:=binaryWfm[j+(i*ActualRecordLength-1)]*wfmInfo[i].Gain+wfmInfo[i].offset;
435+
436+ for i:=0 to NumWaveform-1 do begin
437+ writeln(' Waveform channel ',i+1);
438+ writeln(' First 10 samples ');
439+ writeln('----------------------');
440+ for j:=0 to 10 do writeln('value[',j,'] = ',scaledWfm[j+(i*ActualRecordLength-1)]:4:2);
441+ writeln('----------------------');
442+ end;
443+ writeln('program stoped');
444+end.
445+```
446+
447+## TESTED HARDWARE
448+
449+- NI PXI-5122 tested successfully under Windows7 32-bit and Scientific Linux 6.x 32-bit.
450+
451+---
452+*From the Free Pascal wiki, page "NI-Scope examples", as it stood on 2015-05-18 after a change by Mig-31, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
453+Category: Hardware
454+Category: Measuring and Instrumentation

Back to history