function out = getwaveform(fid, source, first, last, navg) % % This function is used to capture and transfer data from a Tektronix TDS 220 % digital oscilloscope to a Matlab data structure using an RS-232 (com) port. % Inputs: % fid is pointing to the right serial I/O channel (use the function open_com.m) % source is a string containing either 'CH1' or 'CH2' % first is the first point to be transferred % last is the last point to be transferred % navg is the number of waveforms to be averaged (in the scope). % Ouput Structure: % out.time is an array containing the time values for each wavepoint. % out.wave is an array containing the waveform data. % % Copyright 2002, LJB Development, Inc. % written by Larry Busse, http://www.ljbdev.com % Software is provided without warranty. Use at your own risk. if nargin < 5, navg =1;end clear_com(fid); switch source case 'CH1', fprintf(fid,'DATA:SOURCE CH1;ENCDG SRI;WIDTH 2'); case 'CH2', fprintf(fid,'DATA:SOURCE CH2;ENCDG SRI;WIDTH 2'); otherwise, err('Problem with source'); end if navg == 1, fprintf(fid,'ACQUIRE:MODE SAMPLE'); else fprintf(fid,'ACQUIRE:MODE AVE'); msg = sprintf('ACQ:NUMAV %d',navg); fprintf(fid, msg); end msg=sprintf('data:start %d;stop %d;:HEADER OFF',first,last); fprintf(fid,msg); %fprintf(fid,'acquire:state stop'); %fprintf(fid,'acquire:state run'); i=0; while i < navg fprintf(fid,'acquire:numacq?'); line=fgetl(fid); i = sscanf(line,'%d'); end fprintf(fid,'CURVE?'); q=fread(fid,1,'char'); % should be the # character x=fread(fid,1,'int8'); ss=str2num(char(x)); y=fread(fid,ss,'uchar'); nbytes = str2num(sprintf('%s',y)); nwords = nbytes/2; data = fread(fid,nwords,'int16'); %[data,count] = fread(fid,10000,'uchar') %line = fgetl(fid); %data = sscanf(line,'%d,',inf); % clear_com(fid); qq=sprintf('WFMPRE:%s:NR_PT?',source);fprintf(fid,qq); npts=fscanf(fid,'%d'); % qq=sprintf('WFMPRE:%s:YOFF?',source);fprintf(fid,qq); yoff=fscanf(fid,'%f'); qq=sprintf('WFMPRE:%s:YMULT?',source);fprintf(fid,qq); ymult=fscanf(fid,'%f'); qq=sprintf('WFMPRE:%s:XINCR?',source);fprintf(fid,qq); xincr=fscanf(fid,'%f'); qq=sprintf('WFMPRE:%s:PT_OFF?',source);fprintf(fid,qq); ptoff=fscanf(fid,'%d'); qq=sprintf('WFMPRE:%s:XZERO?',source);fprintf(fid,qq); t0=fscanf(fid,'%f'); out.time = t0+([0:npts-1] - ptoff)*xincr; out.data = ymult*(data'-yoff); function clear_com(fid) % % Function used to clear any remaining data from a COMport. % % Copyright 2002, LJB Development, Inc. % written by Larry Busse, http://www.ljbdev.com % Software is provided without warranty. Use at your own risk. n=get(fid,'BytesAvailable'); if n>0 dummy = fread(fid,n); end