DIGILENTのWaveform SDKについての質問

Python

1from ctypes import *2from dwfconstants import *3import math 4import time 5import matplotlib.pyplot as plt 6import sys 7import numpy 8import csv 9 10if sys.platform.startswith("win"):11 dwf = cdll.dwf 12elif sys.platform.startswith("darwin"):13 dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")14else:15 dwf = cdll.LoadLibrary("libdwf.so")16 17#--------declare ctype variables----------18hdwf = c_int()19sts = c_byte()20hzAcq = c_double(1000000)21N = 26214422rgdSamples1 = (c_double*N)()23rgdSamples2 = (c_double*N)()24cAvailable = c_int()25cLost = c_int()26cCorrupted = c_int()27fLost = 028fCorrupted = 029 30#-----------print(DWF version)---------------31version = create_string_buffer(16)32dwf.FDwfGetVersion(version)33print("DWF Version: "+str(version.value))34 35#---------------open device-------------------36print("Opening first device")37dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))38 39if hdwf.value == hdwfNone.value:40 szerr = create_string_buffer(512)41 dwf.FDwfGetLastErrorMsg(szerr)42 print(str(szerr.value))43 print("failed to open device")44 quit()45 46#------------------------------------Waveform setting------------------------------------------47print("Generating sine wave...")48dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_int(1))49dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcSine)50dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(500))51dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(0.2))52dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))53 54#---------------------------------set up for aquisition----------------------------------------55dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(-1), c_int(1))56dwf.FDwfAnalogInChannelCouplingSet(hdwf, c_int(-1), c_int(1))57dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)58dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)59dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(N/hzAcq.value)) # -1 infinite record length60 61#wait at least 2 seconds for the offset to stabilize62time.sleep(2)63 64print("Starting oscilloscope")65dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))66 67cSamples = 068 69Channel1_data = []70Channel2_data = []71 72#---------------------------------------------Start aquisition-------------------------------------------------73while cSamples < N:74 dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))75 if cSamples == 0 and (sts == DwfStateConfig or sts == DwfStatePrefill or sts == DwfStateArmed) :76 # Acquisition not yet started.77 continue78 79 dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))80 81 cSamples += cLost.value 82 83 if cLost.value :84 fLost = 185 if cCorrupted.value :86 fCorrupted = 187 88 if cAvailable.value==0 :89 continue90 91 if cSamples+cAvailable.value > N :92 cAvailable = c_int(N-cSamples)93 94 dwf.FDwfAnalogInStatusData(hdwf, c_int(0), byref(rgdSamples1, sizeof(c_double)*cSamples), cAvailable) # get channel 1 data95 dwf.FDwfAnalogInStatusData(hdwf, c_int(1), byref(rgdSamples2, sizeof(c_double)*cSamples), cAvailable) # get channel 2 data96 cSamples += cAvailable.value 97 98dwf.FDwfAnalogOutReset(hdwf, c_int(0))99dwf.FDwfDeviceCloseAll()100 101print("Recording done")102if fLost:103 print("Samples were lost! Reduce frequency")104if fCorrupted:105 print("Samples could be corrupted! Reduce frequency")106 107#aquiring data length108data_length = len(rgdSamples1)109 110#Storing data in arrays111for i in range(data_length):112 Channel1_data.append(rgdSamples1[i])113 Channel2_data.append(rgdSamples2[i])114 115#--------------------------stored in csv file------------------------------116with open('record.csv', mode = 'w', newline = '') as file:117 writer = csv.writer(file)118 for item1, item2 in zip(Channel1_data, Channel2_data):119 writer.writerow([item1, item2])120 121plt.plot(numpy.fromiter(rgdSamples1, dtype = numpy.float64))122plt.plot(numpy.fromiter(rgdSamples2, dtype = numpy.float64))123plt.show()

コメントを投稿

0 コメント