Jump to content
Fourth of July -- Digilent US offices closed ×
  • 0

Custom waveform that exceeds the buffer size - Python


Ergest

Question

Hello, I am trying to generate a signal using Python. I have an array to_tx which is about 60k samples and want to generate it using AD2. I do not get any error on the code, but nothing gets generated. 

from ctypes import *
import time
from dwfconstants import *
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()
chIn = c_int(0) # Scope 1
chOut = c_int(0) # Wavegen 1
nBufferIn = c_int()
nBufferOut = c_int()

version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
# print("DWF Version: "+str(version.value))

# prevent temperature drift
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:
    print("failed to open device")

cSamples = 4096

# Convert the numpy array to ctypes array

prefill = (c_double * cSamples)(*to_tx[0:cSamples])
dwf.FDwfAnalogOutNodeEnableSet(hdwf, chOut, 0, c_int(1))
dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0))
dwf.FDwfAnalogInBufferSizeInfo(hdwf, None, byref(nBufferIn))   # get the size of input buffer
dwf.FDwfAnalogOutNodeDataInfo(hdwf, c_int(0), AnalogOutNodeCarrier, None, byref(nBufferOut)) # get te size of output buffer 
nBuffer = nBufferIn.value + nBufferOut.value


dwf.FDwfAnalogOutNodeDataSet(hdwf, chOut, 0, prefill, c_int(cSamples)) # prefill the buffer with 4096 samples
dwf.FDwfAnalogOutNodeEnableSet(hdwf, chOut, AnalogOutNodeCarrier, c_int(1))
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, chOut, AnalogOutNodeCarrier, funcPlay) # do not use fractional step increments
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, chOut, AnalogOutNodeCarrier, c_double(Fs / cSamples))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, chOut, AnalogOutNodeCarrier, c_double(3.0))
dwf.FDwfAnalogOutNodeOffsetSet(hdwf, chOut, 0, c_double(0))
dwf.FDwfAnalogOutRunSet(hdwf, chOut, c_double(cSamples/Fs)) 
dwf.FDwfAnalogOutRepeatSet(hdwf, chOut, c_int(1)) # repeat only once times

dwf.FDwfAnalogOutConfigure(hdwf, chOut, c_int(1))
dwf.FDwfAnalogOutReset(hdwf, chOut)

# Track the position in the 'to_tx' array
to_tx_position = cSamples
cBuffer = 0
cFree = c_int(0)
cAvailable = c_int(0)
cLost = c_int(0)
cCorrupted = c_int(0)
sts = c_ubyte(0)

while to_tx_position < len(to_tx):
    print("Current position:", to_tx_position)

    if dwf.FDwfAnalogOutStatus(hdwf, chOut, byref(sts)) != 1:
        print("AnalogOutStatus error")
        break

    dwf.FDwfAnalogOutNodePlayStatus(hdwf, chOut, AnalogOutNodeCarrier, byref(cFree), byref(cLost), byref(cCorrupted))
    print("cFree:", cFree.value, "cLost:", cLost.value, "cCorrupted:", cCorrupted.value)

    if cLost.value != 0 or cCorrupted.value != 0:
        print("Output buffer issue")
        break

    if cFree.value > 0:
        end_position = min(to_tx_position + cFree.value, len(to_tx))
        data_chunk = (c_double * (end_position - to_tx_position))(*to_tx[to_tx_position:end_position])

        print("Sending chunk of size:", len(data_chunk))
        if dwf.FDwfAnalogOutNodePlayData(hdwf, chOut, 0, byref(data_chunk), cFree) != 1:
            szerr = create_string_buffer(512)
            dwf.FDwfGetLastErrorMsg(szerr)
            print("Error:", szerr.value)
            break

        to_tx_position += cFree.value
    
        
    dwf.FDwfAnalogOutConfigure(hdwf, chOut, c_int(1))
    print("New position:", to_tx_position)
    
dwf.FDwfAnalogOutReset(hdwf, chOut)
dwf.FDwfDeviceClose(hdwf)  

 

Link to comment
Share on other sites

1 answer 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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...