Skip to content

Commit

Permalink
a buncha stuff
Browse files Browse the repository at this point in the history
Trying to figure out python sdk integration
  • Loading branch information
smbdoggy83 committed Oct 27, 2022
1 parent b1f06a9 commit 9d3ebd7
Show file tree
Hide file tree
Showing 21 changed files with 3,215 additions and 45 deletions.
51 changes: 51 additions & 0 deletions Python Waveforms Test/AnalogOut_Sine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""

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()
channel = c_int(0)

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

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")
quit()

dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0))# 0 = the device will be configured only when calling FDwf###Configure

dwf.FDwfAnalogOutNodeEnableSet(hdwf, channel, AnalogOutNodeCarrier, c_bool(True))
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, channel, AnalogOutNodeCarrier, funcSine)
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, channel, AnalogOutNodeCarrier, c_double(1000))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, channel, AnalogOutNodeCarrier, c_double(1.41))
dwf.FDwfAnalogOutNodeOffsetSet(hdwf, channel, AnalogOutNodeCarrier, c_double(1.41))

print("Generating sine wave...")
dwf.FDwfAnalogOutConfigure(hdwf, channel, c_bool(True))

dwf.FDwfDeviceClose(hdwf)
81 changes: 81 additions & 0 deletions Python Waveforms Test/Examples/test_device_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from WF_SDK import device # import instruments

"""-----------------------------------------------------------------------"""

# connect to the device
device_data = device.open()

# check for connection errors
device.check_error(device_data)

"""-----------------------------------"""

# set output file name
filename = device_data.name + ".log"
with open(filename, "wt") as f:

# print device information
print("WaveForms version: " + device_data.version + "\n", file=f)

# print device name
print("Device name: " + device_data.name + "\n", file=f)

# print analog input information
print("Analog input information:", file=f)
print("\tchannels: " + str(device_data.analog.input.channel_count), file=f)
print("\tmaximum buffer size: " + str(device_data.analog.input.max_buffer_size), file=f)
print("\tADC resolution: " + str(device_data.analog.input.max_resolution) + " bits", file=f)
print("\trange settable from " + str(device_data.analog.input.min_range) + "V to " +
str(device_data.analog.input.max_range) + "V in " +
str(device_data.analog.input.steps_range) + " steps", file=f)
print("\toffset settable from " + str(device_data.analog.input.min_offset) + "V to " +
str(device_data.analog.input.max_offset) + "V in " +
str(device_data.analog.input.steps_offset) + " steps\n", file=f)

# print analog output information
print("Analog output information:", file=f)
for channel_index in range(device_data.analog.output.channel_count):
print("\tchannel " + str(channel_index) + ":", file=f)
for node_index in range(device_data.analog.output.node_count[channel_index]):
print("\t\tnode " + str(node_index) + ":", file=f)
print("\t\t\tnode type: " + device_data.analog.output.node_type[channel_index][node_index], file=f)
print("\t\t\tmaximum buffer size: " + str(device_data.analog.output.max_buffer_size[channel_index][node_index]), file=f)
print("\t\t\tamplitude settable from: " + str(device_data.analog.output.min_amplitude[channel_index][node_index]) + "V to " +
str(device_data.analog.output.max_amplitude[channel_index][node_index]) + "V", file=f)
print("\t\t\toffset settable from: " + str(device_data.analog.output.min_offset[channel_index][node_index]) + "V to " +
str(device_data.analog.output.max_offset[channel_index][node_index]) + "V", file=f)
print("\t\t\tfrequency settable from: " + str(device_data.analog.output.min_frequency[channel_index][node_index]) + "Hz to " +
str(device_data.analog.output.max_frequency[channel_index][node_index]) + "Hz\n", file=f)

# print analog IO information
print("Analog IO information:", file=f)
for channel_index in range(device_data.analog.IO.channel_count):
print("\tchannel " + str(channel_index) + ":", file=f)
print("\t\tchannel name: " + device_data.analog.IO.channel_name[channel_index], file=f)
print("\t\tchannel label: " + device_data.analog.IO.channel_label[channel_index], file=f)
for node_index in range(device_data.analog.IO.node_count[channel_index]):
print("\t\tnode " + str(node_index) + ":", file=f)
print("\t\t\tnode name: " + device_data.analog.IO.node_name[channel_index][node_index], file=f)
print("\t\t\tunit of measurement: " + device_data.analog.IO.node_unit[channel_index][node_index], file=f)
print("\t\t\tsettable from: " + str(device_data.analog.IO.min_set_range[channel_index][node_index]) + " to " +
str(device_data.analog.IO.max_set_range[channel_index][node_index]) + " in " +
str(device_data.analog.IO.set_steps[channel_index][node_index]) + " steps", file=f)
print("\t\t\treadable between: " + str(device_data.analog.IO.min_read_range[channel_index][node_index]) + " to " +
str(device_data.analog.IO.max_read_range[channel_index][node_index]) + " in " +
str(device_data.analog.IO.read_steps[channel_index][node_index]) + " steps\n", file=f)


