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 [2022/05/13 13:39] – [3. Using Instruments] Á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 WaveForms SDK ====== | ||
+ | <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. | ||
+ | Normally, Test and Measurement devices are controlled and configured through the WaveForms application with a personal computer. Such a setup may be impossible in a given context, or an amount of automated signal measurement may be required beyond what WaveForms' | ||
+ | |||
+ | 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# | ||
+ | </ | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== Inventory ===== | ||
+ | <WRAP group> | ||
+ | * 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: | ||
+ | * A Computer with WaveForms Installed | ||
+ | * Both the WaveForms application and WaveForms SDK can be installed by following the [[software: | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== SDK Overview ===== | ||
+ | <WRAP group> | ||
+ | WaveForms SDK is included with WaveForms and is installed alongside the application. The SDK is available to use with C/C++, C#, Python, and Visual Basic through a dynamic library (a module that contains data that can be used by other 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, | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== Workflow ===== | ||
+ | ==== 1. Importing the Constants and Loading the Dynamic Library ==== | ||
+ | <WRAP group>< | ||
+ | The WaveForms SDK Python functions use C-compatible data types, so along with the dynamic library and the module containing the constants, you will also need the **ctypes** module, which is installed together with Python by default. | ||
+ | |||
+ | 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. | ||
+ | </ | ||
+ | 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 | ||
+ | |||
+ | # load the dynamic library, get constants path (the path is OS specific) | ||
+ | if platform.startswith(" | ||
+ | # on Windows | ||
+ | dwf = ctypes.cdll.dwf | ||
+ | constants_path = " | ||
+ | elif platform.startswith(" | ||
+ | # on macOS | ||
+ | lib_path = sep + " | ||
+ | dwf = ctypes.cdll.LoadLibrary(lib_path) | ||
+ | constants_path = sep + " | ||
+ | else: | ||
+ | # on Linux | ||
+ | dwf = ctypes.cdll.LoadLibrary(" | ||
+ | constants_path = sep + " | ||
+ | |||
+ | # import constants | ||
+ | path.append(constants_path) | ||
+ | import dwfconstants as constants | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ==== 2. Connecting the Test & Measurement Device ==== | ||
+ | <WRAP group>< | ||
+ | The next step is to " | ||
+ | |||
+ | Opening a specific device and retrieving the name of the connected device is also possible (for more information, | ||
+ | </ | ||
+ | class data: | ||
+ | """ | ||
+ | stores the device handle and the device name | ||
+ | """ | ||
+ | handle = ctypes.c_int(0) | ||
+ | name = "" | ||
+ | |||
+ | def open(): | ||
+ | """ | ||
+ | open the first available device | ||
+ | """ | ||
+ | # this is the device handle - it will be used by all functions to " | ||
+ | device_handle = ctypes.c_int() | ||
+ | # connect to the first available device | ||
+ | dwf.FDwfDeviceOpen(ctypes.c_int(-1), | ||
+ | data.handle = device_handle | ||
+ | return data | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ==== 3. Using Instruments ==== | ||
+ | <WRAP group> | ||
+ | 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>< | ||
+ | Before measuring with the oscilloscope, | ||
+ | </ | ||
+ | class data: | ||
+ | """ | ||
+ | sampling_frequency = 20e06 | ||
+ | buffer_size = 8192 | ||
+ | |||
+ | 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>< | ||
+ | You can measure voltages, like with the Voltmeter instrument in WaveForms. | ||
+ | </ | ||
+ | def measure(device_data, | ||
+ | """ | ||
+ | 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, | ||
+ | | ||
+ | # read data to an internal buffer | ||
+ | dwf.FDwfAnalogInStatus(device_data.handle, | ||
+ | | ||
+ | # extract data from that buffer | ||
+ | voltage = ctypes.c_double() | ||
+ | dwf.FDwfAnalogInStatusSample(device_data.handle, | ||
+ | | ||
+ | # store the result as float | ||
+ | voltage = voltage.value | ||
+ | return voltage | ||
+ | </ | ||
+ | |||
+ | === 3.1.3 Record a Signal === | ||
+ | <WRAP group>< | ||
+ | The most important feature of the oscilloscope is, that it can record signals. The recorded voltages can be stored in a list. | ||
+ | </ | ||
+ | def record(device_data, | ||
+ | """ | ||
+ | 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, | ||
+ | | ||
+ | # read data to an internal buffer | ||
+ | while True: | ||
+ | status = ctypes.c_byte() | ||
+ | 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 / data.sampling_frequency for moment in time] | ||
+ | | ||
+ | # convert into list | ||
+ | buffer = [float(element) for element in buffer] | ||
+ | return buffer, time | ||
+ | </ | ||
+ | |||
+ | === 3.1.4 Reset the Scope === | ||
+ | <WRAP group>< | ||
+ | After usage, reset the oscilloscope to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the scope | ||
+ | """ | ||
+ | dwf.FDwfAnalogInReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.2 Waveform Generator # | ||
+ | <WRAP group> | ||
+ | === 3.2.1 Generate a Signal === | ||
+ | <WRAP group>< | ||
+ | Use the waveform generator to generate different signals. | ||
+ | |||
+ | <WRAP center round tip 80%> | ||
+ | You can define custom function names, to make the usage of the function easier. | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | class function: | ||
+ | """ | ||
+ | custom = constants.funcCustom | ||
+ | sine = constants.funcSine | ||
+ | square = constants.funcSquare | ||
+ | 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 | ||
+ | </ | ||
+ | </ | ||
+ | def generate(device_data, | ||
+ | """ | ||
+ | generate an analog signal | ||
+ | parameters: - device data | ||
+ | - the selected wavegen channel (1-2) | ||
+ | - 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 - 1) | ||
+ | dwf.FDwfAnalogOutNodeEnableSet(device_data.handle, | ||
+ | | ||
+ | # set function type | ||
+ | dwf.FDwfAnalogOutNodeFunctionSet(device_data.handle, | ||
+ | | ||
+ | # load data if the function type is custom | ||
+ | if function == constants.funcCustom: | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | === 3.2.2 Reset the Wavegen === | ||
+ | <WRAP group>< | ||
+ | After usage, reset the wavegen to the default settings. | ||
+ | </ | ||
+ | def close(device_data, | ||
+ | """ | ||
+ | reset a wavegen channel, or all channels (channel=0) | ||
+ | """ | ||
+ | channel = ctypes.c_int(channel - 1) | ||
+ | dwf.FDwfAnalogOutReset(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.3 Power Supplies # | ||
+ | <WRAP group> | ||
+ | --> 3.3.1 Analog Discovery 3 Supplies # | ||
+ | <WRAP group>< | ||
+ | The Analog Discovery 3 has variable positive and negative supplies to set voltage levels. | ||
+ | </ | ||
+ | def _switch_variable_(device_data, | ||
+ | """ | ||
+ | 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/ | ||
+ | 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 | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.3.2 Analog Discovery 2 and Analog Discovery Studio Supplies # | ||
+ | <WRAP group>< | ||
+ | These devices have variable positive and negative supplies, so a voltage level can also be set. | ||
+ | </ | ||
+ | def _switch_variable_(device_data, | ||
+ | """ | ||
+ | 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/ | ||
+ | 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 | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 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: - device data | ||
+ | - master switch - True = on, False = off | ||
+ | - positive supply switch - True = on, False = off | ||
+ | - negative supply switch - True = on, False = off | ||
+ | """ | ||
+ | # enable/ | ||
+ | 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 | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.3.4 Analog Discovery Pro 3X50 and Digital Discovery Supplies # | ||
+ | <WRAP group>< | ||
+ | Devices with digital supplies have only positive voltage supplies with a variable voltage level between 1.2 and 3.3 Volts. | ||
+ | </ | ||
+ | def _switch_digital_(device_data, | ||
+ | """ | ||
+ | turn the power supplies on/off | ||
+ | parameters: - device data | ||
+ | - master switch - True = on, False = off | ||
+ | - 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 | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.3.5 Analog Discovery Pro 5250 6V Power Supply # | ||
+ | <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. | ||
+ | </ | ||
+ | def _switch_6V_(device_data, | ||
+ | """ | ||
+ | turn the 6V supply on the ADP5250 on/off | ||
+ | parameters: - master switch - True = on, False = off | ||
+ | - voltage in volts between 0-6 | ||
+ | - current in amperes between 0-1 | ||
+ | """ | ||
+ | # set the voltage | ||
+ | voltage = max(0, min(6, voltage)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | | ||
+ | # set the current | ||
+ | current = max(0, min(1, current)) | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | | ||
+ | # start/stop the supply - master switch | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.3.6 Analog Discovery Pro 5250 25V Power Supplies # | ||
+ | <WRAP group>< | ||
+ | The positive and negative isolated 25V power supplies are similar to the 6V one, but with a maximum current limit of 500mA. | ||
+ | </ | ||
+ | def _switch_25V_(device_data, | ||
+ | """ | ||
+ | turn the 25V power supplies on/off on the ADP5250 | ||
+ | parameters: - positive supply switch - True = on, False = off | ||
+ | - negative supply switch - True = on, False = off | ||
+ | - 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/ | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | | ||
+ | # master switch | ||
+ | dwf.FDwfAnalogIOEnableSet(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | === 3.3.7 Wrapper Function | ||
+ | <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 (if the device name is set). | ||
+ | </ | ||
+ | class data: | ||
+ | """ | ||
+ | master_state = False # master switch | ||
+ | state = False # digital/ | ||
+ | positive_state = False # positive supply switch | ||
+ | negative_state = False # negative supply switch | ||
+ | positive_voltage = 0 # positive supply voltage | ||
+ | negative_voltage = 0 # negative supply voltage | ||
+ | voltage = 0 # digital/ | ||
+ | positive_current = 0 # positive supply current | ||
+ | negative_current = 0 # negative supply current | ||
+ | current = 0 # digital/6V supply current | ||
+ | |||
+ | 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 = supplies_data.state or supplies_data.positive_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>< | ||
+ | After usage, reset the supplies to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the supplies | ||
+ | """ | ||
+ | dwf.FDwfAnalogIOReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.4 Digital Multimeter - Only on ADP5250 # | ||
+ | <WRAP group> | ||
+ | === 3.4.1 Initialize the DMM === | ||
+ | <WRAP group>< | ||
+ | Before measuring with the digital multimeter, it must be enabled. | ||
+ | </ | ||
+ | def open(device_data): | ||
+ | """ | ||
+ | initialize the digital multimeter | ||
+ | """ | ||
+ | # enable the DMM | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.4.2 Measure With the DMM === | ||
+ | <WRAP group>< | ||
+ | You can use the digital multimeter to measure AC, or DC voltages (in Volts), with an input impedance of 10MΩ, or 10GΩ, low (< | ||
+ | </ | ||
+ | def measure(device_data, | ||
+ | """ | ||
+ | measure a voltage/ | ||
+ | parameters: - device data | ||
+ | - mode: " | ||
+ | - ac: True means AC value, False means DC value, default is DC | ||
+ | - range: voltage/ | ||
+ | - high_impedance: | ||
+ | | ||
+ | returns: | ||
+ | """ | ||
+ | # set voltage mode | ||
+ | if mode == " | ||
+ | # set coupling | ||
+ | if ac: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | else: | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | |||
+ | # 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>< | ||
+ | After usage, reset the instrument to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the instrument | ||
+ | """ | ||
+ | # disable the DMM | ||
+ | dwf.FDwfAnalogIOChannelNodeSet(device_data.handle, | ||
+ | # reset the instrument | ||
+ | dwf.FDwfAnalogIOReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.5 Logic Analyzer # | ||
+ | <WRAP group> | ||
+ | === 3.5.1 Initialize the Logic Analyzer === | ||
+ | <WRAP group>< | ||
+ | Before measuring with the logic analyzer, it must be set up. Change the values to fit your needs. | ||
+ | </ | ||
+ | class data: | ||
+ | """ | ||
+ | sampling_frequency = 100e06 | ||
+ | buffer_size = 4096 | ||
+ | |||
+ | 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>< | ||
+ | Record logic signals in a list of lists, then select the one specific for the required DIO line. | ||
+ | </ | ||
+ | def record(device_data, | ||
+ | """ | ||
+ | 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, | ||
+ | | ||
+ | # read data to an internal buffer | ||
+ | while True: | ||
+ | status = ctypes.c_byte() | ||
+ | dwf.FDwfDigitalInStatus(device_data.handle, | ||
+ | | ||
+ | if status.value == constants.stsDone.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 / data.sampling_frequency for moment in time] | ||
+ | | ||
+ | # get channel specific data | ||
+ | buffer = result[channel] | ||
+ | return buffer, time | ||
+ | </ | ||
+ | |||
+ | === 3.5.3 Reset the Logic Analyzer === | ||
+ | <WRAP group>< | ||
+ | After usage, reset the logic analyzer to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the instrument | ||
+ | """ | ||
+ | dwf.FDwfDigitalInReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.6 Pattern Generator # | ||
+ | <WRAP group> | ||
+ | === 3.6.1 Generate Logic Signals === | ||
+ | <WRAP group>< | ||
+ | Configure the pattern generator to generate logic signals. | ||
+ | |||
+ | <WRAP center round important 80%> | ||
+ | You can use a DIO line for pattern generation only if the respective line is configured as input and set to LOW state by the static I/O instrument (these are the default settings for all lines). | ||
+ | </ | ||
+ | |||
+ | <WRAP center round tip 80%> | ||
+ | You can define custom function, trigger source and idle state names, to make the usage of the function easier. | ||
+ | </ | ||
+ | |||
+ | <code python> | ||
+ | class function: | ||
+ | """ | ||
+ | pulse = constants.DwfDigitalOutTypePulse | ||
+ | custom = constants.DwfDigitalOutTypeCustom | ||
+ | random = constants.DwfDigitalOutTypeRandom | ||
+ | |||
+ | class trigger_source: | ||
+ | """ | ||
+ | none = constants.trigsrcNone | ||
+ | analog = constants.trigsrcDetectorAnalogIn | ||
+ | digital = constants.trigsrcDetectorDigitalIn | ||
+ | external = [None, constants.trigsrcExternal1, | ||
+ | |||
+ | 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>< | ||
+ | After usage, reset the pattern generator to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the instrument | ||
+ | """ | ||
+ | dwf.FDwfDigitalOutReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.7 Static I/O # | ||
+ | <WRAP group> | ||
+ | === 3.7.1 Set Pins As Input Or As Output === | ||
+ | <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. | ||
+ | </ | ||
+ | def set_mode(device_data, | ||
+ | """ | ||
+ | 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: | ||
+ | | ||
+ | # set bit in mask | ||
+ | if output: | ||
+ | mask[15 - channel] = " | ||
+ | else: | ||
+ | mask[15 - channel] = " | ||
+ | | ||
+ | # convert mask to number | ||
+ | mask = "" | ||
+ | mask = int(mask, 2) | ||
+ | | ||
+ | # set the pin to output | ||
+ | dwf.FDwfDigitalIOOutputEnableSet(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.7.2 Get Pin State === | ||
+ | <WRAP group>< | ||
+ | Read the state of a DIO line with the following code snippet: | ||
+ | </ | ||
+ | def get_state(device_data, | ||
+ | """ | ||
+ | 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] != " | ||
+ | value = True | ||
+ | else: | ||
+ | value = False | ||
+ | return value | ||
+ | </ | ||
+ | |||
+ | === 3.7.3 Set Pin State === | ||
+ | <WRAP group>< | ||
+ | To set the state of a DIO line, it must be set as output! | ||
+ | </ | ||
+ | def set_state(device_data, | ||
+ | """ | ||
+ | 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: | ||
+ | | ||
+ | # set bit in mask | ||
+ | if value: | ||
+ | mask[15 - channel] = " | ||
+ | else: | ||
+ | mask[15 - channel] = " | ||
+ | | ||
+ | # convert mask to number | ||
+ | mask = "" | ||
+ | mask = int(mask, 2) | ||
+ | | ||
+ | # set the pin state | ||
+ | dwf.FDwfDigitalIOOutputSet(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.7.4 Reset the Static I/O === | ||
+ | <WRAP group>< | ||
+ | After usage, reset the instrument to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the instrument | ||
+ | """ | ||
+ | dwf.FDwfDigitalIOReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.8 Protocol: UART # | ||
+ | <WRAP group> | ||
+ | === 3.8.1 Initialize the Interface === | ||
+ | <WRAP group>< | ||
+ | Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | ||
+ | </ | ||
+ | def open(device_data, | ||
+ | """ | ||
+ | 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, | ||
+ | |||
+ | # 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 == True: | ||
+ | parity = 2 | ||
+ | elif parity == False: | ||
+ | parity = 1 | ||
+ | else: | ||
+ | parity = 0 | ||
+ | dwf.FDwfDigitalUartParitySet(device_data.handle, | ||
+ | |||
+ | # set stop bit count | ||
+ | dwf.FDwfDigitalUartStopSet(device_data.handle, | ||
+ | |||
+ | # initialize channels with idle levels | ||
+ | |||
+ | # dummy read | ||
+ | dummy_buffer = ctypes.create_string_buffer(0) | ||
+ | dummy_buffer = ctypes.c_int(0) | ||
+ | dummy_parity_flag = ctypes.c_int(0) | ||
+ | dwf.FDwfDigitalUartRx(device_data.handle, | ||
+ | |||
+ | # dummy write | ||
+ | dwf.FDwfDigitalUartTx(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.8.2 Receive Data === | ||
+ | <WRAP group>< | ||
+ | Use the function to the right to read data on an initialized UART interface. | ||
+ | </ | ||
+ | def read(device_data): | ||
+ | """ | ||
+ | receives data from UART | ||
+ | | ||
+ | parameters: - device data | ||
+ | return: | ||
+ | - error message or empty string | ||
+ | """ | ||
+ | # variable to store results | ||
+ | error = "" | ||
+ | rx_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>< | ||
+ | Use the function to the right to send data on an initialized UART interface to another device. | ||
+ | </ | ||
+ | def write(device_data, | ||
+ | """ | ||
+ | send data through UART | ||
+ | | ||
+ | parameters: - data of type string, int, or list of characters/ | ||
+ | """ | ||
+ | # cast data | ||
+ | if type(data) == int: | ||
+ | data = "" | ||
+ | elif type(data) == list: | ||
+ | data = "" | ||
+ | |||
+ | # 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>< | ||
+ | After usage, reset the instrument to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the uart interface | ||
+ | """ | ||
+ | dwf.FDwfDigitalUartReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.9 Protocol: SPI # | ||
+ | <WRAP group> | ||
+ | === 3.9.1 Initialize the Interface === | ||
+ | <WRAP group>< | ||
+ | Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | ||
+ | </ | ||
+ | def open(device_data, | ||
+ | """ | ||
+ | initializes SPI communication | ||
+ | parameters: - device data | ||
+ | - cs (DIO line used for chip select) | ||
+ | - sck (DIO line used for serial clock) | ||
+ | - miso (DIO line used for master in - slave out, optional) | ||
+ | - 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, | ||
+ | |||
+ | # 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>< | ||
+ | Use the function to the right to read data on an initialized SPI interface. | ||
+ | </ | ||
+ | def read(device_data, | ||
+ | """ | ||
+ | receives data from SPI | ||
+ | parameters: - device data | ||
+ | - count (number of bytes to receive) | ||
+ | - chip select line number | ||
+ | return: | ||
+ | """ | ||
+ | # enable the chip select line | ||
+ | dwf.FDwfDigitalSpiSelect(device_data.handle, | ||
+ | |||
+ | # 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>< | ||
+ | Use the function to the right to send data on an initialized SPI interface to another device. | ||
+ | </ | ||
+ | def write(device_data, | ||
+ | """ | ||
+ | send data through SPI | ||
+ | parameters: - device data | ||
+ | - data of type string, int, or list of characters/ | ||
+ | - chip select line number | ||
+ | """ | ||
+ | # cast data | ||
+ | if type(data) == int: | ||
+ | data = "" | ||
+ | elif type(data) == list: | ||
+ | data = "" | ||
+ | |||
+ | # enable the chip select line | ||
+ | dwf.FDwfDigitalSpiSelect(device_data.handle, | ||
+ | |||
+ | # create buffer to write | ||
+ | data = bytes(data, " | ||
+ | buffer = (ctypes.c_ubyte * len(data))() | ||
+ | for index in range(0, len(buffer)): | ||
+ | buffer[index] = ctypes.c_ubyte(data[index]) | ||
+ | |||
+ | # write array of 8 bit elements | ||
+ | dwf.FDwfDigitalSpiWrite(device_data.handle, | ||
+ | |||
+ | # disable the chip select line | ||
+ | dwf.FDwfDigitalSpiSelect(device_data.handle, | ||
+ | return | ||
+ | </ | ||
+ | |||
+ | === 3.9.4 Reset the Interface === | ||
+ | <WRAP group>< | ||
+ | After usage, reset the instrument to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the spi interface | ||
+ | """ | ||
+ | dwf.FDwfDigitalSpiReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> 3.10 Protocol: I2C # | ||
+ | <WRAP group> | ||
+ | === 3.10.1 Initialize the Interface === | ||
+ | <WRAP group>< | ||
+ | Before using a communication interface, it must be initialized by setting the communication parameters to the desired values. | ||
+ | </ | ||
+ | def open(device_data, | ||
+ | """ | ||
+ | initializes I2C communication | ||
+ | parameters: - device data | ||
+ | - sda (DIO line used for data) | ||
+ | - scl (DIO line used for clock) | ||
+ | - rate (clock frequency in Hz, default is 100KHz) | ||
+ | - stretching (enables/ | ||
+ | returns: | ||
+ | """ | ||
+ | # reset the interface | ||
+ | dwf.FDwfDigitalI2cReset(device_data.handle) | ||
+ | |||
+ | # 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>< | ||
+ | Use the function to the right to read data on an initialized I2C interface. | ||
+ | </ | ||
+ | def read(device_data, | ||
+ | """ | ||
+ | receives data from I2C | ||
+ | | ||
+ | parameters: - device data | ||
+ | - count (number of bytes to receive) | ||
+ | - address (8-bit address of the slave device) | ||
+ | | ||
+ | return: | ||
+ | - error message or empty string | ||
+ | """ | ||
+ | # create buffer to store data | ||
+ | buffer = (ctypes.c_ubyte * count)() | ||
+ | |||
+ | # receive | ||
+ | nak = ctypes.c_int() | ||
+ | dwf.FDwfDigitalI2cRead(device_data.handle, | ||
+ | |||
+ | # decode data | ||
+ | data = [int(element) for element in buffer] | ||
+ | |||
+ | # check for not acknowledged | ||
+ | if nak.value != 0: | ||
+ | return data, "NAK: index " + str(nak.value) | ||
+ | return data, "" | ||
+ | </ | ||
+ | |||
+ | === 3.10.3 Send Data === | ||
+ | <WRAP group>< | ||
+ | Use the function to the right to send data on an initialized I2C interface to another device. | ||
+ | </ | ||
+ | def write(device_data, | ||
+ | """ | ||
+ | send data through I2C | ||
+ | | ||
+ | parameters: - device data | ||
+ | - data of type string, int, or list of characters/ | ||
+ | - address (8-bit address of the slave device) | ||
+ | | ||
+ | returns: | ||
+ | """ | ||
+ | # cast data | ||
+ | if type(data) == int: | ||
+ | data = "" | ||
+ | elif type(data) == list: | ||
+ | data = "" | ||
+ | |||
+ | # encode the string into a string buffer | ||
+ | data = bytes(data, " | ||
+ | buffer = (ctypes.c_ubyte * len(data))() | ||
+ | for index in range(0, len(buffer)): | ||
+ | buffer[index] = ctypes.c_ubyte(data[index]) | ||
+ | |||
+ | # send | ||
+ | nak = ctypes.c_int() | ||
+ | dwf.FDwfDigitalI2cWrite(device_data.handle, | ||
+ | |||
+ | # check for not acknowledged | ||
+ | if nak.value != 0: | ||
+ | return "NAK: index " + str(nak.value) | ||
+ | return "" | ||
+ | </ | ||
+ | |||
+ | === 3.10.4 Reset the Interface === | ||
+ | <WRAP group>< | ||
+ | After usage, reset the instrument to the default settings. | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | reset the i2c interface | ||
+ | """ | ||
+ | dwf.FDwfDigitalI2cReset(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | </ | ||
+ | <-- | ||
+ | ---- | ||
+ | ==== 4. Disconnecting the Device ==== | ||
+ | <WRAP group>< | ||
+ | When your script is exiting, it is very important to close the opened connections, | ||
+ | </ | ||
+ | def close(device_data): | ||
+ | """ | ||
+ | close a specific device | ||
+ | """ | ||
+ | dwf.FDwfDeviceClose(device_data.handle) | ||
+ | return | ||
+ | </ | ||
+ | ---- | ||
+ | ===== Creating Modules ===== | ||
+ | <WRAP group> | ||
+ | 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. 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 the archive containing the module and some test files [[https:// | ||
+ | </ | ||
+ | |||
+ | <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 < | ||
+ | |||
+ | <code python> | ||
+ | """ | ||
+ | This module realizes communication with Digilent Test & Measurement devices | ||
+ | """ | ||
+ | |||
+ | from WF_SDK import device | ||
+ | from WF_SDK import scope | ||
+ | from WF_SDK import wavegen | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | Remember, that any submodule (the protocol folder in this case) also needs initialization: | ||
+ | |||
+ | <code python> | ||
+ | """ | ||
+ | This module controls the protocol instrument | ||
+ | """ | ||
+ | |||
+ | from WF_SDK.protocol import i2c | ||
+ | from WF_SDK.protocol import spi | ||
+ | from WF_SDK.protocol import uart | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ==== Testing ==== | ||
+ | <WRAP group> | ||
+ | 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. | ||
+ | |||
+ | 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 " | ||
+ | </ | ||
+ | |||
+ | --> Empty Project Template # | ||
+ | <WRAP group>< | ||
+ | Fill in this template. Be creative, use any instrument in any configuration. | ||
+ | </ | ||
+ | from WF_SDK import device | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | |||
+ | """ | ||
+ | |||
+ | # use instruments here | ||
+ | |||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> Using the Oscilloscope and the Waveform Generator # | ||
+ | <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! | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | {{ : | ||
+ | </ | ||
+ | from WF_SDK import device, scope, wavegen | ||
+ | |||
+ | import matplotlib.pyplot as plt # needed for plotting | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | |||
+ | """ | ||
+ | |||
+ | # initialize the scope with default settings | ||
+ | scope.open(device_data) | ||
+ | |||
+ | # generate a 10KHz sine signal with 2V amplitude on channel 1 | ||
+ | wavegen.generate(device_data, | ||
+ | |||
+ | # record data with the scopeon channel 1 | ||
+ | buffer, time = scope.record(device_data, | ||
+ | |||
+ | # plot | ||
+ | time = [moment * 1e03 for moment in time] # convert time to ms | ||
+ | plt.plot(time, | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | plt.show() | ||
+ | |||
+ | # reset the scope | ||
+ | scope.close(device_data) | ||
+ | |||
+ | # reset the wavegen | ||
+ | wavegen.close(device_data) | ||
+ | |||
+ | """ | ||
+ | |||
+ | # close the connection | ||
+ | device.close(device_data) | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> Using the Logic Analyzer and the Pattern Generator # | ||
+ | <WRAP group>< | ||
+ | This example generates a PWM signal on a DIO line and reads it back with the logic analyzer. As the same line is used both as input and as output, no external connections have to be made. | ||
+ | |||
+ | {{ : | ||
+ | </ | ||
+ | from WF_SDK import device, logic, pattern | ||
+ | |||
+ | import matplotlib.pyplot as plt # needed for plotting | ||
+ | |||
+ | """ | ||
+ | |||
+ | # connect to the device | ||
+ | device_data = device.open() | ||
+ | |||
+ | """ | ||
+ | |||
+ | # 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 on DIO0 | ||
+ | buffer, time = logic.record(device_data, | ||
+ | |||
+ | # plot | ||
+ | time = [moment * 1e06 for moment in time] # convert time to μs | ||
+ | plt.plot(time, | ||
+ | plt.xlabel(" | ||
+ | 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) | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> Using the Static I/O and the Power Supplies # | ||
+ | <WRAP group>< | ||
+ | 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). | ||
+ | |||
+ | {{ : | ||
+ | </ | ||
+ | from WF_SDK import device, static, supplies | ||
+ | |||
+ | 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 = True | ||
+ | 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) | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> Controlling the Pmod CLS and the Pmod MAXSonar with UART # | ||
+ | <WRAP group>< | ||
+ | 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. | ||
+ | |||
+ | {{ : | ||
+ | </ | ||
+ | from WF_SDK import device, supplies, static | ||
+ | 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 MAXSonar reset line | ||
+ | reset = 2 | ||
+ | |||
+ | # define timeout iteration count | ||
+ | timeout = 1000 | ||
+ | |||
+ | # start the power supplies | ||
+ | supplies_data = supplies.data() | ||
+ | supplies_data.master_state = True | ||
+ | supplies_data.state = True | ||
+ | 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 on DIO0 and DIO1 | ||
+ | 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 > 47 and element < 58: | ||
+ | # 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) | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> Controlling the Pmod CLS and the Pmod ALS with SPI # | ||
+ | <WRAP group>< | ||
+ | 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. | ||
+ | |||
+ | {{ : | ||
+ | </ | ||
+ | from WF_SDK import device, supplies | ||
+ | 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 = True | ||
+ | supplies_data.state = True | ||
+ | supplies_data.voltage = 3.3 | ||
+ | supplies.switch(device_data, | ||
+ | |||
+ | # initialize the spi interface on DIO0, DIO1, DIO2, DIO3 and DIO4 | ||
+ | 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]) << 3) | (int(message[1]) >> 4)) / 1.27 | ||
+ | |||
+ | # 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) | ||
+ | </ | ||
+ | <-- | ||
+ | |||
+ | --> Controlling the Pmod CLS and the Pmod TMP2 with I2C # | ||
+ | <WRAP group>< | ||
+ | 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. | ||
+ | |||
+ | {{ : | ||
+ | </ | ||
+ | from WF_SDK import device, supplies | ||
+ | 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 = True | ||
+ | supplies_data.state = True | ||
+ | supplies_data.voltage = 3.3 | ||
+ | supplies.switch(device_data, | ||
+ | sleep(0.1) | ||
+ | |||
+ | # initialize the i2c interface on DIO0 and DIO1 | ||
+ | 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]) << 8) | int(message[1]) | ||
+ | if ((value >> 15) & 1) == 0: | ||
+ | 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) | ||
+ | </ | ||
+ | <-- | ||
+ | ---- | ||
+ | |||
+ | ==== 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 ===== | ||
+ | <WRAP group> | ||
+ | For more guides on how to use your Digilent Test & Measurement Device, return to the device' | ||
+ | |||
+ | For more information on the WaveForms SDK visit the [[software: | ||
+ | |||
+ | For more information on WaveForms visit the [[software: | ||
+ | |||
+ | For technical support, please visit the [[https:// | ||
+ | </ |