Hello,
I am using a Digilent Analog Discovery Pro and trying to interface with it using python. I am able to run some examples, but I am having some trouble running the AnalogOut_pattern.py code as I cannot seem to output the custom pattern just once.
My goal is to input a custom signal and be able to record the signal. I used a combination of the analogIn_record.py sample and analogOut_pattern.py sample to create the code below. However, I just want the pattern to play once, not tile 20 times.
"""
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()
##############################################################
bSamples = 1000
rgdSamples1 = (c_double*bSamples)()
for i in range(0,bSamples):
if i % 2 == 0:
rgdSamples1[i] = 0;
else:
rgdSamples1[i] = 1.0*i/bSamples;
print("Generating wave...")
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_bool(True))
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcPlay)
dwf.FDwfAnalogOutDataSet(hdwf, c_int(0), rgdSamples1, 100)
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(1000))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(5))
dwf.FDwfAnalogOutRunSet(hdwf, c_int(0), c_double(0.01)) # run for 10ms
dwf.FDwfAnalogOutRepeatSet(hdwf, c_int(0), c_int(1)) # repeat once
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
plt.plot(numpy.fromiter(rgdSamples1, dtype = float))
plt.show()
###################################################################
#set up acquisition
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_bool(True))
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
#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 = float))
plt.show()
print(len(numpy.fromiter(rgdSamples, dtype = float)))
Thank you!
-LJH