Jump to content
  • 0

Control Analog Discovery With Python Script


gregnordin

Question

We recently started accessing our Analog Discoveries with custom python scripts. We used to use matlab with direct dwf library calls using matlab's calllib() function, but are finding python to be a much more convenient option.

 

Here is my question: while I can cause the waveform generator to output custom or predefined waveforms and can read them with single shot, PC-triggered acquisitions, I can't retrieve Analog Discovery information such as device type, name, and serial number. Here is some sample code:

 

from __future__ import division
from __future__ import print_function
from ctypes import *
from dwfconstants import *
import collections

#load dwf library and declare ctype variables
if sys.platform.startswith("win"):
    dwf = cdll.dwf
    DADinfo['sysplatform'] = sys.platform
    #print(sys.platform)
else:
    print("THIS IS NOT A WINDOWS SYSTEM")

#print DWF version
version = create_string_buffer(32)
dwf.FDwfGetVersion(version)
DADinfo['dwfversion'] = version.value
print("DWF Version: "+version.value)

#make sure any attached Analog Discovery is in a known state
dwf.FDwfDeviceCloseAll()

nDevice = c_int()
result = dwf.FDwfEnum(enumfilterDiscovery, byref(nDevice))
print("Enum:", result, nDevice.value)
isUsed = c_int()
result = dwf.FDwfEnumDeviceIsOpened(c_int(1), byref(isUsed))
print("DeviceIsOpened:", result, isUsed.value)
hdwf = c_int()
result = dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
print("DeviceOpen:",result, hdwf.value)
result = dwf.FDwfEnumDeviceIsOpened(hdwf, byref(isUsed))
print("DeviceIsOpened:", result, isUsed.value)
sn = create_string_buffer(32)
result = dwf.FDwfEnumSN(hdwf,sn)
print("Serial number:", result, sn.value)
dn = create_string_buffer(32)
dwf.FDwfEnumDeviceName(hdwf,dn)
print("Device name:", result, dn.value)
deviceID = c_int()
devicerevision = c_int()
result = dwf.FDwfEnumDeviceType(hdwf, byref(deviceID), byref(devicerevision))
print("Device type:", result, deviceID.value, devicerevision.value)
 

And here is the output I get when I run this code:

 

DWF Version: 2.7.5
Enum: 1 1
DeviceIsOpened: 0 0
DeviceOpen: 1 1
DeviceIsOpened: 0 0
Serial number: 0
Device name: 0
Device type: 0 0 0

 

Note that none of the requested information is returned. Any suggestions?

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

The FDwfEnum*(SN,DeviceIsOpened,DeviceName...) functions return information stacked by last FDwfEnum() call.

For each of these functions and FDwfDeviceOpen use zero based index.

 

The FDwfDeviceOpen with index -1 performs in background enumeration and opens the first available device.

 

The enumeration and open could look like this:

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: "+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" + devicename.value
    print "t" + 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()

Link to comment
Share on other sites

Thanks, attila! Your example code is very useful and works great. My key misunderstanding was that indexing of devices starts at 0. I really appreciate the clarification and example.

 

If I could make a suggestion. The SDK documentation (Waveforms SDK Reference Manual.pdf) is helpful, as are the simple python example files that ship with Waveforms 2 SDK. It would be much more useful if there were more examples that illustrates each of the library calls in the reference manual and how they are intended to be used. I think this is the main thing that is lacking so that individuals such as myself could get up to speed quickly.

 

Thanks again for your very timely help and clarification!

Link to comment
Share on other sites

The above code is Device_Enumeration.py example.

 

Initially the examples were included in the manual. As we made more examples (~15), covering almost all the functions, it seemed too crowded. We thought it is more convenient to have the manual language independent and provide working examples in separate files. These are intended to have an overlook on the functions (some of the many) needed for performing a specific task. These can be opened with the preferred editor, modified and tested. This is our idea but it might be not the best :)

 

Would you like to have more examples or embedded in the documentation? or both?

 

Thank you for sharing your opinion.

Link to comment
Share on other sites

Would you like to have more examples or embedded in the documentation? or both?

 

I understand the desire to break out the code to leave the manual text uncluttered. Perhaps at a minimum including the file name(s) of the relevant examples in the manual text would help so that a new user has some idea where to go.

 

My problem has been that the manual is ok as a reference, but it's difficult to learn from it how to do things. You have to kind of search through many or most of the commands and logically try to figure it out, and without the code examples there's not enough information to do that. On the other hand, the code examples show only the most stripped down functionality (at least, that's my impression) so the new user is left to guess how to do more involved things. Perhaps this could be addressed by adding some more code examples that do somewhat more complex tasks.

 

As I contemplate how to incorporate the Analog Discovery into our course offerings, it seems like a lot of this would need to be worked out so that students have adequate resources from which to learn.

 

Thanks again for your feedback and help.

Link to comment
Share on other sites

We will add references of the examples to the manual.

 

I suppose because of the many features, functions and options the manual is hard to use at the beginning.

The simple examples are intended to start off with the usage performing simple task. These show the needed functions to: open a device, read analog in samples, perform acquisition, use the trigger, record many samples, generate standard, custom analog waveforms, sweep, generate and acquire at the same time...

 

Please let me know if you have some ideas for more complex examples that could be used in education.

 

Here you can find Doug Mercer's WF SDK based python application:

https://ez.analog.com/community/university-program/blog/2013/12/07/analog-discovery-bjt-curve-tracer-program

and further projects:

https://ez.analog.com/community/university-program/blog/tags#/?tags=analog_discovery

 

Thank you for your constructive feedback.

Link to comment
Share on other sites

Thank you very much for the links to Doug Mercer's code. This is exactly the kind of thing I've been looking for. However, I could never find such examples by googling for them. Perhaps it would be better to put such code on a mainstream site such as github or its equivalents in addition to a more specialized site like ez.analog.com.

 

Are you aware of any other code examples people have made available?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...