Ins and Outs of Oscilloscope Programming: Examples in C and Microsoft Visual Basic
Ins and Outs of Oscilloscope Programming: Examples in C and Microsoft Visual Basic
Ins and Outs of Oscilloscope Programming: Examples in C and Microsoft Visual Basic
%or further information on /.C4 or V./A# perform a *uic( search at the Agilent !echnologies !est and Measurement +e$ page for 9/.C4: or 9V./A: (+++ tm agilent com) -rogram %lo+ Most oscilloscope programs follo+ a standard model !his model is closel& related to ho+ an oscilloscope operates and resem$les the steps ta(en $& someone operating the oscilloscope from the front panel !o illustrate# this flo+chart sho+s a simplified programming model for oscilloscopes +hen data +ill $e do+nloaded to a computer for anal&sis6
Digitize Waveform
"O
!his model is not +ritten in concrete and can var& if the programmer doesn"t need to do+nload the +aveform data# for instance .n that case# less +or( is re*uired $ecause some steps can $e eliminated As &ou can see from the a$ove flo+ chart# this oscilloscope model is centered on the trigger event !he first part of an oscilloscope program is dedicated to preparing for the trigger# +hereas the second part is dedicated to utilizing the data captured in the oscilloscope"s memor& .t is important that there is some s&nchronization in place so that the program is a+are of +hen the oscilloscope has triggered and ac*uisition is complete !his is performed in the example programs $& means of the 6D.3itize command !he digitize command is designed to tell the oscilloscope to perform a single)shot ac*uisition But more than ;ust performing a single)shot ac*uisition +hich could have $een performed +ith a 6/.53le command# the 6D.3itize command prevents the oscilloscope from accepting further commands until the oscilloscope has triggered and filled memor& Careful planning is re*uired to ensure that the interface does not time out if a trigger is not found immediatel& /everal means exist for handling this situation li(e use of /0<s or increasing the interface"s timeout setting =o+ever# since handling timing events li(e this re*uires a length& explanation# it is $etter left to descriptions in the oscilloscope"s manual and the documentation on the interface/driver $eing used As previousl& stated# the a$ove model is $ased on a single)shot ac*uisition !his means the oscilloscope +ill simpl& stop after filling memor& .f the oscilloscope is given a 6085 command rather than a 6/.53le command or 6D.3itize command# the oscilloscope continuousl& updates =o+ever# unless the user +ants to vie+ the results on the oscilloscope"s displa&# it is not a trivial matter to (eep the program s&nchronized +ith a continuousl& changing data set !herefore# all examples included rel& on the single)shot ac*uisition mode At this ;uncture# it is pro$a$l& $est to start dealing +ith real examples to illustrate the principles mentioned a$ove !here are four oscilloscope program examples $elo+ !+o examples are in C and t+o are in Microsoft Visual Basic (VB) !he programs +ere developed to +or( +ith the .nfiniium and the >?@1A famil& of oscilloscopes from Agilent !echnologies /everal differences exist $et+een these oscilloscopes !here is also a significant difference $et+een ho+ C and VB deal +ith varia$le t&pes A discussion of these differences and hints on ho+ to +rite &our o+n customized program can $e found follo+ing the code Examples !he follo+ing examples are do+nloada$le in zip format %or the C programming examples# onl& the source code is included (console applications) %or the VB examples# the pro;ect +or(space is included and $est vie+ed $& opening it in Microsoft Visual Basic .f Microsoft VB is not loaded on the computer &ou are currentl& using# a text editor can $e used to loo( at the source code minus the 38. (contained in the formB frm form file and the ModuleB $as file) -roper configuration of the environment varia$les is
necessar& in $oth C and VB %or more information# see chapter 1 of the Agilent /.C4 8ser"s 3uide for ,indo+s titled 93etting /tarted +ith /.C4: >?@1A /eries 'scilloscope -rogram in C version @ C (generates scdata txt output) >?@1A /eries 'scilloscope -rogram in VB version @ C .nfiniium /eries 'scilloscope -rogram in C version @ C (generates scdata txt output) .nfiniium /eries 'scilloscope -rogram in VB version @ C De& -oints B !he .nfiniium oscilloscope supports a command for setting the memor& depth prior to capturing a +aveform (96AC<uire6-'.5ts EnF:) ,ith the .nfiniium# this is actuall& the num$er of points that +ill $e ac*uired !he >?@1A series also can accept a command re*uesting a certain num$er of +aveform points prior/after the 6D.3itize command# $ut +hat this does is compress the +aveform after it has $een ac*uired (96,AVeform6-'.5ts EnF:) !he .nfiniium saves the +aveform points in a signed integer format !he >?@1A series stores the +aveform points in unsigned or signed format !his is an important piece of information +hen programming in Visual Basic Visual Basic does not support an integer varia$le t&pe that is unsigned and B@ $its +ide /ince these examples utilize the ,'0D data format and the default is to store the data in unsigned format# the data from the >?@1A +as read into a $&te arra& in VB .t could $e argued that it +ould have $een easier to ;ust use the signed format from the oscilloscope# $ut since these examples are meant to illustrate concepts# unsigned +as used Calculating the voltage values is a $it more complicated since the +ords are stored as pairs of $&tes ,hen the oscilloscope digitizes a +aveform# t+o things are stored !hese are the pream$le and +aveform data !he pream$le has the scaling factors to translate the integers in the +aveform data to floating point voltage values !hese can $e *ueried individuall& or as an arra& Both methods are demonstrated in the examples ,hen using ,'0D format for the +aveform data# care has to $e ta(en to ensure that the data is stored in the correct $&te order !he 96,AVeform6BG!eorder E4/B%irst H M/B%irstF: command tells the oscilloscope ho+ &ou expect to receive the data /ome pointer t&pe casting is used in the C examples to allo+ the iread function to fill B@)$it +ide elements of an arra& =o+ever# great care should $e ta(en +hen performing an operation li(e pointer t&pe casting to ensure that ever& $it is dealt +ith correctl& /ee the program comments for more explanation
>
%or trou$leshooting an oscilloscope program# it is good to have a $asic understanding of de$ug tools +ithin the compiler Most compilers allo+ stepping through individual lines of code ,atching the oscilloscope as each command is sent can give a clue to +here a pro$lem is hiding in the program !here are times +hen the onl& indication of a $ad command is a small pop)up message on the oscilloscope"s screen stating 9.nvalid =eader: or something similar !his message t&picall& is missed $ecause it disappears too *uic(l& +hen executing the program at full speed ,hen +riting an oscilloscope program# al+a&s read the header of the $inar& $loc( +hen *uer&ing the +aveform data /ometimes the scope returns a different num$er of $&tes than +hat is expected in the earl& stages of program development ($efore $ugs are all +or(ed out)# ho+ever the header al+a&s specifies ho+ man& $&tes are to follo+ in the $loc( At times it seems a $it of extra +or( tr&ing to properl& process the header# $ut it is +ell +orth it .f it is not done# some of the $&tes from the header +ill inevita$l& $e read as part of the +aveform data and cause a great deal of confusion %or more information on $inar& $loc( format# refer to the oscilloscope"s programming guide (specificall& the 96,AVeform6DA!A7: *uer&) !he example programs +ere tested +ith a BC6B passive pro$e on channel B connected to the oscilloscope"s front)panel cali$ration signal !his meant there +as al+a&s a trigger event availa$le almost immediatel& after the 6D.3itize command +as sent =o+ever# that is often not the case in real +orld applications ./' interfaces have timeouts to prevent individual commands from holding up the interface indefinitel& !herefore# either the timeout has to $e increased or interrupt handling should $e used !his is application)dependent and re*uires a long discussion involving specifics of the instrument"s service re*uest register structure -lease refer to the oscilloscope"s programming guide for further details