跳转到主要内容

smbus2 是纯Python实现的 smbus-cffi/smbus-python 的直接替换品

项目描述

smbus2

纯Python实现的 smbus-cffi/smbus-python 的直接替换品

Build Status Documentation Status CodeQL Quality Gate Status

Python Verions PyPi Version PyPI - Downloads

简介

smbus2 是(另一个)纯Python实现的 python-smbus 包。

它从零开始设计,有两个目标

  1. 它应该是smbus的直接替换品。语法应该是相同的。
  2. 比其他纯Python实现(如 pysmbus)更多地使用内在的i2c结构和联合。通过这样做,它将更加功能完整且易于扩展。

当前支持的功能包括

  • 获取i2c功能(I2C_FUNCS)
  • SMBus数据包错误检查(PEC)支持
  • 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 - 带有重复起始的联合写入/读取事务

它是在Python 2.7上开发的,但在Python 3.X上无需任何修改即可运行。

有关更新和一般变更的更多信息记录在变更日志中。

SMBus代码示例

由于smbus2作为包安装在与smbus相同的位置,因此它并不是一个100%的替代品。您必须更改模块名称。

示例1a:读取一个字节

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

示例1b:使用'with'读取一个字节

这是一个完全相同的示例,但由于smbus会在退出with块时自动关闭,所以更安全。

from smbus2 import SMBus

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

示例1c:启用PEC读取一个字节

启用包错误检测的相同示例。

from smbus2 import SMBus

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

示例2:读取数据块

您可以一次性读取最多32字节。

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)

示例3:写入一个字节

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)

示例4:写入数据块

虽然可以一次写入32字节,但我发现这样容易出错。如果遇到问题,请减少写入量并在之间添加延迟。

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

从v0.2版本开始,smbus2库也支持组合读取和写入事务。 i2c_rdwr 并不是SMBus功能,但在主设备需要

  1. 读取或写入超过SMBus 32字节限制的大批量数据时很有用。
  2. 写入一些数据,然后使用重复起始位而不是停止位从从设备读取。

每个操作都由一个 i2c_msg 消息对象表示。

示例5:单个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)

示例6:双重i2c_rdwr

要执行双重操作,只需将更多的i2c_msg实例添加到总线调用中即可

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)

示例7:访问i2c_msg数据

所有数据都包含在i2c_msg实例中。以下是一些数据访问选项。

# 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])

安装说明

使用pipPyPi

pip install smbus2

使用condaconda-forge

conda install -c conda-forge smbus2

从源代码安装非常直接

python setup.py install

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源代码分发

smbus2-0.4.3.tar.gz (16.8 kB 查看哈希值)

上传时间

构建分发

smbus2-0.4.3-py2.py3-none-any.whl (11.6 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者