Measurement Computing recently released an official Python API for Windows. Python is a very attractive option for creating the glue code required to bring your project together quickly and painlessly. By providing direct access to the MCC hardware, this API should allow for new and exciting possibilities for users of MCC hardware in Python.
The Python API simplifies acquisition, but it can do so much more than that. The Python Package Index (PyPI) lists over 100,000 open-source packages, so you won’t have to reinvent the wheel. Python provides the flexibility to extend and expand your application quickly and easily, by eliminating many of the code maintenance tasks required in other languages. It focuses on code readability, and a shallow learning curve. It has the potential to save you time and money that may have otherwise been spent fighting with language details.
The are many success stories listed on the Python website and elsewhere. Particularly interesting is the story from the United Space Alliance, in which a developer single-handedly designed a data management application for NASA in less than a year. Though admittedly not directly related to data acquisition, it highlights how Python tends to be easier to use than other languages.
I’ve outlined some use cases below.
Python provides several data analysis packages, such as NumPy, SciPy, and Matplotlib. These will aid you in analyzing the data you’ve acquired from the hardware. Now, your capture and analysis can be done in the same program with direct access to the data in real-time. By cutting out the middle-man, you no longer need to export the data to a file and re-import it into a complex analysis package.
Perhaps you’d like to accelerate your analysis or free up your CPU for other tasks. With packages like PyOpenCL, you can offload your analysis to the GPU. This is simplified with additional packages that run alongside or on top of PyOpenCL. For example, pyclblas runs linear algebra (BLAS routines) on the GPU and the gpyfft package provides a GPU-accelerated FFT library.
Using the Python API and a package such as scikit-learn or PyBrain, you can use Machine Learning on signals from the real world. This could be useful for identifying signals that have complex mathematical models, or that vary enough such that traditional detection models are inaccurate and unreliable.
Measurement Computing devices have options to both read and output analog and digital data. After analyzing the data using the packages above, the same hardware can be used to control an entire system. An example pulled from the Python success stories is a CD/DVD packaging control system.
We previously posted an article about how to create a temperature two-point controller, and used DASYlab, LabVIEW, and VB.NET to create the application. The Python API could have also been used. Below is the code that would replace the DASYlab worksheet. In the interest of keeping the example simple for this article, a UI has not been added.
from mcculw import ul from mcculw.enums import TempScale, TInOptions, DigitalPortType if __name__ == '__main__': board_num = 0 setpoint = 70 while True: temp = ul.t_in( board_num, 0, TempScale.FAHRENHEIT, TInOptions.WAITFORNEWDATA) if temp < setpoint: ul.d_bit_out(board_num, DigitalPortType.AUXPORT, 0, 1) else: ul.d_bit_out(board_num, DigitalPortType.AUXPORT, 0, 0)
Python has been used to develop numerous websites, including YouTube, DropBox, and even Google. Using a framework like Django or Flask, your data acquisition device can be controlled over the internet. Maybe you don’t need an entire platform, and just need regular e-mail notifications – Python ships with an SMTP module, smtplib, which would make this a breeze.
This is just the surface of what could be done with the Python API. We’d love to hear from you about your experiences with Python and its potential applications. Leave a comment below to share your thoughts!