Python I2C 通信 TTP229

标签 python linux arduino raspberry-pi i2c

我需要从 TTP229(16 键或 8 键触摸板检测器)读取 2 个不同的字节。
我在 Python 中使用 I2C。 TTP229 datasheet PDF .
我无法读取第二个字节,但我可以读取第一个字节。

Python代码:

import smbus
bus = smbus.SMBus(1)
adressTTP229 =  0x57 #0xAF>>1
byte1 = bus.read_byte(adressTTP229)
byte2 = bus.read_byte(adressTTP229)

byte1 始终等于 byte2

这个 Arduino 代码,工作正常:

#include <Wire.h>
#define ttp229 (0xAF>>1) 

void setup() {
  Serial.begin(9600);  // start serial for output
  Wire.begin();
}

void loop() {
  delay(50);
  bool isNewData = false;
  Wire.requestFrom(ttp229,2,true);
  while (Wire.available()) { 
    uint16_t b1 = Wire.read(); // receive a first byte
    uint16_t b2 = Wire.read(); // receive a second byte
    if (b1==b2 && b2==0) {break;}
    //... 

    }
  }

如何在 Python 中使用 Arduino 的 requestFrom() 函数?

最佳答案

尝试:

import smbus, time
bus = smbus.SMBus(1)
while True:
   print bus.read_word(0xAF)
   time.sleep(0.1)

不要更改地址,总线会进行转换,如果您读取字节,您将始终获得相同的第一个字节。你想一次读一个字 = 2 个字节

未测试,但可能有效,已订购并将测试

关于Python I2C 通信 TTP229,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41710567/

相关文章:

Python 路径作为字符串

python - 我在python 3.5 Windows 10中安装 “icu”时遇到问题

python - eval() 总是错误的吗?

linux - Buildroot - 系统不启动 -/dev/ttyS0 没有这样的文件

linux - 如何创建 PDF 的 JPG 预览(使用 Linux 命令行)?

python - Django GET 请求参数来自 POST 请求

linux - NeoVim 缺少 Python 支持

arduino - RabbitMQ 与 Arduino Uno

python - 向 Arduino 发送串行通信(在 Ubuntu 上使用 Python)

python - 连续数据流 Arduino 到 Python