menach_g Posted September 2, 2019 Share Posted September 2, 2019 I'm trying to use output a custom pattern contained in csv format out of a digital discovery. I succeeded doing so using the program and adding manually, now I want to do this in python using the sdk. I can see the function I need to use is "FDwfDigitalOutDataSet" but I don't seem to get it working. the only example on custom data is on the analog discovery and it doesn't seem to be the same. here is the code I try running from ctypes import * from dwfconstants import * import math import time import matplotlib.pyplot as plt import sys dwf = cdll.dwf hdwf = c_int() sts = c_byte() version = create_string_buffer(16) dwf.FDwfGetVersion(version) print("DWF Version: " + str(version.value)) print("Opening first device") dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf)) if hdwf.value == 0: print("failed to open device") szerr = create_string_buffer(512) dwf.FDwfGetLastErrorMsg(szerr) print(str(szerr.value)) quit() hzSys = c_double() dwf.FDwfDigitalOutInternalClockInfo(hdwf, byref(hzSys)) print("Internal frequency is " + str(hzSys.value / 1e6) + " MHz") # 100kHz counter rate, SystemFrequency/100kHz cntFreq = c_uint(int(hzSys.value / 10e6)) 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=(c_int*len(data_py))() # for i in range(0,len(data)): # data[i] = 1.0*i/len(data_py); for i in range(len(data_py)): data[i]=c_int( data_py[i]) pin=0 # generate pattern dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1)) dwf.FDwfDigitalOutOutputSet(hdwf, c_int(pin),c_int(3)) dwf.FDwfDigitalOutDataSet(hdwf,c_int(pin),data,c_int(len(data))) dwf.FDwfDigitalOutConfigure(hdwf, c_int(pin),c_int(1)) print("Generating pattern...") time.sleep(10) dwf.FDwfDigitalOutReset(hdwf) dwf.FDwfDeviceCloseAll() if anyone have an example of a working patterns code using custom pattern is would be much appreciated Link to comment Share on other sites More sharing options...
attila Posted September 2, 2019 Share Posted September 2, 2019 Hi @menach_g 1. The FDwfDigitalOutDataSet expects of bits. See the code below. 2. With FDwfDigitalOutOutputSet(.. .3) did you wanted ThreeState ? In this case the bits will represent: value, output-enable, value.... 3. No need for pin argument in FDwfDigitalOutConfigure 4. You forgot to specify the frequency with FDwfDigitalOutDividerSet See the WF SDK manual and examples. from ctypes import * from dwfconstants 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") hdwf = c_int() version = create_string_buffer(16) dwf.FDwfGetVersion(version) print("DWF Version: "+str(version.value)) print("Opening first device") dwf.FDwfDeviceOpen(c_int(-1), 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] # how many bytes we need to fit this many bits, (+7)/8 rgbdata=(c_ubyte*((len(data_py)+7)>>3))(0) # array to bits in byte array for i in range(len(data_py)): if data_py[i] != 0: rgbdata[i>>3] |= 1<<(i&7) pin=0 # generate pattern dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1)) dwf.FDwfDigitalOutTypeSet(hdwf, c_int(pin), DwfDigitalOutTypeCustom) # 100kHz sample rate dwf.FDwfDigitalOutDividerSet(hdwf, c_int(pin), c_int(int(hzSys.value/100e3))) # 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)) time.sleep(10) dwf.FDwfDigitalOutReset(hdwf) dwf.FDwfDeviceCloseAll() Link to comment Share on other sites More sharing options...
menach_g Posted September 3, 2019 Author Share Posted September 3, 2019 16 hours ago, attila said: Hi @menach_g 1. The FDwfDigitalOutDataSet expects of bits. See the code below. 2. With FDwfDigitalOutOutputSet(.. .3) did you wanted ThreeState ? In this case the bits will represent: value, output-enable, value.... 3. No need for pin argument in FDwfDigitalOutConfigure 4. You forgot to specify the frequency with FDwfDigitalOutDividerSet See the WF SDK manual and examples. from ctypes import * from dwfconstants 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") hdwf = c_int() version = create_string_buffer(16) dwf.FDwfGetVersion(version) print("DWF Version: "+str(version.value)) print("Opening first device") dwf.FDwfDeviceOpen(c_int(-1), 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] # how many bytes we need to fit this many bits, (+7)/8 rgbdata=(c_ubyte*((len(data_py)+7)>>3))(0) # array to bits in byte array for i in range(len(data_py)): if data_py[i] != 0: rgbdata[i>>3] |= 1<<(i&7) pin=0 # generate pattern dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1)) dwf.FDwfDigitalOutTypeSet(hdwf, c_int(pin), DwfDigitalOutTypeCustom) # 100kHz sample rate dwf.FDwfDigitalOutDividerSet(hdwf, c_int(pin), c_int(int(hzSys.value/100e3))) # 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)) time.sleep(10) dwf.FDwfDigitalOutReset(hdwf) dwf.FDwfDeviceCloseAll() thank you very much I finally got it working since then using the example "DigitalOut_Pins.py" do you know if there is a way to input custom data not in byte list format but as a list of bits? also I can't seem to find a command to set the output volatge like in wf program? I tried "FDwfAnalogImpedanceAmplitudeSet" which is the closest thing I found, but it doesn't change the output volatge. I need it to be set to 1.2v instead of 3.3v. thnks for the help! Link to comment Share on other sites More sharing options...
attila Posted September 3, 2019 Share Posted September 3, 2019 Hi @menach_g See the SDK/ samples/ py/ AnalogIO_DigitalDiscovery.py example and the manual PDF. # set digital voltage between 1.2 and 3.3V dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(1.8)) Link to comment Share on other sites More sharing options...
menach_g Posted September 3, 2019 Author Share Posted September 3, 2019 yes it works for 1.2v thank you very much! and I need to output more complicated signals than a simple clock do you know of a way to input the custom data as bit list and not byte list?(yes I know I can add a function to format it that way nut I want to avoid that as I'm dealing with long signals) Link to comment Share on other sites More sharing options...
attila Posted September 3, 2019 Share Posted September 3, 2019 The conversion is needed. The FDwfDigitalOutDataSet only accepts such compact custom data arrays. The Digital Discovery digital-out-custom supports up to 32.768 bit-samples / channel (4kB) Link to comment Share on other sites More sharing options...
menach_g Posted September 19, 2019 Author Share Posted September 19, 2019 On 9/3/2019 at 4:00 PM, attila said: The conversion is needed. The FDwfDigitalOutDataSet only accepts such compact custom data arrays. The Digital Discovery digital-out-custom supports up to 32.768 bit-samples / channel (4kB) hey @attila thanks for all the great help I got the custom output setup fully working for my implemntation but now I want to add digitaln reading back using din pins I want the recording to be in sync with one of the io output channels which is a clock I got working in wf program like so: but when I did not find the equivalent functions in the sdk documentation I tried using snippets of code from "DigitalIn_Sync.py" nut it just goes into infinite loop with "cAvailable.value" == 0 every time..(I would add my code here but some parts like the custom setup are too long for simple code snippets) do you know how to set up a simple input in sync with custom output in python sdk your help will be greatly appreciated!! Link to comment Share on other sites More sharing options...
attila Posted September 19, 2019 Share Posted September 19, 2019 Hi @menach_g For DIO 24 (Digital Discovery) use: # trigger detector mask: low & hight & ( rising | falling ) dwf.FDwfDigitalInTriggerSet(hdwf, c_int(0), c_int(0), c_int(1<<24), c_int(0)) # DIO 24 Link to comment Share on other sites More sharing options...
menach_g Posted September 22, 2019 Author Share Posted September 22, 2019 solved: the problem was made when syncing with a clock and nSamples bigger than clock cycles. solution: adding exit condition in acquire loop, using FDwfDigitalOutStatus to check if output is done Link to comment Share on other sites More sharing options...
Question
menach_g
I'm trying to use output a custom pattern contained in csv format out of a digital discovery.
I succeeded doing so using the program and adding manually, now I want to do this in python using the sdk.
I can see the function I need to use is "FDwfDigitalOutDataSet" but I don't seem to get it working.
the only example on custom data is on the analog discovery and it doesn't seem to be the same.
here is the code I try running
if anyone have an example of a working patterns code using custom pattern is would be much appreciated
Link to comment
Share on other sites
8 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.