Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
test-and-measurement:guides:waveforms-sdk-getting-started [2021/12/08 09:39] – [Getting Started with the WaveForms SDK] Álmos Veres-Vitályos | test-and-measurement:guides:waveforms-sdk-getting-started [2024/03/25 22:09] (current) – [Getting Started with WaveForms SDK] Arthur Brown | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Getting Started with the WaveForms SDK ====== | + | ====== Getting Started with WaveForms SDK ====== |
<WRAP group> | <WRAP group> | ||
WaveForms SDK is a set of tools provided within the WaveForms installation that are used to develop custom software solutions that use Digilent Test and Measurement devices. The WaveForms SDK API is available in several programming languages, making it easy to use across many different platforms. | WaveForms SDK is a set of tools provided within the WaveForms installation that are used to develop custom software solutions that use Digilent Test and Measurement devices. The WaveForms SDK API is available in several programming languages, making it easy to use across many different platforms. | ||
Line 6: | Line 6: | ||
This guide demonstrates the usage of some basic instruments and presents some patterns to help develop your own application. It is written with Python in mind, but users of other languages will still find it useful to illustrate the principles of working with the SDK. | This guide demonstrates the usage of some basic instruments and presents some patterns to help develop your own application. It is written with Python in mind, but users of other languages will still find it useful to illustrate the principles of working with the SDK. | ||
+ | |||
+ | For an up-to-date version of the scripts, check the [[https:// | ||
+ | |||
+ | <WRAP round important> | ||
+ | The GitHub material used by this guide has not been been updated since 2022. Newer hardware, such as the Analog Discovery 3, and additional software features that have been added since that time are not directly supported in Python modules and test examples provided by the GitHub material. | ||
+ | |||
+ | Alternate, fully supported examples for each supported programming language, maintained by the WaveForms development team, are provided with the WaveForms SDK download and may be found at the following locations: | ||
+ | * Windows 32-bit: **C: | ||
+ | * Windows 64-bit: **C: | ||
+ | * Linux: **/ | ||
+ | * macOS: **/ | ||
+ | |||
+ | The Python examples included with the WaveForms SDK download do not use any wrapper modules. However, an overview of how some of the underlying ctypes functions might be used to create modules is provided in the four numbered __[[waveforms-sdk-getting-started# | ||
+ | </ | ||
</ | </ | ||
---- | ---- | ||
Line 12: | Line 26: | ||
<WRAP group> | <WRAP group> | ||
* A Digilent Test & Measurement Device | * A Digilent Test & Measurement Device | ||
+ | * [[test-and-measurement: | ||
+ | * [[test-and-measurement: | ||
* [[test-and-measurement: | * [[test-and-measurement: | ||
* [[test-and-measurement: | * [[test-and-measurement: | ||
+ | * [[test-and-measurement: | ||
* [[test-and-measurement: | * [[test-and-measurement: | ||
* [[test-and-measurement: | * [[test-and-measurement: | ||
Line 29: | Line 46: | ||
Another important file is the one that contains the definition of all constants. If you want to use the SDK from Python, this file is a Python module, while for C/C++ applications, | Another important file is the one that contains the definition of all constants. If you want to use the SDK from Python, this file is a Python module, while for C/C++ applications, | ||
- | Working code examples for each supported programming language are provided with the SDK and may be found at the following locations: | ||
- | * Windows 32-bit: **C: | ||
- | * Windows 64-bit: **C: | ||
- | * Linux: **/ | ||
- | * macOS: **/ | ||
- | For other examples, check: [[test-and-measurement: | + | |
</ | </ | ||
---- | ---- | ||
Line 45: | Line 57: | ||
As the first step of your project import the **dwfconstants.py** file to your project directory (it is located among the sample codes, in the **py** folder), then load the necessary modules and the dynamic library. | As the first step of your project import the **dwfconstants.py** file to your project directory (it is located among the sample codes, in the **py** folder), then load the necessary modules and the dynamic library. | ||
- | </ | + | </ |
- | <script src=" | + | import ctypes |
- | </ | + | 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 | ||
- | --> Optional: Importing the Constants from the Project Directory | + | # load the dynamic library, get constants path (the path is OS specific) |
- | <WRAP group>< | + | if platform.startswith(" |
- | Alternatively, | + | # on Windows |
- | </ | + | dwf = ctypes.cdll.dwf |
- | < | + | |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fdevice.py%23L4-L17& | + | elif platform.startswith(" |
- | </html> | + | # on macOS |
- | </ | + | lib_path = sep + " |
- | <-- | + | dwf = ctypes.cdll.LoadLibrary(lib_path) |
+ | constants_path = sep + " | ||
+ | else: | ||
+ | # on Linux | ||
+ | dwf = ctypes.cdll.LoadLibrary(" | ||
+ | constants_path | ||
+ | |||
+ | # import constants | ||
+ | path.append(constants_path) | ||
+ | import dwfconstants as constants | ||
+ | </code></ | ||
---- | ---- | ||
Line 63: | Line 86: | ||
<WRAP group>< | <WRAP group>< | ||
The next step is to " | The next step is to " | ||
- | </ | ||
- | <script src=" | ||
- | </ | ||
- | --> Optional: Connecting to a Specific Device # | + | Opening |
- | <WRAP group>< | + | </ |
- | If you have multiple Test & Measurement devices connected, opening the first available | + | class data: |
- | </ | + | |
- | <script src="https:// | + | stores the device handle and the device name |
- | </ | + | """ |
- | <-- | + | handle |
+ | name = "" | ||
- | --> Optional: Error Checking # | + | def open(): |
- | <WRAP group>< | + | """ |
- | You can check for errors, | + | open the first available device |
- | </ | + | """ |
- | <script src=" | + | # this is the device handle - it will be used by all functions |
- | </html></ | + | device_handle = ctypes.c_int() |
- | <-- | + | # connect to the first available device |
+ | dwf.FDwfDeviceOpen(ctypes.c_int(-1), ctypes.byref(device_handle)) | ||
+ | data.handle | ||
+ | return data | ||
+ | </code></ | ||
---- | ---- | ||
==== 3. Using Instruments ==== | ==== 3. Using Instruments ==== | ||
- | --> Oscilloscope # | ||
<WRAP group> | <WRAP group> | ||
- | === 3.1 Initialize the Scope === | + | The code snippets in this section present basic functionality of some instruments for some devices. For more possibilities (advanced features and more instruments) check the documentation of the WaveForms SDK, the available sample scripts and the GitHub repository. |
+ | </ | ||
+ | |||
+ | --> 3.1 Oscilloscope # | ||
+ | <WRAP group> | ||
+ | === 3.1.1 Initialize the Scope === | ||
<WRAP group>< | <WRAP group>< | ||
Before measuring with the oscilloscope, | Before measuring with the oscilloscope, | ||
- | </ | + | </ |
- | <script src="https:// | + | class data: |
- | </ | + | |
+ | sampling_frequency | ||
+ | buffer_size | ||
- | === 3.2 Measure a Voltage === | + | def open(device_data, |
+ | """ | ||
+ | initialize the oscilloscope | ||
+ | parameters: - device data | ||
+ | - sampling frequency in Hz, default is 20MHz | ||
+ | - buffer size, default is 8192 | ||
+ | - offset voltage in Volts, default is 0V | ||
+ | - amplitude range in Volts, default is ±5V | ||
+ | """ | ||
+ | # enable all channels | ||
+ | dwf.FDwfAnalogInChannelEnableSet(device_data.handle, | ||
+ | |||
+ | # set offset voltage (in Volts) | ||
+ | dwf.FDwfAnalogInChannelOffsetSet(device_data.handle, | ||
+ | |||
+ | # set range (maximum signal amplitude in Volts) | ||
+ | dwf.FDwfAnalogInChannelRangeSet(device_data.handle, | ||
+ | |||
+ | # set the buffer size (data point in a recording) | ||
+ | dwf.FDwfAnalogInBufferSizeSet(device_data.handle, | ||
+ | |||
+ | # set the acquisition frequency (in Hz) | ||
+ | dwf.FDwfAnalogInFrequencySet(device_data.handle, | ||
+ | |||
+ | # disable averaging (for more info check the documentation) | ||
+ | dwf.FDwfAnalogInChannelFilterSet(device_data.handle, | ||
+ | data.sampling_frequency = sampling_frequency | ||
+ | data.buffer_size = buffer_size | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.1.2 Measure a Voltage === | ||
<WRAP group>< | <WRAP group>< | ||
You can measure voltages, like with the Voltmeter instrument in WaveForms. | You can measure voltages, like with the Voltmeter instrument in WaveForms. | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fscope.py%23L68-L89& | + | def measure(device_data, |
- | </html></ | + | |
+ | measure a voltage | ||
+ | parameters: - device data | ||
+ | - the selected oscilloscope channel (1-2, or 1-4) | ||
+ | |||
+ | returns: | ||
+ | """ | ||
+ | # set up the instrument | ||
+ | dwf.FDwfAnalogInConfigure(device_data.handle, ctypes.c_bool(False), | ||
+ | |||
+ | # read data to an internal buffer | ||
+ | dwf.FDwfAnalogInStatus(device_data.handle, | ||
+ | |||
+ | # extract data from that buffer | ||
+ | voltage | ||
+ | dwf.FDwfAnalogInStatusSample(device_data.handle, | ||
+ | |||
+ | # store the result as float | ||
+ | voltage | ||
+ | return voltage | ||
+ | </code></ | ||
- | === 3.3 Record a Signal === | + | === 3.1.3 Record a Signal === |
<WRAP group>< | <WRAP group>< | ||
The most important feature of the oscilloscope is, that it can record signals. The recorded voltages can be stored in a list. | The most important feature of the oscilloscope is, that it can record signals. The recorded voltages can be stored in a list. | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fscope.py%23L137-L172& | + | def record(device_data, |
- | </html></ | + | |
+ | record an analog signal | ||
+ | parameters: - device data | ||
+ | - the selected oscilloscope channel (1-2, or 1-4) | ||
+ | returns: | ||
+ | - time - a list with the time moments for each voltage in seconds (with the same index as " | ||
+ | """ | ||
+ | # set up the instrument | ||
+ | dwf.FDwfAnalogInConfigure(device_data.handle, ctypes.c_bool(False), | ||
+ | |||
+ | # read data to an internal buffer | ||
+ | while True: | ||
+ | status | ||
+ | dwf.FDwfAnalogInStatus(device_data.handle, | ||
+ | |||
+ | # check internal buffer status | ||
+ | if status.value == constants.DwfStateDone.value: | ||
+ | # exit loop when ready | ||
+ | break | ||
+ | |||
+ | # copy buffer | ||
+ | buffer = (ctypes.c_double * data.buffer_size)() | ||
+ | dwf.FDwfAnalogInStatusData(device_data.handle, | ||
+ | |||
+ | # calculate aquisition time | ||
+ | time = range(0, data.buffer_size) | ||
+ | time = [moment | ||
+ | |||
+ | # convert into list | ||
+ | buffer = [float(element) for element in buffer] | ||
+ | return buffer, time | ||
+ | </code></ | ||
- | === 3.4 Reset the Scope === | + | === 3.1.4 Reset the Scope === |
<WRAP group>< | <WRAP group>< | ||
After usage, reset the oscilloscope to the default settings. | After usage, reset the oscilloscope to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </ | + | """ |
- | + | | |
- | --> Optional: Triggering # | + | " |
- | <WRAP group>< | + | dwf.FDwfAnalogInReset(device_data.handle) |
- | Before recording a signal, you might want to set up triggering. Set the trigger parameters to fit your needs. | + | return |
- | + | </code></ | |
- | <WRAP center round tip 80%> | + | |
- | You can define custom trigger source names, to make the usage of the function easier. | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | <script src="https:// | + | |
- | </ | + | |
- | </ | + | |
- | <script src=" | + | |
- | </html></ | + | |
- | <-- | + | |
- | ---- | + | |
</ | </ | ||
<-- | <-- | ||
- | --> Waveform Generator # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Generate a Signal === | + | === 3.2.1 Generate a Signal === |
<WRAP group>< | <WRAP group>< | ||
Use the waveform generator to generate different signals. | Use the waveform generator to generate different signals. | ||
Line 144: | Line 245: | ||
</ | </ | ||
- | <html> | + | <code python> |
- | <script src=" | + | class function: |
- | </html> | + | """ |
- | </ | + | custom = constants.funcCustom |
- | <script src="https:// | + | sine = constants.funcSine |
- | </html></ | + | square |
+ | triangle = constants.funcTriangle | ||
+ | noise = constants.funcNoise | ||
+ | dc = constants.funcDC | ||
+ | pulse = constants.funcPulse | ||
+ | trapezium = constants.funcTrapezium | ||
+ | sine_power = constants.funcSinePower | ||
+ | ramp_up = constants.funcRampUp | ||
+ | ramp_down = constants.funcRampDown | ||
+ | </code> | ||
+ | </ | ||
+ | def generate(device_data, | ||
+ | | ||
+ | generate an analog signal | ||
+ | parameters: - device data | ||
+ | | ||
+ | - function - possible: custom, sine, square, triangle, noise, ds, pulse, trapezium, sine_power, ramp_up, ramp_down | ||
+ | - offset voltage in Volts | ||
+ | - frequency in Hz, default is 1KHz | ||
+ | - amplitude in Volts, default is 1V | ||
+ | - signal symmetry in percentage, default is 50% | ||
+ | - wait time in seconds, default is 0s | ||
+ | - run time in seconds, default is infinite (0) | ||
+ | - repeat count, default is infinite (0) | ||
+ | - data - list of voltages, used only if function=custom, | ||
+ | """ | ||
+ | # enable channel | ||
+ | channel = ctypes.c_int(channel | ||
+ | dwf.FDwfAnalogOutNodeEnableSet(device_data.handle, | ||
+ | |||
+ | # set function type | ||
+ | dwf.FDwfAnalogOutNodeFunctionSet(device_data.handle, | ||
+ | |||
+ | # load data if the function type is custom | ||
+ | if function | ||
+ | data_length = len(data) | ||
+ | buffer = (ctypes.c_double * data_length)() | ||
+ | for index in range(0, len(buffer)): | ||
+ | buffer[index] = ctypes.c_double(data[index]) | ||
+ | dwf.FDwfAnalogOutNodeDataSet(device_data.handle, | ||
+ | |||
+ | # set frequency | ||
+ | dwf.FDwfAnalogOutNodeFrequencySet(device_data.handle, | ||
+ | |||
+ | # set amplitude or DC voltage | ||
+ | dwf.FDwfAnalogOutNodeAmplitudeSet(device_data.handle, | ||
+ | |||
+ | # set offset | ||
+ | dwf.FDwfAnalogOutNodeOffsetSet(device_data.handle, | ||
+ | |||
+ | # set symmetry | ||
+ | dwf.FDwfAnalogOutNodeSymmetrySet(device_data.handle, | ||
+ | |||
+ | # set running time limit | ||
+ | dwf.FDwfAnalogOutRunSet(device_data.handle, | ||
+ | |||
+ | # set wait time before start | ||
+ | dwf.FDwfAnalogOutWaitSet(device_data.handle, | ||
+ | |||
+ | # set number of repeating cycles | ||
+ | dwf.FDwfAnalogOutRepeatSet(device_data.handle, | ||
+ | |||
+ | # start | ||
+ | dwf.FDwfAnalogOutConfigure(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
- | === 3.2 Reset the Wavegen === | + | === 3.2.2 Reset the Wavegen === |
<WRAP group>< | <WRAP group>< | ||
After usage, reset the wavegen to the default settings. | After usage, reset the wavegen to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data, |
- | </html></ | + | |
- | ---- | + | reset a wavegen channel, or all channels (channel=0) |
+ | """ | ||
+ | channel = ctypes.c_int(channel | ||
+ | dwf.FDwfAnalogOutReset(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | --> Power Supplies # | + | --> |
<WRAP group> | <WRAP group> | ||
- | --> 3.1 Analog Discovery Supplies # | + | --> |
<WRAP group>< | <WRAP group>< | ||
- | The Analog Discovery has only fixed supplies, so just a limited number of functions are available. | + | The Analog Discovery |
- | </ | + | </ |
- | <script src=" | + | def _switch_variable_(device_data, |
- | </html></ | + | """ |
+ | turn the power supplies on/off | ||
+ | parameters: | ||
+ | | ||
+ | | ||
+ | - negative supply switch - True = on, False = off | ||
+ | - positive supply voltage in Volts | ||
+ | - negative supply voltage in Volts | ||
+ | | ||
+ | # set positive voltage | ||
+ | positive_voltage = max(0, min(5, positive_voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set negative voltage | ||
+ | negative_voltage = max(-5, min(0, negative_voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # enable/disable the positive supply | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # enable the negative supply | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # start/stop the supplies - master switch | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
<-- | <-- | ||
- | --> 3.1 Analog Discovery 2 and Analog Discovery Studio Supplies # | + | --> 3.3.2 Analog Discovery 2 and Analog Discovery Studio Supplies # |
<WRAP group>< | <WRAP group>< | ||
These devices have variable positive and negative supplies, so a voltage level can also be set. | These devices have variable positive and negative supplies, so a voltage level can also be set. | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js? | + | def _switch_variable_(device_data, |
- | </html></ | + | |
+ | turn the power supplies on/off | ||
+ | parameters: - device data | ||
+ | - master switch - True = on, False = off | ||
+ | - positive supply switch - True = on, False = off | ||
+ | - negative supply switch - True = on, False = off | ||
+ | - positive supply voltage in Volts | ||
+ | - negative supply voltage in Volts | ||
+ | """ | ||
+ | # set positive voltage | ||
+ | positive_voltage = max(0, min(5, positive_voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set negative voltage | ||
+ | negative_voltage = max(-5, min(0, negative_voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # enable/disable the positive supply | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # enable the negative supply | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # start/stop the supplies - master switch | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, ctypes.c_int(master_state)) | ||
+ | return | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.3.3 Analog Discovery (Legacy) Supplies # | ||
+ | <WRAP group>< | ||
+ | The Analog Discovery has only fixed supplies, so just a limited number of functions are available. | ||
+ | </ | ||
+ | def _switch_fixed_(device_data, | ||
+ | """ | ||
+ | turn the power supplies on/off | ||
+ | parameters: | ||
+ | - master switch - True = on, False = off | ||
+ | - positive supply switch - True = on, False = off | ||
+ | - negative supply switch - True = on, False = off | ||
+ | | ||
+ | # enable/disable the positive supply | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # enable the negative supply | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # start/stop the supplies - master switch | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
<-- | <-- | ||
- | --> 3.1 Analog Discovery Pro 3X50 and Digital Discovery Supplies # | + | --> 3.3.4 Analog Discovery Pro 3X50 and Digital Discovery Supplies # |
<WRAP group>< | <WRAP group>< | ||
Devices with digital supplies have only positive voltage supplies with a variable voltage level between 1.2 and 3.3 Volts. | Devices with digital supplies have only positive voltage supplies with a variable voltage level between 1.2 and 3.3 Volts. | ||
- | </ | + | </ |
- | <script src=" | + | def _switch_digital_(device_data, |
- | </html></ | + | """ |
+ | turn the power supplies on/off | ||
+ | parameters: | ||
+ | | ||
+ | - supply voltage in Volts | ||
+ | | ||
+ | # set supply voltage | ||
+ | voltage = max(1.2, min(3.3, voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # start/stop the supplies - master switch | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
<-- | <-- | ||
- | --> 3.1 Analog Discovery Pro 5250 6V Power Supply # | + | --> 3.3.5 Analog Discovery Pro 5250 6V Power Supply # |
<WRAP group>< | <WRAP group>< | ||
You can set not only the voltage for the 6V power supply on the Analog Discovery 5250, but also the current limit, up to 1A. | You can set not only the voltage for the 6V power supply on the Analog Discovery 5250, but also the current limit, up to 1A. | ||
- | </ | + | </ |
- | <script src="https:// | + | def _switch_6V_(device_data, |
- | </html></ | + | """ |
+ | turn the 6V supply on the ADP5250 on/off | ||
+ | parameters: - master switch - True = on, False = off | ||
+ | | ||
+ | | ||
+ | """ | ||
+ | # set the voltage | ||
+ | voltage | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set the current | ||
+ | current | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # start/stop the supply - master switch | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
<-- | <-- | ||
- | --> 3.1 Analog Discovery Pro 5250 25V Power Supplies # | + | --> 3.3.6 Analog Discovery Pro 5250 25V Power Supplies # |
<WRAP group>< | <WRAP group>< | ||
The positive and negative isolated 25V power supplies are similar to the 6V one, but with a maximum current limit of 500mA. | The positive and negative isolated 25V power supplies are similar to the 6V one, but with a maximum current limit of 500mA. | ||
- | </ | + | </ |
- | <script src=" | + | def _switch_25V_(device_data, |
- | </html></ | + | """ |
+ | turn the 25V power supplies on/off on the ADP5250 | ||
+ | parameters: | ||
+ | | ||
+ | - positive supply voltage in Volts | ||
+ | - negative supply voltage in Volts | ||
+ | - positive supply current limit | ||
+ | - negative supply current limit | ||
+ | | ||
+ | # set positive voltage | ||
+ | positive_voltage = max(0, min(25, positive_voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set negative voltage | ||
+ | negative_voltage *= -1 | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set positive current limit | ||
+ | positive_current = max(0, min(0.5, positive_current)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set negative current limit | ||
+ | negative_current *= -1 | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # enable/disable the supplies | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # master switch | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
<-- | <-- | ||
- | --> Optional: Power Supplies | + | === 3.3.7 Wrapper Function |
<WRAP group>< | <WRAP group>< | ||
- | To make the usage of the above functions easier, you can create a wrapper function, which is able to call the function you need. | + | To make the usage of the above functions easier, you can create a wrapper function, which is able to call the function you need (if the device name is set). |
- | </ | + | </ |
- | <script src=" | + | class data: |
- | </html></ | + | """ |
- | <-- | + | master_state = False # master switch |
+ | state = False # digital/6V/positive supply state | ||
+ | positive_state | ||
+ | negative_state | ||
+ | positive_voltage | ||
+ | negative_voltage = 0 # negative supply voltage | ||
+ | voltage = 0 # digital/positive supply voltage | ||
+ | positive_current = 0 # positive supply current | ||
+ | | ||
+ | current = 0 # digital/6V supply current | ||
- | === 3.2 Reset the Supplies === | + | def switch(device_data, |
+ | """ | ||
+ | turn the power supplies on/off | ||
+ | parameters: - device data | ||
+ | - class containing supplies data: | ||
+ | - master_state | ||
+ | - state and/or positive_state and negative_state | ||
+ | - voltage and/or positive_voltage and negative_voltage | ||
+ | - current and/or positive_current and negative_current | ||
+ | """ | ||
+ | if device_data.name | ||
+ | # switch fixed supplies on AD | ||
+ | supply_state | ||
+ | _switch_fixed_(device_data, | ||
+ | |||
+ | elif device_data.name == " | ||
+ | # switch variable supplies on AD2 | ||
+ | supply_state = supplies_data.state or supplies_data.positive_state | ||
+ | supply_voltage = supplies_data.voltage + supplies_data.positive_voltage | ||
+ | _switch_variable_(device_data, | ||
+ | |||
+ | elif device_data.name == " | ||
+ | # switch the digital supply on DD, or ADP3x50 | ||
+ | supply_state = supplies_data.master_state and (supplies_data.state or supplies_data.positive_state) | ||
+ | supply_voltage = supplies_data.voltage + supplies_data.positive_voltage | ||
+ | _switch_digital_(device_data, | ||
+ | |||
+ | elif device_data.name == " | ||
+ | # switch the 6V supply on ADP5250 | ||
+ | supply_state = supplies_data.master_state and supplies_data.state | ||
+ | _switch_6V_(device_data, | ||
+ | # switch the 25V supplies on ADP5250 | ||
+ | supply_positive_state = supplies_data.master_state and supplies_data.positive_state | ||
+ | supply_negative_state = supplies_data.master_state and supplies_data.negative_state | ||
+ | _switch_25V_(device_data, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.3.8 Reset the Supplies === | ||
<WRAP group>< | <WRAP group>< | ||
After usage, reset the supplies to the default settings. | After usage, reset the supplies to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | """ |
- | ---- | + | reset the supplies |
+ | """ | ||
+ | dwf.FDwfAnalogIOReset(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | --> Digital Multimeter - Only on ADP5250 # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Initialize the DMM === | + | === 3.4.1 Initialize the DMM === |
<WRAP group>< | <WRAP group>< | ||
Before measuring with the digital multimeter, it must be enabled. | Before measuring with the digital multimeter, it must be enabled. | ||
- | </ | + | </ |
- | <script src=" | + | def open(device_data): |
- | </html></ | + | """ |
+ | initialize the digital multimeter | ||
+ | """ | ||
+ | # enable the DMM | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, ctypes.c_int(3), ctypes.c_int(0), ctypes.c_double(1.0)) | ||
+ | return | ||
+ | </code></ | ||
- | === 3.2 Measure With the DMM === | + | === 3.4.2 Measure With the DMM === |
<WRAP group>< | <WRAP group>< | ||
- | <WRAP center round important 60%> | ||
- | This function was not tested. | ||
- | </ | ||
- | |||
You can use the digital multimeter to measure AC, or DC voltages (in Volts), with an input impedance of 10MΩ, or 10GΩ, low (< | You can use the digital multimeter to measure AC, or DC voltages (in Volts), with an input impedance of 10MΩ, or 10GΩ, low (< | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js? | + | def measure(device_data, |
- | </html>< | + | """ |
+ | measure a voltage/current/resistance/continuity/ | ||
+ | parameters: | ||
+ | | ||
+ | | ||
+ | | ||
+ | - high_impedance: | ||
+ | |||
+ | returns: | ||
+ | """ | ||
+ | # set voltage mode | ||
+ | if mode == " | ||
+ | # set coupling | ||
+ | if ac: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | else: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
- | === 3.3 Reset the DMM === | + | # set input impedance |
+ | if high_impedance: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | else: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set high current mode | ||
+ | elif mode == "high current": | ||
+ | # set coupling | ||
+ | if ac: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | else: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set low current mode | ||
+ | elif mode == "low current": | ||
+ | # set coupling | ||
+ | if ac: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | else: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set resistance mode | ||
+ | elif mode == " | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set continuity mode | ||
+ | elif mode == " | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set diode mode | ||
+ | elif mode == " | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set temperature mode | ||
+ | elif mode == " | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # set range | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # fetch analog I/O status | ||
+ | if dwf.FDwfAnalogIOStatus(device_data.handle) == 0: | ||
+ | # signal error | ||
+ | return None | ||
+ | |||
+ | # get reading | ||
+ | measurement = ctypes.c_double() | ||
+ | dwf.FDwfAnalogIOChannelNodeStatus(device_data.handle, | ||
+ | |||
+ | return measurement.value | ||
+ | </ | ||
+ | |||
+ | === 3.4.3 Reset the DMM === | ||
<WRAP group>< | <WRAP group>< | ||
After usage, reset the instrument to the default settings. | After usage, reset the instrument to the default settings. | ||
- | </ | + | </ |
- | <script src=" | + | def close(device_data): |
- | </html></ | + | """ |
- | ---- | + | reset the instrument |
+ | """ | ||
+ | # disable the DMM | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, ctypes.c_int(3), ctypes.c_int(0), ctypes.c_double(0)) | ||
+ | # reset the instrument | ||
+ | dwf.FDwfAnalogIOReset(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | --> Logic Analyzer # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Initialize the Logic Analyzer === | + | === 3.5.1 Initialize the Logic Analyzer === |
<WRAP group>< | <WRAP group>< | ||
Before measuring with the logic analyzer, it must be set up. Change the values to fit your needs. | Before measuring with the logic analyzer, it must be set up. Change the values to fit your needs. | ||
- | </ | + | </ |
- | <script src="https:// | + | class data: |
- | </ | + | |
+ | sampling_frequency | ||
+ | buffer_size | ||
- | === 3.2 Record Logic Signals === | + | def open(device_data, |
+ | """ | ||
+ | initialize the logic analyzer | ||
+ | parameters: - device data | ||
+ | - sampling frequency in Hz, default is 100MHz | ||
+ | - buffer size, default is 4096 | ||
+ | """ | ||
+ | # get internal clock frequency | ||
+ | internal_frequency = ctypes.c_double() | ||
+ | dwf.FDwfDigitalInInternalClockInfo(device_data.handle, | ||
+ | |||
+ | # set clock frequency divider (needed for lower frequency input signals) | ||
+ | dwf.FDwfDigitalInDividerSet(device_data.handle, | ||
+ | |||
+ | # set 16-bit sample format | ||
+ | dwf.FDwfDigitalInSampleFormatSet(device_data.handle, | ||
+ | |||
+ | # set buffer size | ||
+ | dwf.FDwfDigitalInBufferSizeSet(device_data.handle, | ||
+ | data.sampling_frequency = sampling_frequency | ||
+ | data.buffer_size = buffer_size | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.5.2 Record Logic Signals === | ||
<WRAP group>< | <WRAP group>< | ||
Record logic signals in a list of lists, then select the one specific for the required DIO line. | Record logic signals in a list of lists, then select the one specific for the required DIO line. | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Flogic.py%23L99-L140& | + | def record(device_data, |
- | </html></ | + | |
+ | initialize the logic analyzer | ||
+ | parameters: - device data | ||
+ | - channel - the selected DIO line number | ||
+ | returns: | ||
+ | - time - a list with the time moments for each value in seconds (with the same index as " | ||
+ | """ | ||
+ | # set up the instrument | ||
+ | dwf.FDwfDigitalInConfigure(device_data.handle, ctypes.c_bool(False), | ||
+ | |||
+ | # read data to an internal buffer | ||
+ | while True: | ||
+ | status | ||
+ | dwf.FDwfDigitalInStatus(device_data.handle, | ||
+ | |||
+ | if status.value | ||
+ | # exit loop when finished | ||
+ | break | ||
+ | |||
+ | # get samples | ||
+ | buffer = (ctypes.c_uint16 * data.buffer_size)() | ||
+ | dwf.FDwfDigitalInStatusData(device_data.handle, | ||
+ | |||
+ | # convert buffer to list of lists of integers | ||
+ | buffer = [int(element) for element in buffer] | ||
+ | result = [[] for _ in range(16)] | ||
+ | for point in buffer: | ||
+ | for index in range(16): | ||
+ | result[index].append(point & (1 << index)) | ||
+ | |||
+ | # calculate acquisition time | ||
+ | time = range(0, data.buffer_size) | ||
+ | time = [moment | ||
+ | |||
+ | # get channel specific data | ||
+ | buffer = result[channel] | ||
+ | return buffer, time | ||
+ | </code></ | ||
- | === 3.3 Reset the Logic Analyzer === | + | === 3.5.3 Reset the Logic Analyzer === |
<WRAP group>< | <WRAP group>< | ||
After usage, reset the logic analyzer to the default settings. | After usage, reset the logic analyzer to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </ | + | """ |
- | + | reset the instrument | |
- | --> Optional: Triggering # | + | """ |
- | <WRAP group>< | + | dwf.FDwfDigitalInReset(device_data.handle) |
- | Before recording a signal, you might want to set up triggering. Set the trigger parameters to fit your needs. | + | |
- | </ | + | </code></ |
- | <script src=" | + | |
- | </html></ | + | |
- | <-- | + | |
- | ---- | + | |
</ | </ | ||
<-- | <-- | ||
- | --> Pattern Generator # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Generate Logic Signals === | + | === 3.6.1 Generate Logic Signals === |
<WRAP group>< | <WRAP group>< | ||
Configure the pattern generator to generate logic signals. | Configure the pattern generator to generate logic signals. | ||
Line 296: | Line 794: | ||
<WRAP center round tip 80%> | <WRAP center round tip 80%> | ||
- | You can define custom function | + | You can define custom function, trigger source |
</ | </ | ||
- | <html> | + | <code python> |
- | <script src=" | + | class function: |
- | </ | + | """ |
+ | pulse = constants.DwfDigitalOutTypePulse | ||
+ | custom | ||
+ | | ||
- | < | + | class trigger_source: |
- | <script src=" | + | """ |
- | </ | + | none = constants.trigsrcNone |
- | </ | + | analog = constants.trigsrcDetectorAnalogIn |
- | <script src=" | + | digital |
- | </ | + | |
- | === 3.2 Reset the Pattern Generator === | + | class idle_state: |
+ | """ | ||
+ | initial = constants.DwfDigitalOutIdleInit | ||
+ | high = constants.DwfDigitalOutIdleHigh | ||
+ | low = constants.DwfDigitalOutIdleLow | ||
+ | high_impedance = constants.DwfDigitalOutIdleZet | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | def generate(device_data, | ||
+ | """ | ||
+ | generate a logic signal | ||
+ | |||
+ | parameters: - channel - the selected DIO line number | ||
+ | - function - possible: pulse, custom, random | ||
+ | - frequency in Hz | ||
+ | - duty cycle in percentage, used only if function = pulse, default is 50% | ||
+ | - data list, used only if function = custom, default is empty | ||
+ | - wait time in seconds, default is 0 seconds | ||
+ | - repeat count, default is infinite (0) | ||
+ | - run_time: in seconds, 0=infinite, " | ||
+ | - idle - possible: initial, high, low, high_impedance, | ||
+ | - trigger_enabled - include/ | ||
+ | - trigger_source - possible: none, analog, digital, external[1-4] | ||
+ | - trigger_edge_rising - True means rising, False means falling, None means either, default is rising | ||
+ | """ | ||
+ | # get internal clock frequency | ||
+ | internal_frequency = ctypes.c_double() | ||
+ | dwf.FDwfDigitalOutInternalClockInfo(device_data.handle, | ||
+ | |||
+ | # get counter value range | ||
+ | counter_limit = ctypes.c_uint() | ||
+ | dwf.FDwfDigitalOutCounterInfo(device_data.handle, | ||
+ | |||
+ | # calculate the divider for the given signal frequency | ||
+ | if function == constants.DwfDigitalOutTypePulse: | ||
+ | divider = int(-(-(internal_frequency.value / frequency) // counter_limit.value)) | ||
+ | else: | ||
+ | divider = int(internal_frequency.value / frequency) | ||
+ | |||
+ | # enable the respective channel | ||
+ | dwf.FDwfDigitalOutEnableSet(device_data.handle, | ||
+ | |||
+ | # set output type | ||
+ | dwf.FDwfDigitalOutTypeSet(device_data.handle, | ||
+ | |||
+ | # set frequency | ||
+ | dwf.FDwfDigitalOutDividerSet(device_data.handle, | ||
+ | |||
+ | # set idle state | ||
+ | dwf.FDwfDigitalOutIdleSet(device_data.handle, | ||
+ | |||
+ | # set PWM signal duty cycle | ||
+ | if function == constants.DwfDigitalOutTypePulse: | ||
+ | # calculate counter steps to get the required frequency | ||
+ | steps = int(round(internal_frequency.value / frequency / divider)) | ||
+ | # calculate steps for low and high parts of the period | ||
+ | high_steps = int(steps * duty_cycle / 100) | ||
+ | low_steps = int(steps - high_steps) | ||
+ | dwf.FDwfDigitalOutCounterSet(device_data.handle, | ||
+ | |||
+ | # load custom signal data | ||
+ | elif function == constants.DwfDigitalOutTypeCustom: | ||
+ | # format data | ||
+ | buffer = (ctypes.c_ubyte * ((len(data) + 7) >> 3))(0) | ||
+ | for index in range(len(data)): | ||
+ | if data[index] != 0: | ||
+ | buffer[index >> 3] |= 1 << (index & 7) | ||
+ | |||
+ | # load data | ||
+ | dwf.FDwfDigitalOutDataSet(device_data.handle, | ||
+ | |||
+ | # calculate run length | ||
+ | if run_time == " | ||
+ | run_time = len(data) / frequency | ||
+ | |||
+ | # set wait time | ||
+ | dwf.FDwfDigitalOutWaitSet(device_data.handle, | ||
+ | |||
+ | # set repeat count | ||
+ | dwf.FDwfDigitalOutRepeatSet(device_data.handle, | ||
+ | |||
+ | # set run length | ||
+ | dwf.FDwfDigitalOutRunSet(device_data.handle, | ||
+ | |||
+ | # enable triggering | ||
+ | dwf.FDwfDigitalOutRepeatTriggerSet(device_data.handle, | ||
+ | |||
+ | if trigger_enabled: | ||
+ | # set trigger source | ||
+ | dwf.FDwfDigitalOutTriggerSourceSet(device_data.handle, | ||
+ | |||
+ | # set trigger slope | ||
+ | if trigger_edge_rising == True: | ||
+ | # rising edge | ||
+ | dwf.FDwfDigitalOutTriggerSlopeSet(device_data.handle, | ||
+ | elif trigger_edge_rising == False: | ||
+ | # falling edge | ||
+ | dwf.FDwfDigitalOutTriggerSlopeSet(device_data.handle, | ||
+ | elif trigger_edge_rising == None: | ||
+ | # either edge | ||
+ | dwf.FDwfDigitalOutTriggerSlopeSet(device_data.handle, | ||
+ | |||
+ | # start generating the signal | ||
+ | dwf.FDwfDigitalOutConfigure(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.6.2 Reset the Pattern Generator === | ||
<WRAP group>< | <WRAP group>< | ||
After usage, reset the pattern generator to the default settings. | After usage, reset the pattern generator to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | """ |
- | ---- | + | reset the instrument |
+ | """ | ||
+ | dwf.FDwfDigitalOutReset(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | --> Static I/O # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Set Pins As Input Or As Output === | + | === 3.7.1 Set Pins As Input Or As Output === |
<WRAP group>< | <WRAP group>< | ||
Each digital pin of the Test & Measurement device can be used only as input, or as output at a time. The default settings for each line are input states. | Each digital pin of the Test & Measurement device can be used only as input, or as output at a time. The default settings for each line are input states. | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fstatic.py%23L28-L55& | + | def set_mode(device_data, |
- | </html></ | + | |
+ | set a DIO line as input, or as output | ||
+ | parameters: - device data | ||
+ | - selected DIO channel number | ||
+ | - True means output, False means input | ||
+ | """ | ||
+ | # load current state of the output enable buffer | ||
+ | mask = ctypes.c_uint16() | ||
+ | dwf.FDwfDigitalIOOutputEnableGet(device_data.handle, | ||
+ | |||
+ | # convert mask to list | ||
+ | mask = list(bin(mask.value)[2:].zfill(16)) | ||
+ | |||
+ | # set bit in mask | ||
+ | if output: | ||
+ | mask[15 | ||
+ | else: | ||
+ | mask[15 - channel] | ||
+ | |||
+ | # convert mask to number | ||
+ | mask = "" | ||
+ | mask = int(mask, 2) | ||
+ | |||
+ | # set the pin to output | ||
+ | dwf.FDwfDigitalIOOutputEnableSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
- | === 3.2 Get Pin State === | + | === 3.7.2 Get Pin State === |
<WRAP group>< | <WRAP group>< | ||
Read the state of a DIO line with the following code snippet: | Read the state of a DIO line with the following code snippet: | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fstatic.py%23L59-L83& | + | def get_state(device_data, |
- | </html></ | + | |
+ | get the state of a DIO line | ||
+ | parameters: - device data | ||
+ | - selected DIO channel number | ||
+ | returns: | ||
+ | """ | ||
+ | # load internal buffer with current state of the pins | ||
+ | dwf.FDwfDigitalIOStatus(device_data.handle) | ||
+ | |||
+ | # get the current state of the pins | ||
+ | data = ctypes.c_uint32() | ||
+ | dwf.FDwfDigitalIOInputStatus(device_data.handle, | ||
+ | |||
+ | # convert the state to a 16 character binary string | ||
+ | data = list(bin(data.value)[2: | ||
+ | |||
+ | # check the required bit | ||
+ | if data[15 - channel] != "0": | ||
+ | value = True | ||
+ | else: | ||
+ | value = False | ||
+ | return value | ||
+ | </code></ | ||
- | === 3.3 Set Pin State === | + | === 3.7.3 Set Pin State === |
<WRAP group>< | <WRAP group>< | ||
To set the state of a DIO line, it must be set as output! | To set the state of a DIO line, it must be set as output! | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fstatic.py%23L87-L114& | + | def set_state(device_data, |
- | </html></ | + | |
+ | set a DIO line as input, or as output | ||
+ | parameters: - device data | ||
+ | - selected DIO channel number | ||
+ | - True means HIGH, False means LOW | ||
+ | """ | ||
+ | # load current state of the output state buffer | ||
+ | mask = ctypes.c_uint16() | ||
+ | dwf.FDwfDigitalIOOutputGet(device_data.handle, | ||
+ | |||
+ | # convert mask to list | ||
+ | mask = list(bin(mask.value)[2:].zfill(16)) | ||
+ | |||
+ | # set bit in mask | ||
+ | if value: | ||
+ | mask[15 | ||
+ | else: | ||
+ | mask[15 - channel] | ||
+ | |||
+ | # convert mask to number | ||
+ | mask = "" | ||
+ | mask = int(mask, 2) | ||
+ | |||
+ | # set the pin state | ||
+ | dwf.FDwfDigitalIOOutputSet(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
- | === 3.4 Reset the Static I/O === | + | === 3.7.4 Reset the Static I/O === |
<WRAP group>< | <WRAP group>< | ||
After usage, reset the instrument to the default settings. | After usage, reset the instrument to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | |
- | + | reset the instrument | |
- | --> Optional: Limiting DIO Channel Current # | + | """ |
- | <WRAP group>< | + | dwf.FDwfDigitalIOReset(device_data.handle) |
- | Test & Measurement devices with digital supplies (the Digital Discovery and the Analog Discovery Pro 3X50) also have options to limit the output current of a DIO channel. | + | return |
- | + | </code></ | |
- | <WRAP center round important 60%> | + | |
- | This function was not tested. | + | |
</ | </ | ||
- | </ | ||
- | <script src=" | ||
- | </ | ||
<-- | <-- | ||
- | --> | + | --> |
- | <WRAP group>< | + | |
- | Test & Measurement devices with digital supplies (the Digital Discovery and the Analog Discovery Pro 3X50) also have options to pull a DIO channel up, or down. | + | |
- | + | ||
- | <WRAP center round important 60%> | + | |
- | This function was not tested. | + | |
- | </ | + | |
- | </ | + | |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | ---- | + | |
- | </ | + | |
- | <-- | + | |
- | + | ||
- | --> | + | |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Initialize the Interface === | + | === 3.8.1 Initialize the Interface === |
<WRAP group>< | <WRAP group>< | ||
Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | ||
- | </ | + | </ |
- | <script src="https://emgithub.com/embed.js?target=https%3A%2F%2Fgithub.com%2FDigilent%2FWaveForms-SDK-Getting-Started%2Fblob%2Fmaster%2FWF_SDK%2Fprotocol%2Fuart.py%23L28-L73& | + | def open(device_data, |
- | </html></ | + | |
+ | initializes UART communication | ||
+ | |||
+ | parameters: - device data | ||
+ | - rx (DIO line used to receive data) | ||
+ | - tx (DIO line used to send data) | ||
+ | - baud_rate (communication speed, default is 9600 bits/s) | ||
+ | - parity possible: None (default), True means even, False means odd | ||
+ | - data_bits (default is 8) | ||
+ | - stop_bits (default is 1) | ||
+ | """ | ||
+ | # set baud rate | ||
+ | dwf.FDwfDigitalUartRateSet(device_data.handle, ctypes.c_double(baud_rate)) | ||
+ | |||
+ | # set communication channels | ||
+ | dwf.FDwfDigitalUartTxSet(device_data.handle, | ||
+ | dwf.FDwfDigitalUartRxSet(device_data.handle, | ||
+ | |||
+ | # set data bit count | ||
+ | dwf.FDwfDigitalUartBitsSet(device_data.handle, | ||
+ | |||
+ | # set parity bit requirements | ||
+ | if parity | ||
+ | parity = 2 | ||
+ | elif parity == False: | ||
+ | parity = 1 | ||
+ | else: | ||
+ | parity = 0 | ||
+ | dwf.FDwfDigitalUartParitySet(device_data.handle, ctypes.c_int(parity)) | ||
+ | |||
+ | # set stop bit count | ||
+ | dwf.FDwfDigitalUartStopSet(device_data.handle, | ||
+ | |||
+ | # initialize channels with idle levels | ||
+ | |||
+ | # dummy read | ||
+ | dummy_buffer | ||
+ | dummy_buffer | ||
+ | dummy_parity_flag = ctypes.c_int(0) | ||
+ | dwf.FDwfDigitalUartRx(device_data.handle, | ||
+ | |||
+ | # dummy write | ||
+ | dwf.FDwfDigitalUartTx(device_data.handle, | ||
+ | return | ||
+ | </code></ | ||
- | === 3.2 Receive Data === | + | === 3.8.2 Receive Data === |
<WRAP group>< | <WRAP group>< | ||
Use the function to the right to read data on an initialized UART interface. | Use the function to the right to read data on an initialized UART interface. | ||
- | </ | + | </ |
- | <script src="https:// | + | def read(device_data): |
- | </ | + | |
+ | receives data from UART | ||
+ | |||
+ | parameters: - device data | ||
+ | return: | ||
+ | | ||
+ | """ | ||
+ | # variable to store results | ||
+ | error = "" | ||
+ | | ||
- | === 3.3 Send Data === | + | # create empty string buffer |
+ | data = (ctypes.c_ubyte * 8193)() | ||
+ | |||
+ | # character counter | ||
+ | count = ctypes.c_int(0) | ||
+ | |||
+ | # parity flag | ||
+ | parity_flag= ctypes.c_int(0) | ||
+ | |||
+ | # read up to 8k characters | ||
+ | dwf.FDwfDigitalUartRx(device_data.handle, | ||
+ | |||
+ | # append current data chunks | ||
+ | for index in range(0, count.value): | ||
+ | rx_data.append(int(data[index])) | ||
+ | |||
+ | # ensure data integrity | ||
+ | while count.value > 0: | ||
+ | # create empty string buffer | ||
+ | data = (ctypes.c_ubyte * 8193)() | ||
+ | |||
+ | # character counter | ||
+ | count = ctypes.c_int(0) | ||
+ | |||
+ | # parity flag | ||
+ | parity_flag= ctypes.c_int(0) | ||
+ | |||
+ | # read up to 8k characters | ||
+ | dwf.FDwfDigitalUartRx(device_data.handle, | ||
+ | # append current data chunks | ||
+ | for index in range(0, count.value): | ||
+ | rx_data.append(int(data[index])) | ||
+ | |||
+ | # check for not acknowledged | ||
+ | if error == "": | ||
+ | if parity_flag.value < 0: | ||
+ | error = " | ||
+ | elif parity_flag.value > 0: | ||
+ | error = " | ||
+ | return rx_data, | ||
+ | </ | ||
+ | |||
+ | === 3.8.3 Send Data === | ||
<WRAP group>< | <WRAP group>< | ||
Use the function to the right to send data on an initialized UART interface to another device. | Use the function to the right to send data on an initialized UART interface to another device. | ||
- | </ | + | </ |
- | <script src="https:// | + | def write(device_data, |
- | </ | + | |
+ | send data through UART | ||
+ | |||
+ | parameters: - data of type string, int, or list of characters/integers | ||
+ | """ | ||
+ | # cast data | ||
+ | if type(data) | ||
+ | data = "" | ||
+ | elif type(data) == list: | ||
+ | data = "" | ||
- | === 3.4 Reset the Interface === | + | # encode the string into a string buffer |
+ | data = ctypes.create_string_buffer(data.encode(" | ||
+ | |||
+ | # send text, trim zero ending | ||
+ | dwf.FDwfDigitalUartTx(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.8.4 Reset the Interface === | ||
<WRAP group>< | <WRAP group>< | ||
After usage, reset the instrument to the default settings. | After usage, reset the instrument to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | """ |
- | ---- | + | reset the uart interface |
+ | """ | ||
+ | dwf.FDwfDigitalUartReset(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | --> Protocol: SPI # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Initialize the Interface === | + | === 3.9.1 Initialize the Interface === |
<WRAP group>< | <WRAP group>< | ||
Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | ||
- | </ | + | </ |
- | <script src="https:// | + | def open(device_data, |
- | </ | + | |
+ | initializes SPI communication | ||
+ | parameters: - device data | ||
+ | | ||
+ | | ||
+ | | ||
+ | - mosi (DIO line used for master out - slave in, optional) | ||
+ | - frequency (communication frequency in Hz, default is 1MHz) | ||
+ | - mode (SPI mode: 0: CPOL=0, CPHA=0; 1: CPOL-0, CPHA=1; 2: CPOL=1, CPHA=0; 3: CPOL=1, CPHA=1) | ||
+ | - order (endianness, | ||
+ | "" | ||
+ | # set the clock frequency | ||
+ | dwf.FDwfDigitalSpiFrequencySet(device_data.handle, | ||
- | === 3.2 Receive Data === | + | # set the clock pin |
+ | dwf.FDwfDigitalSpiClockSet(device_data.handle, | ||
+ | |||
+ | if mosi != None: | ||
+ | # set the mosi pin | ||
+ | dwf.FDwfDigitalSpiDataSet(device_data.handle, | ||
+ | |||
+ | # set the initial state | ||
+ | dwf.FDwfDigitalSpiIdleSet(device_data.handle, | ||
+ | |||
+ | if miso != None: | ||
+ | # set the miso pin | ||
+ | dwf.FDwfDigitalSpiDataSet(device_data.handle, | ||
+ | |||
+ | # set the initial state | ||
+ | dwf.FDwfDigitalSpiIdleSet(device_data.handle, | ||
+ | |||
+ | # set the SPI mode | ||
+ | dwf.FDwfDigitalSpiModeSet(device_data.handle, | ||
+ | |||
+ | # set endianness | ||
+ | if order: | ||
+ | # MSB first | ||
+ | dwf.FDwfDigitalSpiOrderSet(device_data.handle, | ||
+ | else: | ||
+ | # LSB first | ||
+ | dwf.FDwfDigitalSpiOrderSet(device_data.handle, | ||
+ | |||
+ | # set the cs pin HIGH | ||
+ | dwf.FDwfDigitalSpiSelect(device_data.handle, | ||
+ | |||
+ | # dummy write | ||
+ | dwf.FDwfDigitalSpiWriteOne(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.9.2 Receive Data === | ||
<WRAP group>< | <WRAP group>< | ||
Use the function to the right to read data on an initialized SPI interface. | Use the function to the right to read data on an initialized SPI interface. | ||
- | </ | + | </ |
- | <script src="https:// | + | def read(device_data, |
- | </ | + | |
+ | receives data from SPI | ||
+ | parameters: - device data | ||
+ | | ||
+ | | ||
+ | return: | ||
+ | "" | ||
+ | # enable the chip select line | ||
+ | dwf.FDwfDigitalSpiSelect(device_data.handle, | ||
- | === 3.3 Send Data === | + | # create buffer to store data |
+ | buffer = (ctypes.c_ubyte*count)() | ||
+ | |||
+ | # read array of 8 bit elements | ||
+ | dwf.FDwfDigitalSpiRead(device_data.handle, | ||
+ | |||
+ | # disable the chip select line | ||
+ | dwf.FDwfDigitalSpiSelect(device_data.handle, | ||
+ | |||
+ | # decode data | ||
+ | data = [int(element) for element in buffer] | ||
+ | return data | ||
+ | </ | ||
+ | |||
+ | === 3.9.3 Send Data === | ||
<WRAP group>< | <WRAP group>< | ||
Use the function to the right to send data on an initialized SPI interface to another device. | Use the function to the right to send data on an initialized SPI interface to another device. | ||
- | </ | + | </ |
- | <script src="https:// | + | def write(device_data, |
- | </ | + | |
+ | send data through SPI | ||
+ | parameters: - device data | ||
+ | | ||
+ | | ||
+ | """ | ||
+ | # cast data | ||
+ | if type(data) | ||
+ | data = "" | ||
+ | elif type(data) == list: | ||
+ | data = "" | ||
- | === 3.4 Exchange Data === | + | # enable |
- | <WRAP group>< | + | dwf.FDwfDigitalSpiSelect(device_data.handle, ctypes.c_int(cs), ctypes.c_int(0)) |
- | You might want to receive data from a slave device while you are sending commands to it. In this case the data exchanging function can be used. | + | |
- | </ | + | |
- | <script src=" | + | |
- | </ | + | |
- | === 3.5 Spy on SPI communication === | + | # create buffer to write |
- | <WRAP group>< | + | data = bytes(data, " |
- | If two other devices, a master and a slave, are communicating using the initialized communication interface, the Test & Measurement device can be used to spy on this communication | + | buffer |
+ | for index in range(0, len(buffer)): | ||
+ | buffer[index] = ctypes.c_ubyte(data[index]) | ||
- | <WRAP center round important 60%> | + | # write array of 8 bit elements |
- | This function was not tested. | + | dwf.FDwfDigitalSpiWrite(device_data.handle, |
- | </ | + | |
- | </ | + | # disable the chip select line |
- | <script src=" | + | dwf.FDwfDigitalSpiSelect(device_data.handle, ctypes.c_int(cs), ctypes.c_int(1)) |
- | </html></ | + | return |
+ | </code></ | ||
- | === 3.6 Reset the Interface === | + | === 3.9.4 Reset the Interface === |
<WRAP group>< | <WRAP group>< | ||
After usage, reset the instrument to the default settings. | After usage, reset the instrument to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | """ |
- | ---- | + | reset the spi interface |
+ | """ | ||
+ | dwf.FDwfDigitalSpiReset(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | --> Protocol: I2C # | + | --> |
<WRAP group> | <WRAP group> | ||
- | === 3.1 Initialize the Interface === | + | === 3.10.1 Initialize the Interface === |
<WRAP group>< | <WRAP group>< | ||
Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | ||
- | </ | + | </ |
- | <script src="https:// | + | def open(device_data, |
- | </ | + | |
+ | initializes I2C communication | ||
+ | parameters: - device data | ||
+ | | ||
+ | | ||
+ | | ||
+ | - stretching (enables/disables clock stretching) | ||
+ | returns: | ||
+ | """ | ||
+ | # reset the interface | ||
+ | dwf.FDwfDigitalI2cReset(device_data.handle) | ||
- | === 3.2 Receive Data === | + | # clock stretching |
+ | if stretching: | ||
+ | dwf.FDwfDigitalI2cStretchSet(device_data.handle, | ||
+ | else: | ||
+ | dwf.FDwfDigitalI2cStretchSet(device_data.handle, | ||
+ | |||
+ | # set clock frequency | ||
+ | dwf.FDwfDigitalI2cRateSet(device_data.handle, | ||
+ | |||
+ | # set communication lines | ||
+ | dwf.FDwfDigitalI2cSclSet(device_data.handle, | ||
+ | dwf.FDwfDigitalI2cSdaSet(device_data.handle, | ||
+ | |||
+ | # check bus | ||
+ | nak = ctypes.c_int() | ||
+ | dwf.FDwfDigitalI2cClear(device_data.handle, | ||
+ | if nak.value == 0: | ||
+ | return " | ||
+ | |||
+ | # write 0 bytes | ||
+ | dwf.FDwfDigitalI2cWrite(device_data.handle, | ||
+ | if nak.value != 0: | ||
+ | return "NAK: index " + str(nak.value) | ||
+ | return "" | ||
+ | </ | ||
+ | |||
+ | === 3.10.2 Receive Data === | ||
<WRAP group>< | <WRAP group>< | ||
Use the function to the right to read data on an initialized I2C interface. | Use the function to the right to read data on an initialized I2C interface. | ||
- | </ | + | </ |
- | <script src="https:// | + | def read(device_data, |
- | </html></ | + | |
+ | receives data from I2C | ||
+ | |||
+ | parameters: - device data | ||
+ | | ||
+ | | ||
+ | |||
+ | return: | ||
+ | - error message or empty string | ||
+ | """ | ||
+ | # create buffer to store data | ||
+ | buffer | ||
+ | |||
+ | # receive | ||
+ | nak = ctypes.c_int() | ||
+ | | ||
+ | |||
+ | # decode data | ||
+ | data = [int(element) for element in buffer] | ||
- | === 3.3 Send Data === | + | # check for not acknowledged |
+ | if nak.value != 0: | ||
+ | return data, "NAK: index " + str(nak.value) | ||
+ | return data, "" | ||
+ | </ | ||
+ | |||
+ | === 3.10.3 Send Data === | ||
<WRAP group>< | <WRAP group>< | ||
Use the function to the right to send data on an initialized I2C interface to another device. | Use the function to the right to send data on an initialized I2C interface to another device. | ||
- | </ | + | </ |
- | <script src="https:// | + | def write(device_data, |
- | </ | + | |
+ | send data through I2C | ||
+ | |||
+ | parameters: - device data | ||
+ | | ||
+ | | ||
+ | |||
+ | returns: | ||
+ | """ | ||
+ | # cast data | ||
+ | if type(data) | ||
+ | data = "" | ||
+ | elif type(data) == list: | ||
+ | data = "" | ||
- | === 3.4 Exchange Data === | + | # encode the string into a string buffer |
- | <WRAP group>< | + | data = bytes(data, " |
- | You might want to receive | + | buffer |
- | </ | + | for index in range(0, len(buffer)): |
- | <script src=" | + | |
- | </ | + | |
- | === 3.5 Spy on I2C communication === | + | # send |
- | <WRAP group><WRAP half column> | + | nak = ctypes.c_int() |
- | If two other devices, a master and a slave, are communicating using the initialized communication interface, the Test & Measurement device can be used to spy on this communication | + | |
- | <WRAP center round important 60%> | + | # check for not acknowledged |
- | This function was not tested. | + | if nak.value != 0: |
- | </ | + | |
- | </ | + | return "" |
- | <script src="https://emgithub.com/ | + | </code></ |
- | </html></ | + | |
- | === 3.6 Reset the Interface === | + | === 3.10.4 Reset the Interface === |
<WRAP group>< | <WRAP group>< | ||
After usage, reset the instrument to the default settings. | After usage, reset the instrument to the default settings. | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | """ |
- | ---- | + | reset the i2c interface |
+ | """ | ||
+ | dwf.FDwfDigitalI2cReset(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
</ | </ | ||
<-- | <-- | ||
- | |||
- | <WRAP group> | ||
- | The code snippets above present basic functionality of some instruments. For more possibilities (advanced features and more instruments) check the documentation of the WaveForms SDK and the available sample scripts. | ||
- | </ | ||
---- | ---- | ||
==== 4. Disconnecting the Device ==== | ==== 4. Disconnecting the Device ==== | ||
<WRAP group>< | <WRAP group>< | ||
When your script is exiting, it is very important to close the opened connections, | When your script is exiting, it is very important to close the opened connections, | ||
- | </ | + | </ |
- | <script src="https:// | + | def close(device_data): |
- | </html></ | + | """ |
+ | close a specific device | ||
+ | """ | ||
+ | dwf.FDwfDeviceClose(device_data.handle) | ||
+ | return | ||
+ | </code></ | ||
---- | ---- | ||
===== Creating Modules ===== | ===== Creating Modules ===== | ||
Line 527: | Line 1469: | ||
To avoid copying several hundred lines of code into every project, you can create Python modules from the functions controlling the instruments. These modules then can be imported in your project. | To avoid copying several hundred lines of code into every project, you can create Python modules from the functions controlling the instruments. These modules then can be imported in your project. | ||
- | To create a module, create a new file with the desired name and the extension ***.py**, then copy the respective functions into that file. Don't forget to also import the dwfconstants file into every module. | + | To create a module, create a new file with the desired name and the extension ***.py**, then copy the respective functions into that file. Don't forget to also import the dwfconstants file into every module. Place your modules in a separate folder, name this folder (for example WF_SDK is a good name as it is suggestive). |
- | Place your modules in a separate folder, name this folder (for example WF_SDK is a good name as it is suggestive), | + | You can download |
- | + | ||
- | <WRAP center round important 60%> | + | |
- | Don't forget to correct the import path of the **dwfconstants.py** file, if you import it from the project directory. The name of the parent directory (WF_SDK), followed by a dot must be added before the file name. | + | |
- | + | ||
- | If you import the file from the installation folder, no changes have to be made. | + | |
</ | </ | ||
- | You can download | + | <WRAP group> |
+ | As the created function set will be used as a module, an initializer is needed, to let the editors recognize the module. This file contains only the description of the module and imports every file in the module, to make the functions accessible. The created file has to be named < | ||
- | Open the drop-downs below to see the content of the created modules. | + | <code python> |
- | </WRAP> | + | """ |
+ | This module realizes communication with Digilent Test & Measurement devices | ||
+ | """ | ||
- | --> Device Control - device.py # | + | from WF_SDK import |
- | <WRAP group>< | + | from WF_SDK import scope |
- | <script src=" | + | from WF_SDK import wavegen |
- | </html></ | + | from WF_SDK import supplies |
- | <-- | + | from WF_SDK import dmm |
+ | from WF_SDK import logic | ||
+ | from WF_SDK import pattern | ||
+ | from WF_SDK import static | ||
+ | from WF_SDK import protocol | ||
+ | </code> | ||
- | --> Oscilloscope Control - scope.py # | + | Remember, that any submodule (the protocol folder in this case) also needs initialization: |
- | <WRAP group>< | + | |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | --> Waveform Generator Control - wavegen.py # | + | <code python> |
- | <WRAP group>< | + | "" |
- | <script src="https:// | + | This module controls the protocol instrument |
- | </ | + | """ |
- | <-- | + | |
- | --> Power Supplies Control - supplies.py # | + | from WF_SDK.protocol import i2c |
- | <WRAP group>< | + | from WF_SDK.protocol import spi |
- | <script src=" | + | from WF_SDK.protocol import uart |
- | </html></ | + | </code></ |
- | <-- | + | ---- |
- | --> Digital Multimeter Control - supplies.py # | + | ==== Testing ==== |
<WRAP group> | <WRAP group> | ||
- | <WRAP center round important 60%> | + | Copy your module folder (WF_SDK in this case) into the project directory, then create a new Python script. Import the necessary modules, then use your functions to control the Test & Measurement device. |
- | This module | + | |
+ | In the drop-downs below, several examples and a project template will be presented. | ||
+ | |||
+ | **Note: ** //The example using the oscilloscope and the waveform generator won't work on devices without analog I/O capability (Digital Discovery).// | ||
+ | |||
+ | **Note: ** //Name your test scripts " | ||
</ | </ | ||
- | <html> | + | --> Empty Project Template # |
- | <script src=" | + | <WRAP group><WRAP half column> |
- | </html></WRAP> | + | Fill in this template. Be creative, use any instrument in any configuration. |
- | <-- | + | </WRAP>< |
+ | from WF_SDK import device | ||
- | --> Logic Analyzer Control | + | """ |
- | <WRAP group>< | + | |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | --> Pattern Generator Control - pattern.py | + | # connect to the device |
- | <WRAP group>< | + | device_data |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | --> Static I/O Control | + | """ |
- | <WRAP group> | + | |
- | <WRAP center round important 60%> | + | |
- | This module contains untested functions. | + | |
- | </ | + | |
- | < | + | # use instruments here |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | --> Protocol: UART Interface - protocol/ | ||
- | <WRAP group> | ||
- | < | ||
- | <script src=" | ||
- | </ | ||
- | <-- | ||
- | --> Protocol: SPI Interface | + | """ |
- | <WRAP group> | + | |
- | <WRAP center round important 60%> | + | |
- | This module contains untested functions. | + | |
- | </ | + | |
- | < | + | # close the connection |
- | <script src=" | + | device.close(device_data) |
- | </html></ | + | </code></WRAP></ |
<-- | <-- | ||
- | --> | + | --> |
- | <WRAP group> | + | <WRAP group>< |
- | < | + | This example generates a sinusoidal signal on a wavegen channel, then records it on a scope channel. Connect the respective channels together on your device! |
- | This module contains untested functions. | + | |
- | </ | + | |
- | < | + | {{ :test-and-measurement: |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | <WRAP group> | + | {{ :test-and-measurement: |
- | As the created function set will be used as a module, an initializer is needed, to let the text editors recognize the module. This file contains only the description of the module | + | </WRAP><WRAP half column><code python> |
+ | from WF_SDK import device, scope, wavegen | ||
- | After the file is created, your module will be recognized: the module name in the text editor will be colored (this depends on the editor) and if you hover the mouse on the module name, the module description appears. | + | import matplotlib.pyplot as plt # needed for plotting |
- | < | + | """ |
- | <script src="https:// | + | |
- | </ | + | |
- | ---- | + | |
- | ===== Testing ===== | + | # connect |
- | <WRAP group> | + | device_data = device.open() |
- | Copy your module folder (WF_SDK in this case) into the project directory, then create a new Python script. Import the necessary modules, then use your functions | + | |
- | In the drop-downs below, several examples and a project template will be presented. | + | """ |
- | **Note: ** //The example using the oscilloscope and the waveform generator won't work on devices without analog I/O capability | + | # initialize |
- | </ | + | scope.open(device_data) |
- | --> Empty Project Template | + | # generate a 10KHz sine signal with 2V amplitude on channel 1 |
- | <WRAP group>< | + | wavegen.generate(device_data, channel=1, function=wavegen.function.sine, offset=0, frequency=10e03, amplitude=2) |
- | Fill in this template. Be creative, use any instrument in any configuration. | + | |
- | </ | + | |
- | <script src=" | + | |
- | </ | + | |
- | <-- | + | |
- | --> Using the Oscilloscope and the Waveform Generator # | + | # record data with the scopeon channel 1 |
- | <WRAP group>< | + | buffer, time = scope.record(device_data, |
- | This example generates a sinusoidal signal on a wavegen channel, then records it on a scope channel. Connect the respective channels together on your device! | + | |
- | {{ : | + | # plot |
+ | time = [moment * 1e03 for moment in time] # convert time to ms | ||
+ | plt.plot(time, buffer) | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | plt.show() | ||
- | {{ : | + | # reset the scope |
- | </ | + | scope.close(device_data) |
- | <script src="https:// | + | |
- | </html></ | + | # reset the wavegen |
+ | wavegen.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </code></ | ||
<-- | <-- | ||
Line 673: | Line 1593: | ||
{{ : | {{ : | ||
- | </ | + | </ |
- | <script src=" | + | from WF_SDK import device, logic, pattern |
- | </html></ | + | |
+ | import matplotlib.pyplot as plt # needed for plotting | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data | ||
+ | |||
+ | """ | ||
+ | |||
+ | # initialize the logic analyzer with default settings | ||
+ | logic.open(device_data) | ||
+ | |||
+ | # generate a 100KHz PWM signal with 30% duty cycle on DIO0 | ||
+ | pattern.generate(device_data, | ||
+ | |||
+ | # record a logic signal | ||
+ | buffer, time = logic.record(device_data, | ||
+ | |||
+ | # plot | ||
+ | time = [moment * 1e06 for moment in time] # convert time to μs | ||
+ | plt.plot(time, | ||
+ | plt.xlabel("time [μs]" | ||
+ | plt.ylabel(" | ||
+ | plt.yticks([0, | ||
+ | plt.show() | ||
+ | |||
+ | # reset the logic analyzer | ||
+ | logic.close(device_data) | ||
+ | |||
+ | # reset the pattern generator | ||
+ | pattern.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </code></ | ||
<-- | <-- | ||
Line 682: | Line 1639: | ||
Connect LEDs and series resistors to each DIO channel of your device. Use the positive, or the digital power supply to provide current to the LEDs, then use the Static I/O instrument to sink the currents (turn the LEDs on/off). | Connect LEDs and series resistors to each DIO channel of your device. Use the positive, or the digital power supply to provide current to the LEDs, then use the Static I/O instrument to sink the currents (turn the LEDs on/off). | ||
- | {{ : | + | {{ : |
- | </ | + | </ |
- | <script src="https:// | + | from WF_SDK import device, static, supplies |
- | </html></ | + | |
+ | from time import sleep # needed for delays | ||
+ | |||
+ | device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | device_data.name = device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # start the positive supply | ||
+ | supplies_data = supplies.data() | ||
+ | supplies_data.master_state | ||
+ | supplies_data.state = True | ||
+ | supplies_data.voltage = 3.3 | ||
+ | supplies.switch(device_data, | ||
+ | |||
+ | # set all pins as output | ||
+ | for index in range(16): | ||
+ | static.set_mode(device_data, | ||
+ | |||
+ | try: | ||
+ | while True: | ||
+ | # repeat | ||
+ | mask = 1 | ||
+ | while mask < 0x10000: | ||
+ | # go through possible states | ||
+ | for index in range(16): | ||
+ | # set the state of every DIO channel | ||
+ | static.set_state(device_data, | ||
+ | sleep(0.1) | ||
+ | mask <<= 1 # switch mask | ||
+ | |||
+ | while mask > 1: | ||
+ | # go through possible states backward | ||
+ | mask >>= 1 # switch mask | ||
+ | for index in range(16): | ||
+ | # set the state of every DIO channel | ||
+ | static.set_state(device_data, | ||
+ | sleep(0.1) | ||
+ | |||
+ | except KeyboardInterrupt: | ||
+ | # stop if Ctrl+C is pressed | ||
+ | pass | ||
+ | |||
+ | finally: | ||
+ | # stop the static I/O | ||
+ | static.close(device_data) | ||
+ | |||
+ | # stop and reset the power supplies | ||
+ | supplies_data.master_state = False | ||
+ | supplies.switch(device_data, | ||
+ | supplies.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </code></ | ||
<-- | <-- | ||
Line 692: | Line 1710: | ||
Connect the UART interface of both Pmods to your Test & Measurement device as presented below. Pay special attention to the jumpers on the Pmod CLS. Use the positive, or the digital power supply to provide current to the Pmods, then receive and send data with the protocol instrument. | Connect the UART interface of both Pmods to your Test & Measurement device as presented below. Pay special attention to the jumpers on the Pmod CLS. Use the positive, or the digital power supply to provide current to the Pmods, then receive and send data with the protocol instrument. | ||
- | {{ : | + | {{ : |
- | </ | + | </ |
- | <script src="https:// | + | from WF_SDK import device, supplies, static |
- | </html></ | + | from WF_SDK.protocol import uart # import protocol instrument |
+ | |||
+ | from time import sleep # needed for delays | ||
+ | |||
+ | device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | device_data.name = device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # define | ||
+ | reset = 2 | ||
+ | |||
+ | # define timeout iteration count | ||
+ | timeout = 1000 | ||
+ | |||
+ | # start the power supplies | ||
+ | supplies_data = supplies.data() | ||
+ | supplies_data.master_state | ||
+ | supplies_data.state | ||
+ | supplies_data.voltage = 3.3 | ||
+ | supplies.switch(device_data, | ||
+ | sleep(0.1) | ||
+ | |||
+ | # initialize the reset line | ||
+ | static.set_mode(device_data, | ||
+ | static.set_state(device_data, | ||
+ | |||
+ | # initialize the uart interface | ||
+ | uart.open(device_data, | ||
+ | |||
+ | try: | ||
+ | # repeat | ||
+ | while True: | ||
+ | # clear the screen and home cursor | ||
+ | uart.write(device_data, | ||
+ | |||
+ | # display a message | ||
+ | uart.write(device_data, | ||
+ | |||
+ | # read raw data | ||
+ | static.set_state(device_data, | ||
+ | message = "" | ||
+ | for _ in range(timeout): | ||
+ | # wait for data | ||
+ | message, error = uart.read(device_data) | ||
+ | if message != "": | ||
+ | # exit when data is received | ||
+ | break | ||
+ | static.set_state(device_data, | ||
+ | |||
+ | # convert raw data into distance | ||
+ | try: | ||
+ | if message[0] == 234: | ||
+ | message.pop(0) | ||
+ | value = 0 | ||
+ | for element in message: | ||
+ | if element | ||
+ | # concatenate valid bytes | ||
+ | value = value * 10 + (element - 48) | ||
+ | value *= 2.54 # convert to cm | ||
+ | except: | ||
+ | # error in message | ||
+ | value = -1 | ||
+ | |||
+ | # display the distance | ||
+ | uart.write(device_data, | ||
+ | |||
+ | # display a message | ||
+ | uart.write(device_data, | ||
+ | |||
+ | # delay 1s | ||
+ | sleep(1) | ||
+ | |||
+ | except KeyboardInterrupt: | ||
+ | # exit on Ctrl+C | ||
+ | pass | ||
+ | |||
+ | # reset the interface | ||
+ | uart.close(device_data) | ||
+ | |||
+ | # reset the static I/O | ||
+ | static.set_mode(device_data, | ||
+ | static.set_state(device_data, | ||
+ | static.close(device_data) | ||
+ | |||
+ | # stop and reset the power supplies | ||
+ | supplies_data.master_state = False | ||
+ | supplies.switch(device_data, | ||
+ | supplies.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </code></ | ||
<-- | <-- | ||
Line 702: | Line 1819: | ||
Connect the SPI interface of both Pmods to your Test & Measurement device as presented below. Pay special attention to the jumpers on the Pmod CLS. Use the positive, or the digital power supply to provide current to the Pmods, then receive and send data with the protocol instrument. | Connect the SPI interface of both Pmods to your Test & Measurement device as presented below. Pay special attention to the jumpers on the Pmod CLS. Use the positive, or the digital power supply to provide current to the Pmods, then receive and send data with the protocol instrument. | ||
- | {{ : | + | {{ : |
- | </ | + | </ |
- | <script src="https:// | + | from WF_SDK import device, supplies |
- | </html></ | + | from WF_SDK.protocol import spi # import protocol instrument |
+ | |||
+ | from time import sleep # needed for delays | ||
+ | |||
+ | device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | device_data.name = device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # define chip select lines | ||
+ | CLS_cs = 0 | ||
+ | ALS_cs = 1 | ||
+ | |||
+ | # start the power supplies | ||
+ | supplies_data = supplies.data() | ||
+ | supplies_data.master_state | ||
+ | supplies_data.state | ||
+ | supplies_data.voltage = 3.3 | ||
+ | supplies.switch(device_data, | ||
+ | |||
+ | # initialize the spi interface | ||
+ | spi.open(device_data, | ||
+ | spi.open(device_data, | ||
+ | |||
+ | try: | ||
+ | # repeat | ||
+ | while True: | ||
+ | # clear the screen and home cursor | ||
+ | spi.write(device_data, | ||
+ | |||
+ | # display a message | ||
+ | spi.write(device_data, | ||
+ | |||
+ | # read the temperature | ||
+ | message = spi.read(device_data, | ||
+ | value = ((int(message[0]) | ||
+ | |||
+ | # display the temperature | ||
+ | spi.write(device_data, | ||
+ | |||
+ | # display a message | ||
+ | spi.write(device_data, | ||
+ | |||
+ | # delay 1s | ||
+ | sleep(1) | ||
+ | |||
+ | except KeyboardInterrupt: | ||
+ | # exit on Ctrl+C | ||
+ | pass | ||
+ | |||
+ | # reset the interface | ||
+ | spi.close(device_data) | ||
+ | |||
+ | # stop and reset the power supplies | ||
+ | supplies_data.master_state = False | ||
+ | supplies.switch(device_data, | ||
+ | supplies.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </code></ | ||
<-- | <-- | ||
Line 712: | Line 1896: | ||
Connect the I2C interface of both Pmods to your Test & Measurement device as presented below. Pay special attention to the jumpers on the Pmod CLS. Use the positive, or the digital power supply to provide current to the Pmods, then receive and send data with the protocol instrument. | Connect the I2C interface of both Pmods to your Test & Measurement device as presented below. Pay special attention to the jumpers on the Pmod CLS. Use the positive, or the digital power supply to provide current to the Pmods, then receive and send data with the protocol instrument. | ||
- | {{ : | + | {{ : |
- | </ | + | </ |
- | <script src="https:// | + | from WF_SDK import device, supplies |
- | </html></ | + | from WF_SDK.protocol import i2c # import protocol instrument |
+ | |||
+ | from time import sleep # needed for delays | ||
+ | |||
+ | device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | device_data.name = device_name | ||
+ | |||
+ | """ | ||
+ | |||
+ | # define i2c addresses | ||
+ | CLS_address = 0x48 | ||
+ | TMP2_address = 0x4B | ||
+ | |||
+ | # start the power supplies | ||
+ | supplies_data = supplies.data() | ||
+ | supplies_data.master_state | ||
+ | supplies_data.state | ||
+ | supplies_data.voltage = 3.3 | ||
+ | supplies.switch(device_data, | ||
+ | sleep(0.1) | ||
+ | |||
+ | # initialize the i2c interface | ||
+ | i2c.open(device_data, | ||
+ | |||
+ | # initialize the PMOD TMP2 (set output size to 16-bit) | ||
+ | i2c.write(device_data, | ||
+ | |||
+ | # save custom character | ||
+ | i2c.write(device_data, | ||
+ | i2c.write(device_data, | ||
+ | |||
+ | try: | ||
+ | # repeat | ||
+ | while True: | ||
+ | # clear the screen and home cursor | ||
+ | i2c.write(device_data, | ||
+ | |||
+ | # display a message | ||
+ | i2c.write(device_data, | ||
+ | |||
+ | # read the temperature | ||
+ | message, error = i2c.read(device_data, | ||
+ | value = (int(message[0]) | ||
+ | if ((value | ||
+ | value /= 128 # decode positive numbers | ||
+ | else: | ||
+ | value = (value - 65535) / 128 # decode negative numbers | ||
+ | |||
+ | # display the temperature | ||
+ | i2c.write(device_data, | ||
+ | |||
+ | # display a message | ||
+ | i2c.write(device_data, | ||
+ | i2c.write(device_data, | ||
+ | |||
+ | # delay 1s | ||
+ | sleep(1) | ||
+ | |||
+ | except KeyboardInterrupt: | ||
+ | # exit on Ctrl+C | ||
+ | pass | ||
+ | |||
+ | # reset the interface | ||
+ | i2c.close(device_data) | ||
+ | |||
+ | # stop and reset the power supplies | ||
+ | supplies_data.master_state = False | ||
+ | supplies.switch(device_data, | ||
+ | supplies.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </code></ | ||
<-- | <-- | ||
---- | ---- | ||
+ | ==== Installing the Package ==== | ||
+ | <WRAP group> | ||
+ | Once you completed the package, you might want to install it, like other Python packages and use it on new projects as well. To do so, you must create some additional files in the project folder. | ||
+ | |||
+ | First, exclude the test scripts from the final package. Create a file named **MANIFEST.in**, | ||
+ | < | ||
+ | |||
+ | The installer needs the list of dependencies to install your package. Specify this list on a file called **requirements.txt**: | ||
+ | < | ||
+ | setuptools==58.1.0 | ||
+ | wheel==0.37.1</ | ||
+ | |||
+ | Finally, create a **README.md** file with the description of your package, then create the installer. The installer is the file named **setup.py**, | ||
+ | <code python> | ||
+ | from setuptools import setup | ||
+ | |||
+ | with open(" | ||
+ | long_description = f.read() | ||
+ | |||
+ | setup( | ||
+ | name = " | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | url = " | ||
+ | | ||
+ | ) | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP group> | ||
+ | Once the necessary files are created, open a terminal, go to the project folder and install your package with the following command: | ||
+ | < | ||
+ | |||
+ | Alternatively, | ||
+ | < | ||
+ | |||
+ | If you already installed the package, you can update it with the command: | ||
+ | < | ||
+ | |||
+ | **Note:** //Use " | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== Other Programming Languages ===== | ||
+ | <WRAP group> | ||
+ | Realizing the same package in other programming languages is also possible. To check the C++ version of the package and some test programs, follow this [[https:// | ||
+ | </ | ||
+ | ---- | ||
===== Next Steps ===== | ===== Next Steps ===== | ||
<WRAP group> | <WRAP group> | ||
Line 727: | Line 2042: | ||
For more information on WaveForms visit the [[software: | For more information on WaveForms visit the [[software: | ||
- | For technical support, please visit the [[https:// | + | For technical support, please visit the [[https:// |
</ | </ |