有没有办法让 Raspberry Pi 通过 I2C 读取从 Arduino 发送的数据?
我正在尝试使用 Python smbus 模块将数据从 Arduino 读取到 Raspberry Pi。我试图让 Pi 读取的数据只是数字,例如 200、100 490 等,基本上类似于向 Pi 报告其状态的状态代码。然而,我在网上找不到 smbus 文档,只找到了一个难题,那就是 bus.read_byte
仅 从 Arduino 接收的数据中读取一个字节。
因此,当前代码每次运行时只能显示 0(或一些神秘的框):
写入蓝色是发送到 Arduino 的数据,带有 0 的框是接收到的数据
这是充分理解实际情况的主要代码:
import time
from smbus import SMBus
clientAddr = 0x08
bus = SMBus(1)
def i2cWrite(msg): # Main Issue
for c in msg:
bus.write_byte(clientAddr, ord(c))
return -1
def main():
print("Send message to Arduino")
while True:
print(bus.read_byte(clientAddr)) # Part that deals with printing messages from arduino on to python terminal
msg = input("> ")
print("...")
i2cWrite(msg)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Program Terminated Manually")
我试图从 Arduino 发送的信息是一个输出从电位计,它运行正常并输出其当前值,只是它没有出现在 Pi 端。
Arduino 代码主要部分:
// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
// receive byte as a character, char converts unicode to character and add these characters into string
received_str += (char)Wire.read();
}
}
// Function that sends data to Master (Pi)
void sendData(int msg)
{
Wire.write(msg);
}
void loop() {
delay(100);
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
sendData(sensorValue);
int n = received_str.toInt();
//Serial.print(n);
analogWrite(ENA, 255); //for H bridge PWM
if (n > 0) {
Serial.print(received_str);
Serial.println();
received_str = "";
} else {
received_str = "";
}
}
串行监视器输出: 电位器实际工作的串行监视器
所以据我了解,Arduino方面没有问题,但当尝试从 Arduino 向 Pi 接收数据时,它只是不想按需要工作。我是否应该使用 read_byte
以外的其他内容?如果有,是哪一个?
编辑真正导致出现这些框的是这部分代码:
def i2cRead():
data = ""
for i in range(0, 5):
data += chr(bus.read_byte(clientAddr));
print(data)
time.sleep(0.5);
这只是添加以查看是否可以修复错误,但不幸的是,它并没有
I am trying to read data from an Arduino to Raspberry Pi with the Python smbus module. This data that I am trying to get the Pi to read is just numbers, such as 200, 100 490, etc. basically similar to status codes to report its status to the Pi. However, I could not find the smbus documentation online and only found one piece of the puzzle, being that bus.read_byte
only reads a byte from the received data from the Arduino.
Because of this, the current code can only display a 0 (or some mystery boxes) whenever run:
The writing in blue is the data sent to the Arduino, and the boxes with the 0 are the received data
This the main code for full understanding of what's actually going on:
import time
from smbus import SMBus
clientAddr = 0x08
bus = SMBus(1)
def i2cWrite(msg): # Main Issue
for c in msg:
bus.write_byte(clientAddr, ord(c))
return -1
def main():
print("Send message to Arduino")
while True:
print(bus.read_byte(clientAddr)) # Part that deals with printing messages from arduino on to python terminal
msg = input("> ")
print("...")
i2cWrite(msg)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Program Terminated Manually")
The information that I am trying to send from my Arduino is a output from a potentiometer, it runs normally and outputs its current value, just that it does not appear on the Pi side.
Arduino Code Main Parts:
// Function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (Wire.available()) { // loop through all but the last
// receive byte as a character, char converts unicode to character and add these characters into string
received_str += (char)Wire.read();
}
}
// Function that sends data to Master (Pi)
void sendData(int msg)
{
Wire.write(msg);
}
void loop() {
delay(100);
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
sendData(sensorValue);
int n = received_str.toInt();
//Serial.print(n);
analogWrite(ENA, 255); //for H bridge PWM
if (n > 0) {
Serial.print(received_str);
Serial.println();
received_str = "";
} else {
received_str = "";
}
}
Serial Monitor Output:
Serial Monitor of Potentiometer actually working
So to my understanding, there is no issue with the Arduino side, but it just does not want to work as needed when trying to receive data from the Arduino to the Pi. Am I supposed to use something other than read_byte
? If so, which?
EDIT What actually caused the boxes to appear was this section of the code:
def i2cRead():
data = ""
for i in range(0, 5):
data += chr(bus.read_byte(clientAddr));
print(data)
time.sleep(0.5);
This was just added to see if it would fix the error, but unfortunately, it did not
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了这个问题,事实证明 Arduino 代码在设置部分需要一个
onRequest(event)
来基本上注册一个函数,以便在主机请求数据时调用。python 上的主代码也已完成,成功将数据从 arduino 打印到 Pi 中
Figured this issue, turns out the Arduino code needed an
onRequest(event)
in the setup section to basically register a function to be called whenever the master would request data.The master code on python was also worked on, successfully printing data from the arduino into the Pi