Skip to content

Repository files navigation

NOTICE: vibe coded with vigor by Claude Sonnet 4.5

NI-845x Python Wrapper

Python wrapper for the National Instruments NI-845x USB-to-SPI/I2C interface DLL.

Overview

This project provides a Python interface to the Ni845x.dll, enabling control of NI-845x USB devices for I2C, SPI, and digital I/O communication. The wrapper exposes all 227 exported functions from the DLL with Pythonic error handling and type safety.

Features

  • Device management (find, open, close, configure)
  • I2C master and slave communication
  • SPI communication with full-duplex support
  • Digital I/O control (GPIO)
  • Scripting support for batch operations
  • Comprehensive error handling
  • Type-safe ctypes interface

Files

  • ni845x_wrapper.py - Main wrapper class with common functions
  • ni845x_function_reference.md - Complete list of all 227 DLL functions
  • ni845x_examples.py - Working examples for common use cases

Requirements

  • Python 3.6 or later
  • Windows (DLL is Windows-only)
  • National Instruments NI-845x hardware
  • Ni845x.dll (included with NI-845x driver installation)

Installation

  1. Install the NI-845x drivers from National Instruments
  2. Copy ni845x_wrapper.py to your project
  3. Ensure Ni845x.dll is in your system PATH or specify the full path

Quick Start

from ni845x_wrapper import Ni845x, Ni845xError

# Initialize wrapper
ni = Ni845x()

# Find and open device
find_handle, device_name = ni.find_device()
ni.close_find_device_handle(find_handle)
device = ni.open(device_name)

# Set I/O voltage to 3.3V
ni.set_io_voltage_level(device, 33)

# I2C communication
i2c_config = ni.i2c_configuration_open()
ni.i2c_configuration_set_address(i2c_config, 0x50)
ni.i2c_configuration_set_clock_rate(i2c_config, 100)

# Write and read
ni.i2c_write(device, i2c_config, b'\x00\x00')
data = ni.i2c_read(device, i2c_config, 16)

# Cleanup
ni.i2c_configuration_close(i2c_config)
ni.close(device)

Function Categories

The DLL provides 227 functions organized into:

  • Device Management (12 functions)
  • I2C Configuration (17 functions)
  • I2C Communication (6 functions)
  • I2C Scripting (27 functions)
  • I2C Slave (16 functions)
  • SPI Configuration (14 functions)
  • SPI Communication (3 functions)
  • SPI Scripting (26 functions)
  • SPI Streaming (20 functions)
  • Digital I/O (7 functions)
  • Firmware (4 functions)
  • LabVIEW Integration (75 functions)

See ni845x_function_reference.md for the complete function list.

Examples

The ni845x_examples.py file includes examples for:

  • Device discovery and configuration
  • I2C EEPROM read/write operations
  • SPI flash memory communication
  • Digital I/O pin control
  • Error handling patterns

Error Handling

All functions check return status codes and raise Ni845xError exceptions on failure. Error messages are automatically converted to human-readable text using the DLL's status-to-string function.

try:
    data = ni.i2c_read(device, config, 16)
except Ni845xError as e:
    print(f"I2C Error: {e}")

Common I2C Usage

# Configure I2C
config = ni.i2c_configuration_open()
ni.i2c_configuration_set_address(config, 0x50)
ni.i2c_configuration_set_clock_rate(config, 100)  # 100 kHz

# Write-read transaction
write_data = bytes([0x00, 0x10])
read_data = ni.i2c_write_read(device, config, write_data, 8)

ni.i2c_configuration_close(config)

Common SPI Usage

# Configure SPI
config = ni.spi_configuration_open()
ni.spi_configuration_set_clock_rate(config, 1000)  # 1 MHz
ni.spi_configuration_set_clock_polarity(config, 0)  # CPOL
ni.spi_configuration_set_clock_phase(config, 0)     # CPHA
ni.spi_configuration_set_chip_select(config, 0)

# Full-duplex communication
tx_data = bytes([0x9F, 0x00, 0x00, 0x00])
rx_data = ni.spi_write_read(device, config, tx_data)

ni.spi_configuration_close(config)

Notes

  • This wrapper was created by analyzing the DLL exports
  • Some function prototypes may need adjustment based on official NI documentation
  • The wrapper implements the most commonly used functions
  • Additional functions can be added following the existing patterns
  • LabVIEW-specific functions (LV prefix) are listed but not fully implemented

Documentation

For official National Instruments documentation, visit:

  • NI-845x Hardware and Software Manual
  • NI-845x C API Reference

The DLL typically installs to: C:\Program Files\National Instruments\NI-845x\MS .NET\

License

This wrapper code is provided as-is for use with National Instruments hardware. The underlying Ni845x.dll and hardware are subject to National Instruments licensing terms.

Contributing

When adding new functions:

  1. Define proper ctypes argument and return types
  2. Add error checking with _check_error()
  3. Use descriptive docstrings
  4. Follow existing naming conventions
  5. Add usage examples where appropriate

About

A python wrapper for the NI845X driver.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages