Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

239 wiersze
10KB

  1. classdef Oscilloscope < Equipment
  2. %OSCILLOSCOPE is the classdef for a Oscilloscope
  3. % This oscilloscope object can contain multiple channels and trigger
  4. % settings.
  5. % EQUIPMENT is the parent class of OSCILLOSCOPE
  6. % This class contains objects for each channel and the trigger.
  7. % See also: TRIGGER, CHANNEL
  8. properties
  9. nchannels %number of channels the scope has
  10. horizontalPosition %horizontal position in seconds
  11. timescale %horizontal scale in seconds/div
  12. ch1 %channel 1 object
  13. ch2 %channel 2 object (empty if NCHANNELS < 2)
  14. ch3 %channel 3 object (empty if NCHANNELS < 3)
  15. ch4 %channel 4 object (empty if NCHANNELS < 4)
  16. trigger %trigger object
  17. end
  18. properties (Dependent)
  19. acquisition %acquisition setting (stop|run|single)
  20. end
  21. methods
  22. function sc = Oscilloscope(ipAddress,port,nchannels)
  23. %OSCILLOSCOPE Construct an instance of this class
  24. % This constructor creates a new scope object.
  25. sc@Equipment(ipAddress,port,-1); %make this object a child of the EQUIPMENT class
  26. sc.trigger = Trigger(sc); %create a trigger object
  27. sc.nchannels = min(nchannels,4); %Set number of channels with a maximum of 4.
  28. %more channels is possible but this requires more properties.
  29. for i = 1:nchannels %create a channel object for each channel
  30. sc.(['ch',num2str(i)]) = ChannelClass(sc,i);
  31. end
  32. end
  33. function clear(sc)
  34. %CLEAR is used to clear the oscilloscope.
  35. % closes the message on screen
  36. % sends the clear command to the scope and flushes the
  37. % TCP-buffer.
  38. sc.messageclose;
  39. sc.write_unsafe('*cls');
  40. flushinput(sc.tcp);
  41. end
  42. function s = get.acquisition(sc)
  43. %ACQUISITION returns the acquisition state from the scope.
  44. s = sc.query('ACQ:STAT?');
  45. end
  46. function run(sc)
  47. %RUN sets the ACQUISITION state to run.
  48. sc.write_noerror('RUN');
  49. end
  50. function single(sc)
  51. %SINGLE sets the ACQUISITION state to single
  52. %After running a single acquisition the scope will go to STOP
  53. sc.write_noerror('SING');
  54. end
  55. function stop(sc)
  56. %STOP sets the ACQUISITION state to stop.
  57. %Stops the signal acquisition imidiatly. Use SINGLE to run one
  58. %single waveform acquisition before stopping.
  59. %See also RUN, SINGLE
  60. sc.write_noerror('STOP');
  61. end
  62. function auto(sc)
  63. %AUTO sends the autoset command to the scope.
  64. %It should be noted that this function can result in aliassing
  65. %of the wave acquisition.
  66. sc.write_noerror('AUT');
  67. end
  68. function enable_channels(sc,channels)
  69. %ENABLE_CHANNELS enables all available channels on scope.
  70. if nargin < 2
  71. sc.write('CHAN:AON');
  72. else
  73. for i = channels
  74. sc.(['ch' num2str(i)]).enable;
  75. end
  76. end
  77. end
  78. function disable_channels(sc,channels)
  79. %DISABLE_CHANNELS disables all available channels on scope.
  80. if nargin < 2
  81. sc.write('CHAN:AOFF');
  82. else
  83. for i = channels
  84. sc.(['ch' num2str(i)]).disable;
  85. end
  86. end
  87. end
  88. function setMeasurement(sc,measurement,type,source1,source2)
  89. %SETMEASUREMENT sets the measurement slots on the scope.
  90. %SETMEASUREMENT(sc, measurement, type, source1 [,source2]);
  91. %sets one of the measurement place with 'measurement', this can
  92. %be a value in the range 1 till 4. 'source1' and 'source2' give
  93. %on what channel the measurement should be applied. This should
  94. %be done in de form of 'CH<m>' where <m> = 1..4. The default
  95. %settings are: source1 = 'CH1' and source2 = 'CH2'
  96. %With 'type' a measurement type can be selected. Some of the
  97. %measurements require at least 2 channels.
  98. %
  99. %SINGLE CHANNEL:
  100. % FREQuency | PERiod | PEAK | UPEakvalue | LPEakvalue |
  101. % PPCount | NPCount | RECount | FECount | HIGH | LOW |
  102. % AMPLitude | MEAN | RMS | RTIMe | FTIMe | PDCYcle |
  103. % NDCYcle | PPWidth | NPWidth | CYCMean | CYCRms |
  104. % STDDev | CYCStddev | BWIDth | POVershoot | NOVershoot
  105. %
  106. %DOUBLE CHANNEL:
  107. % DELay | PHASe
  108. prefix = ['MEAS',num2str(measurement),':']; %define the prefix for the SCPI command.
  109. %fprintf(num2str(nargin))
  110. %set the default values.
  111. if nargin < 5
  112. source2 = 'CH2';
  113. if nargin < 4
  114. source1 = 'CH1';
  115. end
  116. end
  117. source = [source1,', ',source2]; %set the sources suffix
  118. sc.write([prefix,'SOUR ',source]); %set the sources for the measurementposition
  119. sc.write([prefix,'MAIN ',type]); %set the measurementtype for the measurementposition
  120. end
  121. function m = getMeasurement(sc,measurement)
  122. %GETMEASUREMENT pulls the value from measurementposition.
  123. %m = GETMEASUREMENT(sc,measurement) will return the value that
  124. %was acquired from the measurementslot (given as
  125. %'measurement') as a double.
  126. m = str2double(sc.query(['MEAS',num2str(measurement),':RES:ACT?']));
  127. end
  128. function setWaveformSettings(sc,window,format,type)
  129. %SETWAVEFORMSETTINGS sets the settings to acquire the waveform
  130. %Before the waveform can be downloaded the aquisition settings
  131. %for the channels and/or scope must be set:
  132. %SETWAVEFORMSETTINGS(sc,window,format,type) where:
  133. %'window'
  134. % Set the acquisition window of the waveform. Possible settings
  135. % are: DEF (default option) | MAX | DMAX
  136. % See also CHANNELCLASS.WAVEFORM
  137. %
  138. %'format'
  139. % Set the dataformat of the data. ASCii|REAL (default option)|UINTeger
  140. %
  141. %'type'
  142. % Set resolution mode. SAMPle (default option)| PDETect | HRESolution
  143. if nargin < 4
  144. type = 'HRES';
  145. if nargin < 3
  146. format = 'REAL';
  147. if nargin < 2
  148. window = 'DEF';
  149. end
  150. end
  151. end
  152. sc.clear;
  153. sc.write(['CHAN:TYPE ',type]); %set resolution
  154. sc.write(['FORM ',format]); %set dataformat
  155. sc.write('FORM:BORD MSBF'); %set bitorder to Most Significant Bit First
  156. sc.write(['CHAN:DATA:POIN ',window]); %set window length
  157. sc.single; %run sigle acquisition
  158. end
  159. function data = readWaveform(sc,datalength)
  160. %READWAVEFORM download waveform from oscilloscope
  161. %Downloads the data from oscilloscope with BIN_READ_FLOAT.
  162. %Datastream is constructed as follows:
  163. % #41024<value1><value2>…<value n>
  164. %#4 = number of digits of the following number (= 4 in the example)
  165. %in the code these two are stored as 'prefixstring'.
  166. %1024 = number of following data bytes.
  167. %This breaks if there the TCP-buffer was not cleared before the
  168. %SCPI-command 'CHAN:DATA?' was send.
  169. prefixstring = fscanf(sc.tcp,'%c',2);%read the first 2 characters of the tcp-buffer and store in prefixstring.
  170. prefixlength = str2double(prefixstring(2));%change the second character of prefixstring to double. (this leaves the # behind)
  171. fscanf(sc.tcp,'%c',prefixlength);%read the number of characters equal to the number from the prefixlength.
  172. data = sc.bin_read_float(datalength); %run the function from the EQUIPMENTCLASS to download all the data.
  173. flushinput(sc.tcp);%after the download make sure that nothing is left behind in the tcpbuffer.
  174. end
  175. function data = waveform(sc,channels,window,type)
  176. %WAVEFORM downloads waveform of one or more channels.
  177. %data = WAVEFORM(sc,channels,window,type) returns a struct with
  178. %all waveformdata from the scope. This includes the sampletime
  179. %and data length.
  180. %'channels' is an array of all the channels that the function
  181. %should acquire.
  182. %'window' is the width of the data acquisition.
  183. %'type' is the resolution mode.
  184. %See also: SETWAVEFORMSETTINGS
  185. if nargin < 4
  186. type = 'HRES'; %default resolution is high resolution
  187. if nargin < 3
  188. window = 'DEF'; %default windowsize is DEFault
  189. if nargin < 2
  190. channels = 1:sc.nchannels; %default selection channels is all channels
  191. end
  192. end
  193. end
  194. sc.enable_channels(channels);%enable the channels for the acquisition
  195. sc.setWaveformSettings(window,'REAL',type);%Set the correct settings
  196. for i = channels
  197. curcha = ['ch',num2str(i)]; %make string for current channel
  198. wave = sc.(curcha).getWaveform; %get waveformdata from current channel
  199. data.(curcha) = wave.data; %store wavefromdata from wave-struct into data-struct.
  200. end
  201. data.sampletime = wave.sampletime; %together with the waveforms add sampletime and datalength to data-struct.
  202. data.length = wave.length;
  203. end
  204. function message(sc,msg)
  205. %MESSAGE Print message on screen of oscilloscope.
  206. sc.messageclose;
  207. sc.write(['DISP:DIAL:MESS ''',msg,''''])%send message
  208. end
  209. function messageclose(sc)
  210. %MESSAGECLOSE Close message on the screen of oscillscope.
  211. sc.opc; %wait on ready from scope.
  212. sc.write_unsafe('DISP:DIAL:CLOS'); %clear message
  213. end
  214. end
  215. end