NOTICE: vibe coded with vigor by Claude Sonnet 4.5
Python wrapper for the National Instruments NI-845x USB-to-SPI/I2C interface DLL.
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.
- 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
ni845x_wrapper.py- Main wrapper class with common functionsni845x_function_reference.md- Complete list of all 227 DLL functionsni845x_examples.py- Working examples for common use cases
- Python 3.6 or later
- Windows (DLL is Windows-only)
- National Instruments NI-845x hardware
- Ni845x.dll (included with NI-845x driver installation)
- Install the NI-845x drivers from National Instruments
- Copy
ni845x_wrapper.pyto your project - Ensure
Ni845x.dllis in your system PATH or specify the full path
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)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.
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
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}")# 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)# 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)- 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
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\
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.
When adding new functions:
- Define proper ctypes argument and return types
- Add error checking with
_check_error() - Use descriptive docstrings
- Follow existing naming conventions
- Add usage examples where appropriate