简体   繁体   中英

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. However, my issue is if try to initiate I2C communication the second time I get - "OSError: [Errno 9] Bad file descriptor" error. 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. How can I restart the I2C once it is closed? I have also attached the error that I get when I try initiate the I2C send time. Please note that I need to connect multiple arduinos as slaves to raspberry, although the current code is for a single slave. And I need to call a specific arduino based on user selection. Do we need to close the bus before we can start communicating with another 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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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