Skip to main content

smbus2 is a drop-in replacement for smbus-cffi/smbus-python in pure Python

Project description

smbus2

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

Build Status Documentation Status CodeQL Quality Gate Status

Python Verions PyPi Version PyPI - Downloads

Introduction

smbus2 is (yet another) pure Python implementation of the python-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 extent than other pure Python implementations like pysmbus does. By doing so, it will be more feature complete and easier to extend.

Currently supported features are:

  • Get i2c capabilities (I2C_FUNCS)
  • SMBus Packet Error Checking (PEC) support
  • read_byte
  • write_byte
  • read_byte_data
  • write_byte_data
  • read_word_data
  • write_word_data
  • read_i2c_block_data
  • write_i2c_block_data
  • write_quick
  • process_call
  • read_block_data
  • write_block_data
  • block_process_call
  • i2c_rdwr - combined write/read transactions with repeated start

It is developed on Python 2.7 but works without any modifications in Python 3.X too.

More information about updates and general changes are recorded in the change log.

SMBus 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 with block.

from smbus2 import SMBus

with SMBus(1) as bus:
    b = bus.read_byte_data(80, 0)
    print(b)

Example 1c: Read a byte with PEC enabled

Same example with Packet Error Checking enabled.

from smbus2 import SMBus

with SMBus(1) as bus:
    bus.pec = 1  # Enable PEC
    b = bus.read_byte_data(80, 0)
    print(b)

Example 2: Read a block of data

You can read up to 32 bytes at once.

from smbus2 import SMBus

with SMBus(1) as bus:
    # Read a block of 16 bytes from address 80, offset 0
    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 SMBus

with SMBus(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 SMBus

with SMBus(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)

I2C

Starting with v0.2, the smbus2 library also has support for combined read and write transactions. i2c_rdwr is not really a SMBus feature but comes in handy when the master needs to:

  1. read or write bulks of data larger than SMBus' 32 bytes limit.
  2. write some data and then read from the slave with a repeated start and no stop bit between.

Each operation is represented by a i2c_msg message object.

Example 5: Single i2c_rdwr

from smbus2 import SMBus, i2c_msg

with SMBus(1) as bus:
    # Read 64 bytes from address 80
    msg = i2c_msg.read(80, 64)
    bus.i2c_rdwr(msg)
    
    # Write a single byte to address 80
    msg = i2c_msg.write(80, [65])
    bus.i2c_rdwr(msg)
    
    # Write some bytes to address 80
    msg = i2c_msg.write(80, [65, 66, 67, 68])
    bus.i2c_rdwr(msg)

Example 6: Dual i2c_rdwr

To perform dual operations just add more i2c_msg instances to the bus call:

from smbus2 import SMBus, i2c_msg

# Single transaction writing two bytes then read two at address 80
write = i2c_msg.write(80, [40, 50])
read = i2c_msg.read(80, 2)
with SMBus(1) as bus:
    bus.i2c_rdwr(write, read)

Example 7: Access i2c_msg data

All data is contained in the i2c_msg instances. Here are some data access alternatives.

# 1: Convert message content to list
msg = i2c_msg.write(60, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
data = list(msg)  # data = [1, 2, 3, ...]
print(len(data))  # => 10

# 2: i2c_msg is iterable
for value in msg:
    print(value)

# 3: Through i2c_msg properties
for k in range(msg.len):
    print(msg.buf[k])

Installation instructions

From PyPi with pip:

pip install smbus2

From conda-forge using conda:

conda install -c conda-forge smbus2

Installation from source code is straight forward:

python setup.py install

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.6.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smbus2-0.6.0-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: smbus2-0.6.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for smbus2-0.6.0.tar.gz
Algorithm Hash digest
SHA256 9b5ff1e998e114730f9dfe0c4babbef06c92468cfb61eaa684e30f225661b95b
MD5 d832ad65cbf7f58685c0e5647ecc6494
BLAKE2b-256 4e36afafd43770caae69f04e21402552a8f94a072def46a002fab9357f4852ce

See more details on using hashes here.

File details

Details for the file smbus2-0.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: smbus2-0.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for smbus2-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 03d83d2a9a4afc5ddca0698ccabf101cb3de52bc5aefd7b76778ffb27ff654e0
MD5 ac8d92f27749747c7498837885b50f69
BLAKE2b-256 a5cf2e1d6805da6f9c9b3a4358076ff2e072d828ba7fed124edc1b729e210c55

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