# print digital input information
print("Digital input information:", file=f)
print("\tchannels: " + str(device_data.digital.input.channel_count), file=f)
print("\tmaximum buffer size: " + str(device_data.digital.input.max_buffer_size) + "\n", file=f)

# print digital output information
print("Digital output information:", file=f)
print("\tchannels: " + str(device_data.digital.output.channel_count), file=f)
print("\tmaximum buffer size: " + str(device_data.digital.output.max_buffer_size), file=f)

"""-----------------------------------"""

# close the connection
device.close(device_data)
58 changes: 14 additions & 44 deletions Python Waveforms Test/Main.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 25 13:56:45 2022
Created on Tue Oct 25 14:17:10 2022
@author: Seth
"""

import ctypes # import the C compatible data types
from sys import platform, path # this is needed to check the OS type and get the PATH
from os import sep # OS specific file path separators
from WF_SDK import device # import instruments

# load the dynamic library, get constants path (the path is OS specific)
if platform.startswith("win"):
# on Windows
dwf = ctypes.cdll.dwf
constants_path = "C:" + sep + "Program Files (x86)" + sep + "Digilent" + sep + "WaveFormsSDK" + sep + "samples" + sep + "py"
elif platform.startswith("darwin"):
# on macOS
lib_path = sep + "Library" + sep + "Frameworks" + sep + "dwf.framework" + sep + "dwf"
dwf = ctypes.cdll.LoadLibrary(lib_path)
constants_path = sep + "Applications" + sep + "WaveForms.app" + sep + "Contents" + sep + "Resources" + sep + "SDK" + sep + "samples" + sep + "py"
else:
# on Linux
dwf = ctypes.cdll.LoadLibrary("libdwf.so")
constants_path = sep + "usr" + sep + "share" + sep + "digilent" + sep + "waveforms" + sep + "samples" + sep + "py"
"""-----------------------------------------------------------------------"""

# import constants
path.append(constants_path)
import dwfconstants as constants

# =============================================================================

class data:
"""
stores the device handle and the device name
"""
handle = ctypes.c_int(0)
name = ""
# connect to the device
device_data = device.open()

def open():
"""
open the first available device
"""
# this is the device handle - it will be used by all functions to "address" the connected device
device_handle = ctypes.c_int()
# connect to the first available device
dwf.FDwfDeviceOpen(ctypes.c_int(-1), ctypes.byref(device_handle))
data.handle = device_handle
return data

# =============================================================================

"""-----------------------------------"""

# use instruments here


"""-----------------------------------"""

# close the connection
device.close(device_data)
179 changes: 179 additions & 0 deletions Python Waveforms Test/Samples/Device_Info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2018-07-19
Requires:
Python 2.7, 3
"""

from ctypes import *
import sys
import time

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")

# check library loading errors
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
if szerr[0] != b'\0':
print(str(szerr.value))

# declare variables
IsInUse = c_bool()
hdwf = c_int()
int0 = c_int()
int1 = c_int()
uint0 = c_uint()
dbl0 = c_double()
dbl1 = c_double()
dbl2 = c_double()
sz0 = create_string_buffer(256)
sz1 = create_string_buffer(256)
cDevice = c_int()
cChannel = c_int()
cNode = c_int()
rgszAnalogOutNode = ["Carrier", "FM", "AM"]

# 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 connected devices
dwf.FDwfEnum(c_int(0), byref(cDevice))
print("Number of Devices: "+str(cDevice.value))

# open devices
for iDevice in range(0, cDevice.value):
dwf.FDwfEnumDeviceName(c_int(iDevice), devicename)
dwf.FDwfEnumSN(c_int(iDevice), serialnum)
print("------------------------------")
print("Device "+str(iDevice+1)+" : ")
print("\t" + str(devicename.value))
print("\t" + str(serialnum.value))
dwf.FDwfDeviceOpen(c_int(iDevice), byref(hdwf))
if hdwf.value == 0:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(szerr.value)
continue

print("")
dwf.FDwfAnalogInChannelCount(hdwf, byref(int0))
print("AnalogIn channels: "+str(int0.value))

dwf.FDwfAnalogInBufferSizeInfo(hdwf, 0, byref(int0))
print("Buffer size: "+str(int0.value))

dwf.FDwfAnalogInBitsInfo(hdwf, byref(int0))
print("ADC bits: "+str(int0.value))

dwf.FDwfAnalogInChannelRangeInfo(hdwf, byref(dbl0), byref(dbl1), byref(dbl2))
print("Range from "+str(dbl0.value)+" to "+str(dbl1.value)+" in "+str(dbl2.value)+" steps")

