Skip to main content

A drop-in replacement for smbus-cffi/smbus-python in pure Python

Project description

Introduction

smbus2 is (yet another) pure Python implementation of of the [python-smbus](http://www.lm-sensors.org/browser/i2c-tools/trunk/py-smbus/) package.

It was designed from the ground up with two goals in mind:

  1. It should be a drop-in replacement of smbus. The syntax shall be the same.

  2. Use the inherent i2c structs and unions to a greater extend than other pure Python implementations like [pysmbus](https://github.com/bjornt/pysmbus) does. By doing so, it will be more feature complete and easier to extend.

Currently supported features are:

  • Get i2c capabilities (I2C_FUNCS)

  • read_byte_data

  • write_byte_data

  • read_word_data

  • write_word_data

  • read_i2c_block_data

  • write_i2c_block_data

It is developed on Python 2.7, but should work in Python 3.X too.

Code examples

smbus2 installs next to smbus as the package, so it’s not really a 100% replacement. You must change the module name.

Example 1a: Read a byte

from smbus2 import SMBus

# Open i2c bus 1 and read one byte from address 80, offset 0 bus = SMBus(1) b = bus.read_byte_data(80, 0) print(b) bus.close()

Example 1b: Read a byte using ‘with’

This is the very same example but safer to use since the smbus will be closed automatically when exiting the while block.

from smbus2 import SMBusWrapper

with SMBusWrapper(1) as bus:

b = bus.read_byte_data(80, 0) print(b)

Example 2: Read a block of data

from smbus2 import SMBusWrapper

with SMBusWrapper(1) as bus:

# Read a block of 16 bytes from address 80, offset 0 # Note: You can read up to 32 bytes at once block = bus.read_i2c_block_data(80, 0, 16) # Returned value is a list of 16 bytes print(block)

Example 3: Write a byte

from smbus2 import SMBusWrapper

with SMBusWrapper(1) as bus:

# Write a byte to address 80, offset 0 data = 45 bus.write_byte_data(80, 0, data)

Example 4: Write a block of data

It is possible to write 32 bytes at the time, but I have found that error-prone. Write less and add a delay in between if you run into trouble.

from smbus2 import SMBusWrapper

with SMBusWrapper(1) as bus:

# Write a block of 8 bytes to address 80 from offset 0 data = [1, 2, 3, 4, 5, 6, 7, 8] bus.write_i2c_block_data(80, 0, data)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smbus2-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file smbus2-0.1.0.tar.gz.

File metadata

  • Download URL: smbus2-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for smbus2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a1ef70581a6a68f74791ae283b5888b280f8130ceff1f0d5b93fbba3f8ac7e49
MD5 261cefa6723f14dc1874156316a871a1
BLAKE2b-256 26d0516ddf1898ec1fe4d3206061fef3f0c06abd1f8449e0401b82cd7c874b08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page