Posted on

Scripting the Analog Discovery 2 with Python in Linux Part 1

First, install Python, etc

First, check we have python installed:

python3 --version

The result is “Python 3.8.2” on my system. Now check we have setuptools and pip installed. These are third-party Python packages that we (may) need later.

command -v pip

The result is nothing, so we need to install as follows:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py

This installs to /home/USER/.local/bin which is not on PATH. To add to the path:

export PATH="$HOME/.local/bin:$PATH"

Now install the Python dependency manager pipenv (this does a user installation to avoid breaking any system-wide packages):

pip install --user pipenv

Next, install the Adept 2 runtime and Waveforms software to allow communication with the hardware

Install Adept 2 from the Digilent site here. Install Waveforms here.

Next, install the Digilent DWF library wrapper for Python

pip install dwf

This installs the latest stable version of DWF.

Try it

Here’s a test script, modified from a version found on the Digilent forum (forum.digilent.com):

from ctypes import *
import sys

if sys.platform.startswith("win"):
    dwf = cdll.dwf
elif sys.platform.startswith("darwin"):
    dwf = cdll.LoadLibrary("libdwf.dylib")
else:
    dwf = cdll.LoadLibrary("libdwf.so")

#declare ctype variables
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print (szerr.value)

#declare ctype variables
IsInUse = c_bool()
hdwf = c_int()
channel = c_int()
hzfreq = c_double()
cdevices = c_int()

#declare string variables
devicename = create_string_buffer(64)
serialnum = create_string_buffer(16)

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

#enumerate and print device information
dwf.FDwfEnum(c_int(0), byref(cdevices))
print ("Number of Devices: "+str(cdevices.value))

for i in range(0, cdevices.value):
    dwf.FDwfEnumDeviceName (c_int(i), devicename)
    dwf.FDwfEnumSN (c_int(i), serialnum)
    print ("------------------------------")
    print ("Device "+str(i)+" : ")
    print ("t" + str(devicename.value))
    print ("t" + str(serialnum.value))
    dwf.FDwfEnumDeviceIsOpened(c_int(i), byref(IsInUse))

    if not IsInUse:
        dwf.FDwfDeviceOpen(c_int(i), byref(hdwf))
        dwf.FDwfAnalogInChannelCount(hdwf, byref(channel))
        dwf.FDwfAnalogInFrequencyInfo(hdwf, None, byref(hzfreq))
        print ("tAnalog input channels: "+str(channel.value))
        print ("tMax freq: "+str(hzfreq.value))
        dwf.FDwfDeviceClose(hdwf)
        hdwf = c_int(-1)

# ensure all devices are closed
dwf.FDwfDeviceCloseAll()

Running the script results in the following output: