简体   繁体   English

与树莓派和 arduino 的 I2C 通信问题

[英]Issue with I2C communication with raspberry pi and arduino

I am able to achieve I2C communication between raspberry pi and arduino and able to transfer the sensor data to the master once.我能够实现树莓派和 arduino 之间的 I2C 通信,并且能够将传感器数据传输到主机一次。 However, my issue is if try to initiate I2C communication the second time I get - "OSError: [Errno 9] Bad file descriptor" error.但是,我的问题是,如果我第二次尝试启动 I2C 通信 - “OSError: [Errno 9] Bad file descriptor”错误。 Here is my code below-下面是我的代码-

import time
import smbus
import struct
#Slave Address from Arduinos
SLAVE_ADDRESS = 0x04

#Creating I2C Bus
I2C_Bus = smbus.SMBus(1)

weight = 0

def get_data():
    return I2C_Bus.read_i2c_block_data(SLAVE_ADDRESS,0,16)

def get_int(data, index):
    bytes = bytearray(data[4*index:(index+1)*4])
    return struct.unpack("<i",bytes)[0]

def send_data(value):
    I2C_Bus.write_byte(SLAVE_ADDRESS,value)
    time.sleep(1)

def start():
    while True:
        for x in range(1,2):
            send_data(1)
            x = x +1
        data = get_data()
        weight = get_int(data,0)
        if (weight == 25000):
            I2C_Bus.close()
            time.sleep(5)
            x = 1
            get_input()
        else:
            print("The transaction weight is: "+str(weight))
        time.sleep(1)

def get_input():
    var = input("Enter a number from 1-9: ") 
    print(var)
    while (var != "1"):
        print ("Please select only 1 for now")
        get_input()
    if (var == "1"):
        start()

while True:
    get_input()

I guess the issue is once the I2C_Bus.close() is executed I am unable to open the I2C Bus again.我想问题是一旦执行 I2C_Bus.close() 我就无法再次打开 I2C 总线。 How can I restart the I2C once it is closed? I2C 关闭后如何重启? I have also attached the error that I get when I try initiate the I2C send time.我还附上了尝试启动 I2C 发送时间时遇到的错误。 Please note that I need to connect multiple arduinos as slaves to raspberry, although the current code is for a single slave.请注意,我需要将多个 arduino 作为从属连接到树莓派,尽管当前代码是针对单个从属的。 And I need to call a specific arduino based on user selection.我需要根据用户选择调用特定的 arduino。 Do we need to close the bus before we can start communicating with another arduino.我们是否需要关闭总线才能开始与另一个 arduino 通信。 Please guide me if I am wrong.如果我错了,请指导我。 Thanks a lot for you time and help.非常感谢您的时间和帮助。

Traceback (most recent call last): File "/home/pi/Desktop/weightData_PiMaster_2.py", line 65, in get_input() File "/home/pi/Desktop/weightData_PiMaster_2.py", line 57, in get_input start() File "/home/pi/Desktop/weightData_PiMaster_2.py", line 38, in start get_input() File "/home/pi/Desktop/weightData_PiMaster_2.py", line 57, in get_input start() File "/home/pi/Desktop/weightData_PiMaster_2.py", line 30, in start send_data(1) File "/home/pi/Desktop/weightData_PiMaster_2.py", line 24, in send_data I2C_Bus.write_byte(SLAVE_ADDRESS,value) OSError: [Errno 9] Bad file descriptor回溯(最后一次调用):文件“/home/pi/Desktop/weightData_PiMaster_2.py”,第 65 行,在 get_input() 文件“/home/pi/Desktop/weightData_PiMaster_2.py”,第 57 行,在 get_input start( ) 文件“/home/pi/Desktop/weightData_PiMaster_2.py”,第 38 行,开始 get_input() 文件“/home/pi/Desktop/weightData_PiMaster_2.py”,第 57 行,get_input start() 文件“/home/ pi/Desktop/weightData_PiMaster_2.py”,第 30 行,开始 send_data(1) 文件“/home/pi/Desktop/weightData_PiMaster_2.py”,第 24 行,在 send_data I2C_Bus.write_byte(SLAVE_ADDRESS,value) OSError: [Errno 9 ] 错误的文件描述符

Calling start() a second time will call send_data() which calls I2C_Bus.write_byte(SLAVE_ADDRESS,value) on I2C_Bus that has already been closed in the last call of start() and will only be opened once on script execution.再次调用start()将调用send_data() ,它调用I2C_Bus.write_byte(SLAVE_ADDRESS,value)上的I2C_Bus已在最后一次调用start()关闭,并且只会在脚本执行时打开一次。

You have to open the bus again befor you attempt to read or write.在尝试读取或写入之前,您必须再次打开总线。

Maybe use it as recommended in the documentation:也许按照文档中的建议使用它:

from smbus2 import SMBus

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM