Learn how to develop your own applications to monitor or control instrumentation hardware. Whether you need to acquire data from a device or automate its functions, this practical book shows you how to use Python's rapid development capabilities to build interfaces that include everything from software to wiring. You get step-by-step instructions, clear examples, and hands-on tips for interfacing a PC to a variety of devices. Use the book's hardware survey to identify the interface type for your particular device, and then follow detailed examples to develop an interface with Python and C. Organized by interface type, data processing activities, and user interface implementations, this book is for anyone who works with instrumentation, robotics, data acquisition, or process control. Understand how to define the scope of an application and determine the algorithms necessary, and why it's important Learn how to use industry-standard interfaces such as RS-232, RS-485, and GPIB Create low-level extension modules in C to interface Python with a variety of hardware and test instruments Explore the console, curses, TkInter, and wxPython for graphical and text-based user interfaces Use open source software tools and libraries to reduce costs and avoid implementing functionality from scratch
Author(s): John Hughes
Publisher: O'Reilly Media
Year: 2010
Language: English
Pages: 624
Copyright
Table of Contents
Preface
Who Is This Book For?
The Programming Languages
Why Python?
The Systems
Methodology
How This Book Is Organized
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Chapter 1. Introduction to Instrumentation
Data Acquisition
Control Output
Open-Loop Control
Closed-Loop Control
Sequential Control
Applications Overview
Electronics Test Instrumentation
Laboratory Instrumentation
Process Control
Summary
Chapter 2. Essential Electronics
Electrical Charge
Electric Current
Basic Circuit Theory
Circuit Schematics
DC Circuit Characteristics
Ohm’s Law
Sinking and Sourcing
More About Resistors
AC Circuits
Sine Waves
Capacitors
Inductors
Other Waveforms: Square, Ramp, Triangle, Pulse
Interfaces
Discrete Digital I/O
Analog I/O
Acquiring analog data
Generating analog data
Counters and Timers
PWM
Serial I/O
Parallel I/O
Summary
Suggested Reading
Chapter 3. The Python Programming Language
Installing Python
The Python Programming Language
The Python Command Line
Command-Line Options and Environment
Objects in Python
Data Types in Python
Numeric data as objects
Sequence objects
Lists
Strings
Tuples
Mapped objects—dictionaries
Expressions
Operators
Arithmetic operators
Logical operators
Comparison operators
Bitwise operators
Assignment operators
Membership operators
Identity operators
Operator precedence
Statements
Indentation
Comments
Keywords
Simple statements
assert
Assignment
Augmented assignment
Compound statements
pass
print
return
break
continue
global
The if statement
The while statement
The for statement
The try statement
Strings
String quotes
String methods
String formatting
Program Organization
Scope
Local scope
Global scope
Modules and packages
Functions, classes, and methods
Built-in scope
Docstrings
Importing Modules
Import methods
Import processing
Cyclic imports
Loading and Running a Python Program
Basic Input and Output
User input
Command-line parameters
Files
Console output using print
Redirecting print
Hints and Tips
Module global variables
Latent defects
Deferred imports
Dictionaries as function parameters
Function return values
Think of modules as objects
Use docstrings and descriptive comments
Coding style
Python Development Tools
Editors and IDEs
Editors
IDE tools
Debuggers
Summary
Suggested Reading
Chapter 4. The C Programming Language
Installing C
Developing Software in C
A Simple C Program
Preprocessor Directives
#include
#define
Standard Data Types
User-Defined Types
Operators
Arithmetic operators
Unary operators
Assignment and augmented assignment operators
Comparison operators
Logical operators
Bitwise operators
Operator precedence
Expressions
Statements
if-else statement
switch statement
while loop
do-while loop
for loop
break statement
continue statement
goto statement
Arrays and Pointers
Arrays
Pointers
Structures
Functions
Function syntax
Function prototypes
The Standard Library
Building C Programs
Header files
Object files
Libraries
Linking
make
C Language Wrap-Up
C Development Tools
Summary
Suggested Reading
Chapter 5. Python Extensions
Creating Python Extensions in C
Python’s C Extension API
Extension Source Module Organization
Python API Types and Functions
The Method Table
Method Flags
METH_VARARGS example
METH_KEYWORDS example
METH_NOARGS example
Passing Data
Using the Python C Extension API
Generic Discrete I/O API
Generic Wrapper Example
Calling the Extension
Python’s ctypes Foreign Function Library
Loading External DLLs with ctypes
Basic Data Types in ctypes
Using ctypes
Summary
Suggested Reading
Chapter 6. Hardware: Tools and Supplies
The Essentials
Hand Tools
Be kind to your tools
Where to purchase tools
Digital Multimeter
DMM resolution
DMM usage tips
Soldering Tools
Nice-to-Have Tools
Advanced Tools
The Oscilloscope
Logic Analyzers
Test Equipment Caveats
Supplies
New Versus Used
Summary
Suggested Reading
Chapter 7. Physical Interfaces
Connectors
DB-Type Connectors
USB Connectors
Circular Connectors
Terminal Blocks
Wiring
Soldering
Crimping
Wiring caveats
Connector Failures
Serial Interfaces
RS-232/EIA-232
RS-232 data formats
RS-232 signals
DTE and DCE
RS-485/EIA-485
RS-485 signals
Line drivers and receivers
RS-485 multi-drop
RS-232 versus RS-485
USB
USB classes
USB data rates
USB instrumentation
Windows Virtual Serial Ports
GPIB/IEEE-488
GPIB/IEEE-488 Signals
GPIB Connections
GPIB via USB
PC Bus Interface Hardware
Pros and Cons of Bus-Based Interfaces
Data Acquisition Cards
GPIB Interface Cards
Old Doesn’t Mean Bad
Summary
Suggested Reading
Chapter 8. Getting Started
Defining the Project
Requirements-Driven Design
Stating the Need
Project Objectives
Requirements
Why Requirements Matter
Well-Formed Requirements
The Big Picture
Requirement Types
Use Cases
Traceability
Capturing Requirements
Designing the Software
The Software Design Description
Graphics in the SDD
Block diagrams
Flowcharts
State diagrams
Message sequence charts
Pseudocode
Divide and Conquer
Handling Errors and Faults
Identifying potential failures
Failure responses
Functional Testing
Testing to the Requirements
Test Cases
Testing Error Handling
Regression Testing
Tracking Progress
Implementation
Coding Styles
Why coding style is important
Adopting existing coding style guidelines
Organizing Your Code
Code Reviews
Unit Testing
Defining a unit test
Implementing unit tests
assert versus assertEqual() and friends
Code coverage
Connecting to the Hardware
Documenting Your Software
Version Control
Defect Tracking
User Documentation
Summary
Suggested Reading
Chapter 9. Control System Concepts
Basic Control Systems Theory
Linear Control Systems
Nonlinear Control Systems
Sequential Control Systems
Terminology and Symbols
Control System Block Diagrams
Input-output relationships
Feedback
Transfer Functions
Time and Frequency
Time and frequency domains
Time and control systems behavior
Discrete-time control systems
Control System Types
Open-Loop Control
Closed-Loop Control
Controlling position—Basic feedback
Controlling velocity—Feed-forward and PWM controllers
Nonlinear Control: Bang-Bang Controllers
Sequential Control Systems
Proportional, PI, and PID Controls
Off-the-shelf controllers versus software implementation
PID overview
The proportional control term
PI and PID controls
Hybrid Control Systems
Implementing Control Systems in Python
Linear Proportional Controller
Bang-Bang Controller
Simple PID Controller
Summary
Suggested Reading
Chapter 10. Building and Using Simulators
What Is Simulation?
Low Fidelity or High Fidelity
Simulating Errors and Faults
Interface faults
System faults
Using Python to Create a Simulator
Package and Module Organization
Data I/O Simulator
DevSim internals
DevSim methods
Some simple examples
User-defined functions
Cyclic functions
Noise
AC Power Controller Simulator
The SPC model
The SPC serial interface and virtual serial ports
Communicating with SPC
The SPC command set
Command descriptions
SPC simulator internals
Configuring the SPC
Interacting with the SPC simulator
Serial Terminal Emulators
Using Terminal Emulator Scripts
Displaying Simulation Data
gnuplot
Installing gnuplot on Windows
Using gnuplot
Method 1: Using Python’s popen() method
Method 2: gnuplot.py
Testing gnuplot.py
Using gnuplot.py
Plotting Simulator Data with gnuplot
Creating Your Own Simulators
Justifying a Simulator
The Simulation Scope
Time and Effort
Summary
Suggested Reading
Chapter 11. Instrumentation Data I/O
Data I/O Interface Software
Interface Formats and Protocols
IVI—Interchangeable Virtual Instrument
IVI-compliant drivers
VISA—Virtual Instrument Software Architecture
SCPI—Standard Commands for Programmable Instruments
Unique protocols
Python Interface Support Packages
pySerial
pyParallel
PyVISA
Alternatives for Windows
Using Bus-Based Hardware I/O Devices with Linux
The Comedi project
Comedi hardware support
Using comedi with Python
Data I/O: Acquiring and Writing Data
Basic Data I/O
Reading data
Writing data
Blocking Versus Nonblocking Calls
Data I/O Methods
On-demand data I/O
Polled data I/O
Acquiring data using a thread
Handling Data I/O Errors
Classes of errors
Error retry and system termination
Error/warning message single-shot logic
Handling Inconsistent Data
Waiting for stability
Dealing with noise: Averaging
Summary
Suggested Reading
Chapter 12. Reading and Writing Data Files
ASCII Data Files
The Original ASCII Character Set
Python’s ASCII Character-Handling Methods
Reading and Writing ASCII Flat Files
Records
Writing ASCII data files
Reading ASCII data files
CSV files
Configuration Data
Basic configuration file organization
Using configuration files
Module AutoConvert.py—Automatic String Conversion
Module FileUtils.py—ASCII Data File I/O Utilities
Binary Data Files
Flat Binary Data Files
Handling Binary Data in Python
Using the ctypes module to handle structured binary data
Using struct to handle structured binary data
Image Data
PGM
PGM file structure
Summary
Suggested Reading
Chapter 13. User Interfaces
Text-Based Interfaces
The Console
Example console display
Reading user input
OS-specific console I/O
Linux and Windows text display differences
Using Python’s msvcrt library module
ANSI Display Control Techniques
ANSI and Windows
Basic ANSI control sequences
The SimpleANSI library
Using SimpleANSI
Python and curses
curses background
Python’s curses library module
A simple data display using curses
Adding a subwindow
To Curse or Not to Curse, Is That the Question?
Graphical User Interfaces
Some GUI Background and Concepts
Using a GUI with Python
GUI objects
Basic GUI display structure
GUI functionality
The GUI main loop
TkInter
Planning your GUI
Geometry management
Simple TkInter example
Tools and resources for TkInter
wxPython
Designing a wxPython GUI
Building a simple wxPython GUI
Tools and resources for wxPython
Summary
Suggested Reading
Chapter 14. Real World Examples
Serial Interfaces
Simple DMM Data Capture
Serial Interface Discrete and Analog Data I/O Devices
Serial Interfaces and Speed Considerations
USB Example: The LabJack U3
LabJack Connections
Installing a LabJack Device
LabJack on Windows
LabJack on Linux
LabJack and Python
Installing and testing LabJackPython
LabJack driver structure
U3 configuration
Using the Python interface with the getFeedback() method
Summary
Suggested Reading
Appendix A. Free and Open Source Software Resources
Appendix B. Instrument Sources
Manufacturers
Used Test Equipment Sources
Manuals
Index