Jump to content
  • 0

Digital discovery DIO pins control with Python


ash

Question

Hi,

We are working on a code for Digital Discovery to generate a pattern from some pins and at the same time read some other pins and show the plot.

This is the code that we are using. As it shows in the code, we are able to select the output pins, however, we would like to select the Digital Discovery input pins to acquire the signal.

from ctypes import *
from dwfconstants import *
import sys
import time
import matplotlib.pyplot as plt

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


#declare ctype variables
IsInUse = c_bool()
hdwf = c_int()
sts = c_byte()
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))


# Get the device name and serial num
dwf.FDwfEnumDeviceName (c_int(0), devicename)
dwf.FDwfEnumSN (c_int(0), serialnum)
print("Device name = %s" %devicename.value)
print("Serial num = %s" %serialnum.value)


# Check if the device is opened
dwf.FDwfEnumDeviceIsOpened(c_int(0), byref(IsInUse))
if not IsInUse:
    dwf.FDwfDeviceOpen(c_int(0), byref(hdwf))

    if hdwf.value == 0:
        print("failed to open device")
        szerr = create_string_buffer(512)
        dwf.FDwfGetLastErrorMsg(szerr)
        print(str(szerr.value))
        quit()


print("Configuring Digital Out")

hzSys = c_double()
dwf.FDwfDigitalOutInternalClockInfo(hdwf, byref(hzSys))


data_py=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]
#data_py=[0,1,1,1,1,1]
# how many bytes we need to fit this many bits, (+7)/8
rgbdata=(c_ubyte*((len(data_py)+7)>>3))(0) # number of bytes that we need to save the array

# array to bits in byte array
for i in range(len(data_py)):
    if data_py[i] != 0:
        rgbdata[i>>3] |= 1<<(i&7)

print(rgbdata[:])

# dwf.FDwfDigitalInInputOrderSet(hdwf, c_int(1))


pin=0 #Pin24
# generate pattern
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1))
dwf.FDwfDigitalOutTypeSet(hdwf, c_int(pin), DwfDigitalOutTypeCustom) # DwfDigitalOutTypeCustom, DwfDigitalOutTypeRandom
# 1kHz sample rate
dwf.FDwfDigitalOutDividerSet(hdwf, c_int(pin), c_int(int(hzSys.value/1e3))) # set sample rate
dwf.FDwfDigitalOutDataSet(hdwf, c_int(pin), byref(rgbdata), c_int(len(data_py)))


print("Generating pattern...")
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

#sample rate = system frequency / divider, 100MHz/1
dwf.FDwfDigitalInDividerSet(hdwf, c_int(1000))
# 16bit per sample format
dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(16))
# set number of sample to acquire
cSamples = 32768
rgwSamples = (c_uint16*cSamples)()
dwf.FDwfDigitalInBufferSizeSet(hdwf, c_int(cSamples))
dwf.FDwfDigitalInInputOrderSet(hdwf, c_int(0)) # with 8 bits DIO-0:7



# begin acquisition
dwf.FDwfDigitalInConfigure(hdwf, c_bool(0), c_bool(1))
print ("waiting to finish")

while True:
    dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts))
    print ("STS VAL: " + str(sts.value))
    if sts.value == stsDone.value:
        break
    time.sleep(1)
print ("Acquisition finished")

# get samples, byte size
dwf.FDwfDigitalInStatusData(hdwf, rgwSamples, 2*cSamples)

time.sleep(10)

dwf.FDwfDigitalOutReset(hdwf)
dwf.FDwfDeviceCloseAll()

rgpy=[0.0]*len(rgwSamples)
for i in range(0,len(rgpy)):
    rgpy[i]=rgwSamples[i]

plt.plot(rgpy)
plt.show()

I was wondering that if someone can help us with this.

 

 

Link to comment
Share on other sites

5 answers 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...