Cover image for Real World Instrumentation with Python

Book description

This book shows you how to develop applications instrumentation software with Python that lets you monitor or control devices in the physical world. Whether you need to acquire data from a hardware device or automate its functions, you get step-by-step instructions, clear working examples, hands-on tips and hints for interfacing a PC to instruments and devices, along with advice to help you avoid common pitfalls.

Table of Contents

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