首页 > 解决方案 > OpwenWrt - 使用 Python 从 SPI 读取字节

问题描述

我想从 spi 总线读取字节。写入一个字节很简单,只需使用

file = open("/dev/spidev1.0", 'wb')
file.write('#')

我尝试使用 read(1) 命令,但这使得 spi 时钟比仅仅一个字节长。

例子:

file = open("/dev/spidev1.0", 'rb')
file.read(1)

有谁知道为什么当我尝试读取一个字节时它只是时钟 8 次?

(顺便说一下,我使用的是 Carambola2 和 spi over gpio)

标签: pythongpiospiopenwrt

解决方案


我自己解决了这个问题。您必须使用 os 库

例子:

import os

file = os.open('filename', os.O_RDWR)

#Write byte 0x1 to SPI Bus
os.write(file, chr(0x1))
#Read one byte from SPI Bus
print(str(ord(os.read(file, 1))))

推荐阅读