Skip to content
Snippets Groups Projects
Commit 3740aa09 authored by Christoph Schmidt's avatar Christoph Schmidt
Browse files

Drop files from .gitignore

parent 070f37b4
No related branches found
No related tags found
No related merge requests found
Showing
with 1992 additions and 0 deletions
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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")
hdwf = c_int()
sts = c_byte()
IsEnabled = c_int()
usbVoltage = c_double()
usbCurrent = c_double()
auxVoltage = c_double()
auxCurrent = c_double()
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
print("failed to open device")
quit()
# set up analog IO channel nodes
# enable positive supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(True))
# set voltage to 5 V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(1), c_double(5.0))
# enable negative supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(1), c_int(0), c_double(True))
# set voltage to -5 V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(1), c_int(1), c_double(-5.0))
# master enable
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True))
for i in range(1, 11):
# wait 1 second between readings
time.sleep(1)
# fetch analogIO status from device
if dwf.FDwfAnalogIOStatus(hdwf) == 0:
break
#supply monitor
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(0), byref(usbVoltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(1), byref(usbCurrent))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(3), c_int(0), byref(auxVoltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(3), c_int(1), byref(auxCurrent))
print("USB: " + str(round(usbVoltage.value,3)) + "V\t" + str(round(usbCurrent.value,3)) + "A")
print("AUX: " + str(round(auxVoltage.value,3)) + "V\t" + str(round(auxCurrent.value,3)) + "A")
# in case of over-current condition the supplies are disabled
dwf.FDwfAnalogIOEnableStatus(hdwf, byref(IsEnabled))
if not IsEnabled:
#re-enable supplies
print("Power supplies stopped. Restarting...")
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(False))
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True))
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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")
hdwf = c_int()
usbVoltage = c_double()
usbCurrent = c_double()
auxVoltage = c_double()
auxCurrent = c_double()
deviceTemperature = c_double()
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:
print("failed to open device")
quit()
print("Device temperature and USB/AUX supply voltage and current")
# 10 times, once per second
for i in range(1, 11):
# wait between readings
time.sleep(1)
# fetch analog IO status from device
if dwf.FDwfAnalogIOStatus(hdwf) == 0 :
break;
# get system monitor readings
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(0), byref(usbVoltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(1), byref(usbCurrent))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(2), byref(deviceTemperature))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(3), c_int(0), byref(auxVoltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(3), c_int(1), byref(auxCurrent))
print("Temperature: " + str(deviceTemperature.value) + "*C")
print("USB:\t" + str(round(usbVoltage.value,3)) + "V\t" + str(round(usbCurrent.value,3)) + "A")
print("AUX:\t" + str(round(auxVoltage.value,3)) + "V\t" + str(round(auxCurrent.value,3)) + "A")
#close the device
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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")
hdwf = c_int()
sts = c_byte()
IsEnabled = c_int()
supplyVoltage = c_double()
supplyCurrent = c_double()
supplyPower = c_double()
supplyLoadPercentage = c_double()
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:
print("failed to open device")
quit()
print("Preparing to read sample...")
#set up analog IO channel nodes
# enable positive supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, 0, 0, 1)
# enable negative supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, 1, 0, 1)
# master enable
dwf.FDwfAnalogIOEnableSet(hdwf, True)
for i in range(1, 61):
#wait 1 second between readings
time.sleep(1)
#fetch analogIO status from device
dwf.FDwfAnalogIOStatus(hdwf)
#supply monitor
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(3), c_int(0), byref(supplyVoltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(3), c_int(1), byref(supplyCurrent))
supplyPower.value = supplyVoltage.value * supplyCurrent.value
print("Total supply power: " + str(supplyPower.value) + "W")
supplyLoadPercentage.value = 100 * (supplyCurrent.value / 0.2)
print("Load: " + str(supplyLoadPercentage.value) + "%")
# in case of over-current condition the supplies are disabled
dwf.FDwfAnalogIOEnableStatus(hdwf, byref(IsEnabled))
if not IsEnabled:
#re-enable supplies
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(0))
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(1))
#close the device
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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")
hdwf = c_int()
deviceVoltage = c_double()
deviceCurrent = c_double()
deviceTemperature = c_double()
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:
print("failed to open device")
quit()
print("Device USB supply voltage, current and device temperature:")
#monitor voltage, current, temperature
#60 times, once per second
for i in range(1, 61):
# wait between readings; the update rate is approximately 1Hz
time.sleep(1)
# fetch analog IO status from device
dwf.FDwfAnalogIOStatus(hdwf)
# get system monitor readings
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(0), byref(deviceVoltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(1), byref(deviceCurrent))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(2), byref(deviceTemperature))
print(str(deviceVoltage.value) + " V\t" + str(deviceCurrent.value) + " A\t " + str(deviceTemperature.value) + "degC")
#close the device
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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")
hdwf = c_int()
sts = c_byte()
IsEnabled = c_int()
voltage = c_double()
current = c_double()
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
print("failed to open device")
quit()
# set digital voltage between 1.2 and 3.3V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(1.8))
# enable VIO output
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True))
# configure week pull for DIN lines, 0.0 low, 0.5 middle, 1 high
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(1), c_double(0.5))
# pull enable for DIO 39 to 24, bit 15 to 0
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(2), c_double(0x0081)) # DIO7 and DIO0
# pull up/down for DIO
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(3), c_double(0x0080)) # DIO7 pull up and DIO0 pull down
# drive strength for DIO lines: 0 (auto based on digital voltage), 2, 4, 6, 8, 12, 16 (mA)
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(4), c_double(8))
# slew rate for DIO lines: 0 quietio, 1 slow, 2 fast
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(5), c_double(0))
for i in range(1, 11):
#wait 1 second between readings
time.sleep(1)
#fetch analogIO status from device
if dwf.FDwfAnalogIOStatus(hdwf) == 0:
break
# USB monitor
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(0), byref(voltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(1), byref(current))
print("USB: " + str(round(voltage.value,3)) + "V\t" + str(round(current.value,3)) + "A")
# VIO monitor
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(1), c_int(0), byref(voltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(1), c_int(1), byref(current))
print("VIO: " + str(round(voltage.value,3)) + "V\t" + str(round(current.value,3)) + "A")
# in case of over-current condition the supplies are disabled
dwf.FDwfAnalogIOEnableStatus(hdwf, byref(IsEnabled))
if not IsEnabled:
#re-enable supplies
print("Restart")
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(False))
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True))
#close the device
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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")
hdwf = c_int()
sts = c_byte()
voltage = c_double()
current = c_double()
vmtr1 = c_double()
vmtr2 = c_double()
vmtr3 = c_double()
vmtr4 = c_double()
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:
print("failed to open device")
quit()
# set up analog IO channel nodes
# enable fixed supply supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(True))
# set voltage 3.3V or 5V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(1), c_double(3.3))
# enable positive supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(1), c_int(0), c_double(True))
# set voltage between 0 and 9V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(1), c_int(1), c_double(6.0))
# set current limitation 0 and 1.5A
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(1), c_int(2), c_double(0.5))
# enable negative supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(2), c_int(0), c_double(True))
# set voltage between 0 and -9V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(2), c_int(1), c_double(-6.0))
# set current limitation between 0 and -1.5A
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(2), c_int(2), c_double(-0.5))
# enable voltage reference 1
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(3), c_int(0), c_double(True))
# set voltage between -10 and 10V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(3), c_int(1), c_double(7.0))
# enable voltage reference 2
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(4), c_int(0), c_double(True))
# set voltage between -10 and 10V
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(4), c_int(1), c_double(-7.0))
# master enable
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True))
for i in range(1, 11):
# wait between readings
time.sleep(1)
# fetch analogIO status from device
if dwf.FDwfAnalogIOStatus(hdwf) == 0:
break
# fixed supply supply readings
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(0), c_int(1), byref(voltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(0), c_int(2), byref(current))
print("VCC: " + str(round(voltage.value,3)) + "V\t" + str(round(current.value,3)) + "A")
# positive supply supply readings
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(1), c_int(1), byref(voltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(1), c_int(2), byref(current))
print("VP+: " + str(round(voltage.value,3)) + "V\t" + str(round(current.value,3)) + "A")
# negative supply supply readings
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(1), byref(voltage))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(2), c_int(2), byref(current))
print("VP-: " + str(round(voltage.value,3)) + "V\t" + str(round(current.value,3)) + "A")
# voltmeter readings -15V..15V
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(5), c_int(0), byref(vmtr1, 0))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(6), c_int(0), byref(vmtr2, 1))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(7), c_int(0), byref(vmtr3, 2))
dwf.FDwfAnalogIOChannelNodeStatus(hdwf, c_int(8), c_int(0), byref(vmtr4, 3))
print("Vmtr1-4: " + str(round(vmtr1.value,3)) + "V\t"+ str(round(vmtr2.value,3)) + "V\t"+ str(round(vmtr3.value,3)) + "V\t"+ str(round(vmtr4.value,3)) + "V\t")
# close the device
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-28
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import sys
import numpy
import matplotlib.pyplot as plt
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
dwf = cdll.LoadLibrary("libdwf.so")
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
hdwf = c_int()
szerr = create_string_buffer(512)
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
# this option will enable dynamic adjustment of analog out settings like: frequency, amplitude...
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(3))
sts = c_byte()
steps = 151
start = 1e2
stop = 1e6
reference = 1e3
print("Reference: "+str(reference)+" Ohm Frequency: "+str(start)+" Hz ... "+str(stop/1e3)+" kHz for nanofarad capacitors")
dwf.FDwfAnalogImpedanceReset(hdwf)
dwf.FDwfAnalogImpedanceModeSet(hdwf, c_int(8)) # 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
dwf.FDwfAnalogImpedanceReferenceSet(hdwf, c_double(reference)) # reference resistor value in Ohms
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(start)) # frequency in Hertz
dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, c_double(1)) # 1V amplitude = 2V peak2peak signal
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(1)) # start
time.sleep(2)
rgHz = [0.0]*steps
rgRs = [0.0]*steps
rgXs = [0.0]*steps
for i in range(steps):
hz = stop * pow(10.0, 1.0*(1.0*i/(steps-1)-1)*math.log10(stop/start)) # exponential frequency steps
print("Step: "+str(i)+" "+str(hz)+"Hz")
rgHz[i] = hz
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(hz)) # frequency in Hertz
# if settle time is required for the DUT, wait and restart the acquisition
# time.sleep(0.01)
# dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(1))
while True:
if dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts)) == 0:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
quit()
if sts.value == 2:
break
resistance = c_double()
reactance = c_double()
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceResistance, byref(resistance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceReactance, byref(reactance))
rgRs[i] = abs(resistance.value) # absolute value for logarithmic plot
rgXs[i] = abs(reactance.value)
for iCh in range(2):
warn = c_int()
dwf.FDwfAnalogImpedanceStatusWarning(hdwf, c_int(iCh), byref(warn))
if warn.value:
dOff = c_double()
dRng = c_double()
dwf.FDwfAnalogInChannelOffsetGet(hdwf, c_int(iCh), byref(dOff))
dwf.FDwfAnalogInChannelRangeGet(hdwf, c_int(iCh), byref(dRng))
if warn.value & 1:
print("Out of range on Channel "+str(iCh+1)+" <= "+str(dOff.value - dRng.value/2)+"V")
if warn.value & 2:
print("Out of range on Channel "+str(iCh+1)+" >= "+str(dOff.value + dRng.value/2)+"V")
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(0)) # stop
dwf.FDwfDeviceClose(hdwf)
plt.plot(rgHz, rgRs, rgHz, rgXs)
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-29
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import sys
import numpy
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
dwf = cdll.LoadLibrary("libdwf.so")
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
dwf.FDwfParamSet(DwfParamOnClose, c_int(0)) # 0 = run, 1 = stop, 2 = shutdown
hdwf = c_int()
szerr = create_string_buffer(512)
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
sts = c_byte()
frequecy = 1e5
reference = 1e5
print("Reference: "+str(reference)+" Ohm Frequency: "+str(frequecy/1e3)+" kHz for picofarad capacitors")
dwf.FDwfAnalogImpedanceReset(hdwf)
dwf.FDwfAnalogImpedanceModeSet(hdwf, c_int(8)) # 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
dwf.FDwfAnalogImpedanceReferenceSet(hdwf, c_double(reference)) # reference resistor value in Ohms
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(frequecy)) # frequency in Hertz
dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, c_double(1))
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(1)) # start
time.sleep(2)
if True:
# open compensation is important for high frequency or high resistor values
print("Leave the inputs open to perform open compensation.")
try: input = raw_input
except NameError: pass
input("Press enter to continue.")
dwf.FDwfAnalogImpedanceCompReset(hdwf)
dwf.FDwfAnalogImpedanceStatus(hdwf, None) # ignore last capture, force a new one
# do averaging of multiple measurements
openResistance = 0
openReactance = 0
for i in range(10):
while True:
dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts))
if sts.value == 2:
break
v1 = c_double()
v2 = c_double()
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceResistance, byref(v1))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceReactance, byref(v2))
openResistance += v1.value/10
openReactance += v2.value/10
dwf.FDwfAnalogImpedanceCompSet(hdwf, c_double(openResistance), c_double(openReactance), c_double(0), c_double(0)) # apply open compensation
input("Connect the load and press enter to continue.")
dwf.FDwfAnalogImpedanceStatus(hdwf, None) # ignore last capture, force a new one
for i in range(10):
while True:
if dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts)) == 0:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
quit()
if sts.value == 2:
break
capacitance = c_double()
resistance = c_double()
reactance = c_double()
phase = c_double()
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceResistance, byref(resistance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceReactance, byref(reactance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceSeriesCapacitance, byref(capacitance))
print(str(i)+" Resistance: "+str(resistance.value)+" Ohm Reactance: "+str(reactance.value/1e3)+" kOhm Capacitance: "+str(capacitance.value*1e12)+" pF")
gain0 = c_double()
gain = c_double()
phase = c_double()
dwf.FDwfAnalogImpedanceStatusInput(hdwf, c_int(0), byref(gain0), None)
dwf.FDwfAnalogImpedanceStatusInput(hdwf, c_int(1), byref(gain), byref(phase))
print(" AWG/C1: "+str(gain0.value)+" C1/C2: "+str(gain.value)+" phase: "+str(phase.value/math.pi*180))
time.sleep(0.2)
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(0)) # stop
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2019-07-12
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import sys
import numpy
import matplotlib.pyplot as plt
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
dwf = cdll.LoadLibrary("libdwf.so")
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
hdwf = c_int()
szerr = create_string_buffer(512)
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
# this option will enable dynamic adjustment of analog out settings like: frequency, amplitude...
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(3))
sts = c_byte()
steps = 151
start = 1e2
stop = 1e5
reference = 1e3
amplitude = 1
t1 = c_double()
t2 = c_double()
print("Frequency: "+str(start)+" Hz ... "+str(stop/1e3)+" kHz Steps: "+str(steps))
dwf.FDwfAnalogImpedanceReset(hdwf)
dwf.FDwfAnalogImpedanceModeSet(hdwf, c_int(0)) # 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
dwf.FDwfAnalogImpedanceReferenceSet(hdwf, c_double(reference)) # reference resistor value in Ohms
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(start)) # frequency in Hertz
dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, c_double(amplitude)) # 1V amplitude = 2V peak2peak signal
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(1)) # start
time.sleep(2)
rgHz = [0.0]*steps
rgGaC2 = [0.0]*steps
rgPhC2 = [0.0]*steps
rgCs = [0.0]*steps
for i in range(steps):
hz = stop * pow(10.0, 1.0*(1.0*i/(steps-1)-1)*math.log10(stop/start)) # exponential frequency steps
rgHz[i] = hz
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(hz)) # frequency in Hertz
time.sleep(0.01) # settle time for DUT, resonant circuits may require more time
dwf.FDwfAnalogImpedanceStatus(hdwf, None) # ignore last capture since we changed the frequency
while True:
if dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts)) == 0:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
quit()
if sts.value == 2:
break
gain2 = c_double()
phase2 = c_double()
dwf.FDwfAnalogImpedanceStatusInput(hdwf, c_int(1), byref(gain2), byref(phase2)) # relative to Channel 1, C1/C#
rgGaC2[i] = 20.0*math.log10(abs(gain2.value-1.0))
rgPhC2[i] = phase2.value*180/math.pi
cs = c_double()
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceSeriesCapacitance, byref(cs))
rgCs[i] = cs.value
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(0)) # stop
dwf.FDwfDeviceClose(hdwf)
plt.subplot(311)
plt.plot(rgHz, rgGaC2, color='blue')
ax = plt.gca()
ax.set_title("{0:.2f}Hz : {1:.2f}dB {2:.2f}* {3:.2f}nF".format(rgHz[0], rgGaC2[0], rgPhC2[0], rgCs[0]*1e9))
ax.set_xscale('log')
plt.subplot(312)
plt.plot(rgHz, rgPhC2)
ax = plt.gca()
ax.set_xscale('log')
plt.subplot(313)
plt.plot(rgHz, rgCs)
ax = plt.gca()
ax.set_xscale('log')
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-28
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import sys
import numpy
# load dwf library
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
dwf = cdll.LoadLibrary("libdwf.so")
# print version information
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
hdwf = c_int()
szerr = create_string_buffer(512)
# try to connect to the first available device
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
# print error message if connection fails
if hdwf.value == hdwfNone.value:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
# configure impedance measurement
frequnecy = 1e3
reference = 1e3
print("Reference: "+str(reference)+" Ohm Frequency: "+str(frequnecy/1e3)+" kHz for nanofarad capacitors")
dwf.FDwfAnalogImpedanceReset(hdwf)
dwf.FDwfAnalogImpedanceModeSet(hdwf, c_int(8)) # 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
dwf.FDwfAnalogImpedanceReferenceSet(hdwf, c_double(reference)) # reference resistor value in Ohms
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(frequnecy)) # frequency in Hertz
dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, c_double(1))
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(1)) # start
time.sleep(1)
dwf.FDwfAnalogImpedanceStatus(hdwf, None) # ignore last capture, force a new one
sts = c_byte()
capacitance = c_double()
resistance = c_double()
reactance = c_double()
# measurement reading loop
for i in range(10):
while True:
if dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts)) == 0: # handle error
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
quit()
if sts.value == 2:
break
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceResistance, byref(resistance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceReactance, byref(reactance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceSeriesCapacitance, byref(capacitance))
print(str(i)+" Resistance: "+str(resistance.value)+" Ohm Reactance: "+str(reactance.value/1e3)+" kOhm Capacitance: "+str(capacitance.value*1e9)+" nF")
time.sleep(0.2)
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(0)) # stop
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2019-10-21
Requires:
Python 2.7, 3
Desciption:
Performs impedance measurements and pushes to ThingSpeak.com
"""
from ctypes import *
from dwfconstants import *
import math
import time
import sys
import numpy
import requests
url = "https://api.thingspeak.com/update?api_key=8C############BU"
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
dwf = cdll.LoadLibrary("libdwf.so")
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
hdwf = c_int()
szerr = create_string_buffer(512)
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
sts = c_byte()
frequnecy = 1e3
reference = 1e6
capacitance = c_double()
resistance = c_double()
reactance = c_double()
print("Reference: "+str(reference)+" Ohm Frequency: "+str(frequnecy/1e3)+" kHz")
dwf.FDwfAnalogImpedanceReset(hdwf)
dwf.FDwfAnalogImpedanceModeSet(hdwf, c_int(8)) # 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
dwf.FDwfAnalogImpedanceReferenceSet(hdwf, c_double(reference)) # reference resistor value in Ohms
dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(frequnecy)) # frequency in Hertz
dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, c_double(1))
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(1)) # start
time.sleep(1)
dwf.FDwfAnalogImpedanceStatus(hdwf, None) # ignore last capture, force a new one
print("Press Ctrl+C to stop...")
try:
for i in range(1000) :
time.sleep(60) # seconds
if dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts)) == 0:
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
quit()
if sts.value != 2:
print("Measurement not done")
continue
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceResistance, byref(resistance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceReactance, byref(reactance))
dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceSeriesCapacitance, byref(capacitance))
print(str(i)+" Resistance: "+str(resistance.value)+" Ohm Reactance: "+str(reactance.value/1e6)+" MOhm Capacitance: "+str(capacitance.value*1e12)+" pF")
r = requests.get(url+"&field1="+str(resistance.value)+"&field2="+str(reactance.value/1e6)+"&field3="+str(capacitance.value*1e12))
if r.status_code != 200:
print(r)
break
except KeyboardInterrupt: # Ctrl+C
pass
dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(0)) # stop
dwf.FDwfDeviceClose(hdwf)
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
import math
import time
import matplotlib.pyplot as plt
import sys
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")
hdwf = c_int()
sts = c_byte()
cSamples = 4000
rgdAnalog = (c_double*cSamples)()
rgwDigital = (c_uint16*cSamples)()
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 == 0:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
print("Generating signal on DIO 0")
# generate on DIO-0 25khz pulse (100MHz/10000/(7+3)), 30% duty (7low 3high)
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfDigitalOutDividerSet(hdwf, c_int(0), c_int(100))
dwf.FDwfDigitalOutCounterSet(hdwf, c_int(0), c_int(7), c_int(3))
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))
# For synchronous analog/digital acquisition set DigitalInTriggerSource to AnalogIn, start DigitalIn then AnalogIn
# configure DigitalIn
dwf.FDwfDigitalInTriggerSourceSet(hdwf, c_ubyte(4)) # trigsrcAnalogIn
#sample rate = system frequency / divider, 100MHz/1
dwf.FDwfDigitalInDividerSet(hdwf, c_int(1))
# 16bit per sample format
dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(16))
# set number of sample to acquire
dwf.FDwfDigitalInBufferSizeSet(hdwf, c_int(cSamples))
dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(int(cSamples/2))) # trigger position in middle of buffer
# configure AnalogIn
dwf.FDwfAnalogInFrequencySet(hdwf, c_double(1e8))
dwf.FDwfAnalogInBufferSizeSet(hdwf, c_int(cSamples))
dwf.FDwfAnalogInTriggerPositionSet(hdwf, c_double(0)) # trigger position in middle of buffer
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelOffsetSet(hdwf, c_int(0), c_double(-2.0))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5.0))
#trigger on digital signal
#dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_byte(3)) # trigsrcDetectorDigitalIn
#dwf.FDwfDigitalInTriggerSet(hdwf, c_int(0), c_int(0), c_int(1), c_int(0)) # DIO-0 rising edge
# trigger on analog signal
dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_byte(2)) # trigsrcDetectorAnalogIn
dwf.FDwfAnalogInTriggerTypeSet(hdwf, c_int(0)) # trigtypeEdge
dwf.FDwfAnalogInTriggerChannelSet(hdwf, c_int(0)) # first channel
dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(1.5)) # 1.5V
dwf.FDwfAnalogInTriggerConditionSet(hdwf, c_int(0)) # trigcondRisingPositive
dwf.FDwfAnalogInConfigure(hdwf, c_int(True), c_int(False))
#wait at least 2 seconds for the offset to stabilize
time.sleep(2)
# start DigitalIn and AnalogIn
dwf.FDwfDigitalInConfigure(hdwf, c_int(True), c_int(True))
dwf.FDwfAnalogInConfigure(hdwf, c_int(True), c_int(True))
while True: # wait to be armed
dwf.FDwfDigitalInStatus(hdwf, c_int(0), byref(sts))
print("DigitalIn state: "+str(sts.value))
if sts.value == 5 :
continue
dwf.FDwfAnalogInStatus(hdwf, c_int(0), byref(sts))
print("AnalogIn state: "+str(sts.value))
if sts.value == 5 :
continue
break
while True:
time.sleep(1)
dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts))
if sts.value == 2 : # done
dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
break
# read data
dwf.FDwfDigitalInStatusData(hdwf, rgwDigital, c_int(sizeof(c_uint16)*cSamples))
dwf.FDwfAnalogInStatusData(hdwf, c_int(0), rgdAnalog, c_int(cSamples)) # get channel 1 data
#dwf.FDwfAnalogInStatusData(hdwf, 1, rgdAnalog, cSamples) # get channel 2 data
dwf.FDwfDigitalOutReset(hdwf)
dwf.FDwfDeviceCloseAll()
# plot
rgIndex=[0.0]*(cSamples)
rgAnalog=[0.0]*(cSamples)
rgDigital=[0.0]*(cSamples)
for i in range(0,cSamples):
rgIndex[i]=i
rgAnalog[i]=rgdAnalog[i]
rgDigital[i]=rgwDigital[i]&1 # mask DIO0
plt.plot(rgIndex, rgAnalog, rgIndex, rgDigital)
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys
import numpy
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()
rgdSamples = (c_double*4000)()
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
dwf.FDwfParamSet(DwfParamOnClose, c_int(0)) # 0 = run, 1 = stop, 2 = shutdown
#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(szerr.value)
print("failed to open device")
quit()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
cBufMax = c_int()
dwf.FDwfAnalogInBufferSizeInfo(hdwf, 0, byref(cBufMax))
print("Device buffer size: "+str(cBufMax.value))
#set up acquisition
dwf.FDwfAnalogInFrequencySet(hdwf, c_double(20000000.0))
dwf.FDwfAnalogInBufferSizeSet(hdwf, c_int(4000))
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(-1), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(-1), c_double(5))
dwf.FDwfAnalogInChannelFilterSet(hdwf, c_int(-1), filterDecimate)
#dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
# wait at least 2 seconds for the offset to stabilize
time.sleep(2)
print("Starting oscilloscope")
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(1))
while True:
dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
if sts.value == DwfStateDone.value :
break
time.sleep(0.1)
print("Acquisition done")
dwf.FDwfAnalogInStatusData(hdwf, 0, rgdSamples, 4000) # get channel 1 data
#dwf.FDwfAnalogInStatusData(hdwf, 1, rgdSamples, 4000) # get channel 2 data
dwf.FDwfDeviceCloseAll()
#plot window
dc = sum(rgdSamples)/len(rgdSamples)
print("DC: "+str(dc)+"V")
plt.plot(numpy.fromiter(rgdSamples, dtype = numpy.float))
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
Description:
Generates AM modulated signal on AWG1 channel
Scope performs scan shift acquisition and logs DC and AC/DC-RMS values
"""
from ctypes import *
import math
import time
import matplotlib.pyplot as plt
import sys
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")
hdwf = c_int()
sts = c_byte()
secLog = 1.0 # logging rate in seconds
nSamples = 8000
rgdSamples = (c_double*nSamples)()
cValid = c_int(0)
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == 0:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
print("Generating AM sine wave...")
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), c_int(0), c_int(1)) # carrier
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), c_int(0), c_int(1)) # sine
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), c_int(0), c_double(50))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), c_int(0), c_double(1))
dwf.FDwfAnalogOutNodeOffsetSet(hdwf, c_int(0), c_int(0), c_double(0.5))
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), c_int(2), c_int(1)) # AM
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), c_int(2), c_int(3)) # triangle
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), c_int(2), c_double(0.1))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), c_int(2), c_double(50))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, c_int(1)) #acqmodeScanShift
dwf.FDwfAnalogInFrequencySet(hdwf, c_double(nSamples/secLog))
dwf.FDwfAnalogInBufferSizeSet(hdwf, c_int(nSamples))
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
#wait at least 2 seconds for the offset to stabilize
time.sleep(1)
#begin acquisition
dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))
print("Press Ctrl+C to stop")
try:
while True:
time.sleep(secLog)
dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
dwf.FDwfAnalogInStatusSamplesValid(hdwf, byref(cValid))
for iChannel in range(2):
dwf.FDwfAnalogInStatusData(hdwf, c_int(iChannel), byref(rgdSamples), cValid) # get channel 1 data
dc = 0
for i in range(nSamples):
dc += rgdSamples[i]
dc /= nSamples
dcrms = 0
acrms = 0
for i in range(nSamples):
dcrms += rgdSamples[i] ** 2
acrms += (rgdSamples[i]-dc) ** 2
dcrms /= nSamples
dcrms = math.sqrt(dcrms)
acrms /= nSamples
acrms = math.sqrt(acrms)
print(f"CH:{iChannel+1} DC:{dc:.3f}V DCRMS:{dcrms:.3f}V ACRMS:{acrms:.3f}V")
except KeyboardInterrupt:
pass
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(0))
dwf.FDwfDeviceCloseAll()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys
import numpy
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(100000)
nSamples = 200000
rgdSamples = (c_double*nSamples)()
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()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
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(1))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(2))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value)) # -1 infinite record length
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
#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
while cSamples < nSamples:
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 > nSamples :
cAvailable = c_int(nSamples-cSamples)
dwf.FDwfAnalogInStatusData(hdwf, c_int(0), byref(rgdSamples, sizeof(c_double)*cSamples), cAvailable) # get channel 1 data
#dwf.FDwfAnalogInStatusData(hdwf, c_int(1), byref(rgdSamples, 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")
f = open("record.csv", "w")
for v in rgdSamples:
f.write("%s\n" % v)
f.close()
plt.plot(numpy.fromiter(rgdSamples, dtype = numpy.float))
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys
import numpy
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
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(100000)
nSamples = 200000
rgdSamples1 = (c_double*nSamples)()
rgdSamples2 = (c_double*nSamples)()
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")
if dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf)) != 1 or hdwf.value == hdwfNone.value:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
print("Generating sine wave...")
# AWG 1 carrier
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), c_int(0), c_int(1))
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), c_int(0), funcSine)
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), c_int(0), c_double(1))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), c_int(0), c_double(2))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, c_int(3)) # record
dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
sRecord = nSamples/hzAcq.value
dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(sRecord)) # -1 infinite record length
dwf.FDwfAnalogInTriggerPositionSet(hdwf, c_double(-0.25*sRecord)) # -0.25 = trigger at 25%
#set up trigger
dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(10)) # 10 second auto trigger timeout
dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_ubyte(2)) # trigsrcDetectorAnalogIn
dwf.FDwfAnalogInTriggerTypeSet(hdwf, c_int(0)) # trigtypeEdge
dwf.FDwfAnalogInTriggerChannelSet(hdwf, c_int(0)) # channel 1
dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(0)) # 0V
dwf.FDwfAnalogInTriggerHysteresisSet(hdwf, c_double(0.01)) # 0.01V
dwf.FDwfAnalogInTriggerConditionSet(hdwf, c_int(0)) # trigcondRisingPositive
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
#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))
iSample = 0
# loop to get data chunks
while True:
if dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts)) != 1:
print("FDwfAnalogInStatus Error")
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(szerr.value)
break
dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
iSample += cLost.value
iSample %= nSamples
if cLost.value :
fLost = 1
if cCorrupted.value :
fCorrupted = 1
iBuffer = 0
while cAvailable.value>0:
cSamples = cAvailable.value
# we are using circular sample buffer, make sure to not overflow our rgdSamples# buffers
if iSample+cAvailable.value > nSamples:
cSamples = nSamples-iSample
dwf.FDwfAnalogInStatusData2(hdwf, c_int(0), byref(rgdSamples1, sizeof(c_double)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 1 data
dwf.FDwfAnalogInStatusData2(hdwf, c_int(1), byref(rgdSamples2, sizeof(c_double)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 2 data
iBuffer += cSamples
cAvailable.value -= cSamples
iSample += cSamples
iSample %= nSamples
if sts.value == DwfStateDone : # done
break
dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfDeviceCloseAll()
if iSample != 0 :
rgdSamples1 = rgdSamples1[iSample:]+rgdSamples1[:iSample]
rgdSamples2 = rgdSamples2[iSample:]+rgdSamples2[:iSample]
print("Recording done")
if fLost:
print("Samples were lost! Reduce frequency")
if fCorrupted:
print("Samples could be corrupted! Reduce frequency")
f = open("record1.csv", "w")
for v in rgdSamples1:
f.write("%s\n" % v)
f.close()
f = open("record2.csv", "w")
for v in rgdSamples2:
f.write("%s\n" % v)
f.close()
plt.plot(numpy.fromiter(rgdSamples1, dtype = numpy.float), color='orange')
plt.plot(numpy.fromiter(rgdSamples2, dtype = numpy.float), color='blue')
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2020-06-04
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys
import numpy
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(1e6)
nSamples = int(1e5)
rgSamples1 = (c_int16*nSamples)()
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()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
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(10.0*hzAcq.value/nSamples))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(2))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value)) # -1 infinite record length
dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_int(7)) # trigsrcAnalogOut1
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
#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))
iSample = 0
while True:
dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
iSample += cLost.value
iSample %= nSamples
if cLost.value :
fLost = 1
if cCorrupted.value :
fCorrupted = 1
iBuffer = 0
while cAvailable.value>0:
cSamples = cAvailable.value
# we are using circular sample buffer, make sure to not overflow
if iSample+cAvailable.value > nSamples:
cSamples = nSamples-iSample
dwf.FDwfAnalogInStatusData16(hdwf, c_int(0), byref(rgSamples1, sizeof(c_int16)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 1 data
iBuffer += cSamples
cAvailable.value -= cSamples
iSample += cSamples
iSample %= nSamples
if sts.value == 2 : # done
break
dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfDeviceCloseAll()
# align recorded data
if iSample != 0 :
rgSamples1 = rgSamples1[iSample:]+rgSamples1[:iSample]
print("Recording done "+str(iSample))
if fLost:
print("Samples were lost! Reduce frequency")
if fCorrupted:
print("Samples could be corrupted! Reduce frequency")
f = open("record.csv", "w")
for v in rgSamples1:
f.write("%s\n" % v)
f.close()
plt.plot(numpy.fromiter(rgSamples1, dtype = numpy.int16))
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
Desciption:
- generates sine on AWG1
- records data on Scope 1
- writes data to 16 bit WAV file
"""
from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys
import numpy
import wave
import datetime
import os
import array
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()
vOffset = c_double(1.41)
vAmplitude = c_double(1.41)
hzSignal = c_double(80)
hzAcq = c_double(80000)
nSamples = 800000
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()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
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, hzSignal)
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, vAmplitude)
dwf.FDwfAnalogOutNodeOffsetSet(hdwf, c_int(0), AnalogOutNodeCarrier, vOffset)
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(2.0*vAmplitude.value))
dwf.FDwfAnalogInChannelOffsetSet(hdwf, c_int(0), vOffset)
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(-1)) # -1 infinite record length
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
#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
print("Generating "+str(hzSignal.value)+"Hz, recording "+str(hzAcq.value)+"Hz for "+str(nSamples/hzAcq.value)+"s, press Ctrl+C to stop...");
#get the proper file name
#open WAV file
starttime = datetime.datetime.now();
startfilename = "AD2_" + "{:04d}".format(starttime.year) + "{:02d}".format(starttime.month) + "{:02d}".format(starttime.day) + "_" + "{:02d}".format(starttime.hour) + "{:02d}".format(starttime.minute) + "{:02d}".format(starttime.second) + ".wav";
print("Writing WAV file '" + startfilename + "'");
waveWrite = wave.open(startfilename, "wb");
waveWrite.setnchannels(1); # 1 channels
waveWrite.setsampwidth(2); # 16 bit / sample
waveWrite.setframerate(hzAcq.value);
waveWrite.setcomptype("NONE","No compression");
try:
while cSamples < nSamples:
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 > nSamples :
cAvailable = c_int(nSamples-cSamples)
rgSamples = (c_int16*cAvailable.value)()
dwf.FDwfAnalogInStatusData16(hdwf, c_int(0), rgSamples, c_int(0), cAvailable) # get channel 1 data chunk
cSamples += cAvailable.value
waveWrite.writeframes(rgSamples)
except KeyboardInterrupt:
pass
endtime = datetime.datetime.now();
dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfDeviceCloseAll()
print(" done")
if fLost:
print("Samples were lost! Reduce frequency")
if fCorrupted:
print("Samples could be corrupted! Reduce frequency")
waveWrite.close();
endfilename = "AD2_" + "{:04d}".format(starttime.year) + "{:02d}".format(starttime.month) + "{:02d}".format(starttime.day) + "_" + "{:02d}".format(starttime.hour) + "{:02d}".format(starttime.minute) + "{:02d}".format(starttime.second) + "-" + "{:02d}".format(endtime.hour) + "{:02d}".format(endtime.minute) + "{:02d}".format(endtime.second) + ".wav";
print("Renaming file from '" + startfilename + "' to '" + endfilename + "'");
os.rename(startfilename, endfilename);
print(" done")
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys
import numpy
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(1e6)
nSamples = 2**17
rgSamples = (c_int16*nSamples)()
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()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
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(1e8/2**16))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(2))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_int(1))
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value)) # -1 infinite record length
dwf.FDwfAnalogInConfigure(hdwf, c_int(1), c_int(0))
#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
while cSamples < nSamples:
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 > nSamples :
cAvailable = c_int(nSamples-cSamples)
dwf.FDwfAnalogInStatusData16(hdwf, c_int(0), byref(rgSamples, sizeof(c_int16)*cSamples), c_int(0), cAvailable) # get channel 1 data
#dwf.FDwfAnalogInStatusData16(hdwf, c_int(1), byref(rgSamples, sizeof(c_int16)*cSamples), c_int(c), Available) # 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")
f = open("record.csv", "w")
for v in rgSamples:
f.write("%s\n" % v)
f.close()
plt.plot(numpy.fromiter(rgSamples, dtype = numpy.int16))
plt.show()
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""
from ctypes import *
from dwfconstants import *
import time
import sys
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()
voltage1 = c_double()
voltage2 = c_double()
#print(DWF version
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
#open device
"Opening first device..."
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(szerr.value)
print("failed to open device")
quit()
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the device will only be configured when FDwf###Configure is called
print("Preparing to read sample...")
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelOffsetSet(hdwf, c_int(0), c_double(0))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(0))
time.sleep(2)
for i in range(5):
time.sleep(1)
dwf.FDwfAnalogInStatus(hdwf, c_int(False), None) # fetch status and samples from device
dwf.FDwfAnalogInStatusSample(hdwf, c_int(0), byref(voltage1))
dwf.FDwfAnalogInStatusSample(hdwf, c_int(1), byref(voltage2))
print("Channel 1: " + str(voltage1.value)+" V")
print("Channel 2: " + str(voltage2.value)+" V")
dwf.FDwfDeviceCloseAll()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment