Take4 Posted September 18, 2023 Share Posted September 18, 2023 (edited) from ctypes import * from dwfconstants import * import math import time import matplotlib.pyplot as plt import sys import numpy import csv if sys.platform.startswith("win"): dwf = cdll.dwf elif sys.platform.startswith("darwin"): dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf") else: dwf = cdll.LoadLibrary("libdwf.so") #--------declare ctype variables---------- hdwf = c_int() sts = c_byte() hzAcq = c_double(1000000) N = 262144 rgdSamples1 = (c_double*N)() rgdSamples2 = (c_double*N)() cAvailable = c_int() cLost = c_int() cCorrupted = c_int() fLost = 0 fCorrupted = 0 #-----------print(DWF version)--------------- version = create_string_buffer(16) dwf.FDwfGetVersion(version) print("DWF Version: "+str(version.value)) #---------------open device------------------- print("Opening first device") dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf)) if hdwf.value == hdwfNone.value: szerr = create_string_buffer(512) dwf.FDwfGetLastErrorMsg(szerr) print(str(szerr.value)) print("failed to open device") quit() #------------------------------------Waveform setting------------------------------------------ print("Generating sine wave...") dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_int(1)) dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcSine) dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(500)) dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(0.2)) dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1)) #---------------------------------set up for aquisition---------------------------------------- dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(-1), c_int(1)) dwf.FDwfAnalogInChannelCouplingSet(hdwf, c_int(-1), c_int(1)) dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord) dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq) dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(N/hzAcq.value)) # -1 infinite record length #wait at least 2 seconds for the offset to stabilize time.sleep(2) print("Starting oscilloscope") dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1)) cSamples = 0 Channel1_data = [] Channel2_data = [] #---------------------------------------------Start aquisition------------------------------------------------- while cSamples < N: dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts)) if cSamples == 0 and (sts == DwfStateConfig or sts == DwfStatePrefill or sts == DwfStateArmed) : # Acquisition not yet started. continue dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted)) cSamples += cLost.value if cLost.value : fLost = 1 if cCorrupted.value : fCorrupted = 1 if cAvailable.value==0 : continue if cSamples+cAvailable.value > N : cAvailable = c_int(N-cSamples) dwf.FDwfAnalogInStatusData(hdwf, c_int(0), byref(rgdSamples1, sizeof(c_double)*cSamples), cAvailable) # get channel 1 data dwf.FDwfAnalogInStatusData(hdwf, c_int(1), byref(rgdSamples2, sizeof(c_double)*cSamples), cAvailable) # get channel 2 data cSamples += cAvailable.value dwf.FDwfAnalogOutReset(hdwf, c_int(0)) dwf.FDwfDeviceCloseAll() print("Recording done") if fLost: print("Samples were lost! Reduce frequency") if fCorrupted: print("Samples could be corrupted! Reduce frequency") #aquiring data length data_length = len(rgdSamples1) #Storing data in arrays for i in range(data_length): Channel1_data.append(rgdSamples1[i]) Channel2_data.append(rgdSamples2[i]) #--------------------------stored in csv file------------------------------ with open('record.csv', mode = 'w', newline = '') as file: writer = csv.writer(file) for item1, item2 in zip(Channel1_data, Channel2_data): writer.writerow([item1, item2]) plt.plot(numpy.fromiter(rgdSamples1, dtype = numpy.float64)) plt.plot(numpy.fromiter(rgdSamples2, dtype = numpy.float64)) plt.show() The top waveform was obtained from the Waveform application and the bottom waveform was obtained using the Waveform SDK. Hello, I have a question regarding the behavior of WaveForm SDK when acquiring waveforms. I use an ADP3450 and I want to get the same waveforms in the WaveForms app and the WaveForm SDK, but this makes a difference. Is this a difference between the settings in the SDK and the application? Can someone please help me with a solution to this? I attach the program of my python and the screen shot of the setting for acquiring waveforms in the application. Below is a screen shot of the settings for waveform acquisition in the application. This is the entire WaveForms setup screen. And, this image is the setting for acquiring waveforms. Thank you. Edited September 18, 2023 by Take4 Link to comment Share on other sites More sharing options...
0 Guest Posted September 18, 2023 Share Posted September 18, 2023 (edited) Hi @Take4 It seems that the offset is the main difference in the data read by your Python script. What happens if you add an FdwfAnalogInChannelOffsetSet() for both channels? Also, consider using pydwf. It will make your life a lot easier going forward. To get a taste: here's the AnalogInRecordMode.py example supplied with pydwf. Edited September 18, 2023 by reddish Link to comment Share on other sites More sharing options...
0 attila Posted September 18, 2023 Share Posted September 18, 2023 Hi @Take4 The ADP3X50 input channels have two input ranges 2V and 50V. Configure dwf.FDwfAnalogInChannelRangeSet(hdwf, channel#, c_double(2.0)) With newer software version you don't have to complicate with Record mode, the default normal (acqmodeSingle) allows you to capture 128Mi samples, see AnalogIn_Trigger.py and other examples. The latest versions also provide device buffering, memory segmentation, low latency <1us for consecutive captures. Link to comment Share on other sites More sharing options...
0 Take4 Posted September 19, 2023 Author Share Posted September 19, 2023 Hi @attila Thank you for your help. I set analog input to 2V by dwf.FDwfAnalogInChannelRangeSet(hdwf, channel#, c_double(2.0)) and got the waveform below. The new version looks useful, I'll consider updating. One more question, can the sample mode be changed from the SDK? Thank you. Link to comment Share on other sites More sharing options...
0 Take4 Posted September 19, 2023 Author Share Posted September 19, 2023 Hi @reddish The dwf package seems very useful. I installed it. Thanks for the useful information. attila 1 Link to comment Share on other sites More sharing options...
Question
Take4
The top waveform was obtained from the Waveform application and the bottom waveform was obtained using the Waveform SDK.
Hello,
I have a question regarding the behavior of WaveForm SDK when acquiring waveforms.
I use an ADP3450 and I want to get the same waveforms in the WaveForms app and the WaveForm SDK, but this makes a difference.
Is this a difference between the settings in the SDK and the application?
Can someone please help me with a solution to this?
I attach the program of my python and the screen shot of the setting for acquiring waveforms in the application.
Below is a screen shot of the settings for waveform acquisition in the application.
This is the entire WaveForms setup screen.
And, this image is the setting for acquiring waveforms.
Thank you.
Edited by Take4Link to comment
Share on other sites
4 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now