dwf.FDwfAnalogInChannelOffsetInfo(hdwf, byref(dbl0), byref(dbl1), byref(dbl2))
print("Offset from "+str(dbl0.value)+" to "+str(dbl1.value)+" in "+str(dbl2.value)+" steps")


print("")
dwf.FDwfAnalogOutCount(hdwf, byref(cChannel))
print("AnalogOut channels: "+str(cChannel.value))

for iChannel in range(0, cChannel.value):
print("Channel "+str(iChannel+1))

fsnodes = c_int()
dwf.FDwfAnalogOutNodeInfo(hdwf, c_int(iChannel), byref(fsnodes))

for iNode in range(0, 2):
if (fsnodes.value & (1<<iNode)) == 0: # bits: AM | FM | Carrier
continue

print("Node: "+rgszAnalogOutNode[iNode])

dwf.FDwfAnalogOutNodeDataInfo(hdwf, iChannel, iNode, 0, byref(int0))
print("Buffer size: "+str(int0.value))

dwf.FDwfAnalogOutNodeAmplitudeInfo(hdwf, iChannel, iNode, byref(dbl0), byref(dbl1))
print("Amplitude from "+str(dbl0.value)+" to "+str(dbl1.value))

dwf.FDwfAnalogOutNodeOffsetInfo(hdwf, iChannel, iNode, byref(dbl0), byref(dbl1))
print("Offset from "+str(dbl0.value)+" to "+str(dbl1.value))

dwf.FDwfAnalogOutNodeFrequencyInfo(hdwf, iChannel, iNode, byref(dbl0), byref(dbl1))
print("Frequency from "+str(dbl0.value)+" to "+str(dbl1.value))

print("")
dwf.FDwfAnalogIOChannelCount(hdwf, byref(cChannel))
print("AnalogIO channels: "+str(cChannel.value))

dwf.FDwfAnalogIOEnableInfo(hdwf, byref(int0), byref(int1))
print("Master Enable: "+("Setting " if int0!=0 else "")+("Reading " if int1!=0 else ""))

for iChannel in range(0, cChannel.value):

dwf.FDwfAnalogIOChannelName(hdwf, iChannel, sz0, sz1)
print("Channel "+str(iChannel+1)+" Name: \""+str(sz0.value)+"\" Label: \""+str(sz1.value)+"\"")

dwf.FDwfAnalogIOChannelInfo(hdwf, iChannel, byref(cNode))

for iNode in range(0, cNode.value):
dwf.FDwfAnalogIOChannelNodeName(hdwf, iChannel, iNode, sz0, sz1)
print("Node "+str(iNode+1)+" Name: \""+str(sz0.value)+"\" Unit: \""+str(sz1.value)+"\"")

dwf.FDwfAnalogIOChannelNodeSetInfo(hdwf, iChannel, iNode, byref(dbl0), byref(dbl1), byref(int0))
if int0.value==1 :
if dbl0.value == dbl1.value :
print("Constant output "+str(dbl0.value))
else:
print("Non settable range from "+str(dbl0)+" to "+str(dbl1))
elif int0.value>1 :
print("Setting from "+str(dbl0.value)+" to "+str(dbl1.value)+" in "+str(int0.value)+" steps")

dwf.FDwfAnalogIOChannelNodeStatusInfo(hdwf, iChannel, iNode, byref(dbl0), byref(dbl1), byref(int0))
if int0.value==1 :
if dbl0.value == dbl1.value :
print("Constant input "+str(dbl0.value))
else:
print("Input range from "+str(dbl0.value)+" to "+str(dbl1.value))
elif int0.value>1 :
print("Reading from "+str(dbl0.value)+" to "+str(dbl1.value)+" in "+str(int0.value)+" steps")


print("")
dwf.FDwfDigitalInBitsInfo(hdwf, byref(int0))
print("DigitalIn channels: "+str(int0.value))

dwf.FDwfDigitalInBufferSizeInfo(hdwf, byref(int0))
print("Buffer size: "+str(int0.value))


print("")
dwf.FDwfDigitalOutCount(hdwf, byref(int0))
print("DigitalOut channels: "+str(int0.value))

dwf.FDwfDigitalOutDataInfo(hdwf, c_int(0), byref(int0))
print("Custom size: "+str(int0.value))


print("")
print("DigitalIO information:")
dwf.FDwfDigitalIOOutputEnableInfo(hdwf, byref(uint0))
print("OE Mask: "+hex(uint0.value))
dwf.FDwfDigitalIOOutputInfo(hdwf, byref(uint0))
print("Output : "+hex(uint0.value))
dwf.FDwfDigitalIOInputInfo(hdwf, byref(uint0))
print("Input : "+hex(uint0.value))

# ensure all devices are closed
dwf.FDwfDeviceCloseAll()
Loading

0 comments on commit 9d3ebd7

Please sign in to comment.