• Content Count

  • Joined

  • Last visited

Posts posted by Amund

  1. I am using the python API for Waveforms to interface an Analog Discovery 2 and are trying monitor a voltage. The problem is that the measurements on channel 1 are capped to about 2.7 Volt wile channel 2 can measure up to 12.5 (the voltage I am supposed to measure) just fine. When I open Waveforms and use the scope there everything seems to work just fine.

    I am using the example code from whit some small cleanup/modifications.

    Does anyone have an idea what I might be doing wrong?

       DWF Python Example
       Author:  Digilent, Inc.
       Revision:  2018-07-19
           Python 2.7, 3
    from ctypes import *
    from dwfconstants import *
    import math
    import time
    import matplotlib.pyplot as plt
    import sys
    import numpy as np
    import signal
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
    class AnalogDiscovery2():
        def __init__(self):
            self.hdwf = c_int()
            dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
        def record_analog_in(self, pin, duartion):
        def __del__(self):
    def rec(fs, duration_seconds=1, infinite=False):
        #declare ctype variables
        hdwf = c_int()
        sts = c_byte()
        hzAcq = c_double(fs)
        nSamples = duration_seconds * fs
        rgdSamples_1 = (c_double*nSamples)()
        rgdSamples_2 = (c_double*nSamples)()
        cAvailable = c_int()
        cLost = c_int()
        cCorrupted = c_int()
        fLost = 0
        fCorrupted = 0
        #open device
        print("Opening first device")
        dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
        if hdwf.value == hdwfNone.value:
            szerr = create_string_buffer(512)
            print("failed to open device")
        #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)
        if infinite:
            dwf.FDwfAnalogInRecordLengthSet(hdwf, -1)#) # -1 infinite record length
            dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value))
        #wait at least 2 seconds for the offset to stabilize
        print("Starting oscilloscope")
        dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))
        cSamples = 0
        time_start = time.time()
        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.
            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 :
            if cSamples+cAvailable.value > nSamples :
                cAvailable = c_int(nSamples-cSamples)
            dwf.FDwfAnalogInStatusData(hdwf, c_int(0), byref(rgdSamples_1, sizeof(c_double)*cSamples), cAvailable) # get channel 1 data
            dwf.FDwfAnalogInStatusData(hdwf, c_int(1), byref(rgdSamples_2, sizeof(c_double)*cSamples), cAvailable) # get channel 2 data
            cSamples += cAvailable.value
        time_used = time.time() - time_start
        dwf.FDwfAnalogOutReset(hdwf, c_int(0))
        print("Recording done")
        if fLost:
            print("Samples were lost! Reduce frequency")
        if fCorrupted:
            print("Samples could be corrupted! Reduce frequency")
        data_1 = np.fromiter(rgdSamples_1, dtype = np.float)
        data_2 = np.fromiter(rgdSamples_2, dtype = np.float)
        t = np.linspace(0, time_used, len(data_1))"data_1", data_1)"data_2", data_2)"time", t)
    if __name__ == "__main__":
        rec(100, 2)