My goal is to be able to see the list of avalible i2c addresses as soon as my program runs. The current program has the ability to list the addresses by user input through the following code:
while True:
if input.upper().startswith("LIST_ADDR"):
devices = device.list_i2c_devices()
for i in range(len (devices)):
print devices[i]
I have been able to use the code with using only the bottom 3 lines, however I now have five i2c devices currently attached to the Pi. Using just the three lines of code is giving me on IndexError: string index out of range. To this I can keep calling the program about four or five times and then it will run without issues. I was just wondering if there was a better way to achieve what I am looking for the program to do without having the error.
I am still pretty new to coding so thank you in advance for your patience.
If you just want to test the I2C connection with raspberry Pi then you can use this code which will help you to detect all the I2C address of all the devuces in one go
import os
import subprocess
import time
p = subprocess.Popen(['i2cdetect', '-y','1'],stdout=subprocess.PIPE,)
#cmdout = str(p.communicate())
for i in range(0,9):
line = str(p.stdout.readline())
print(line)
Basically I am executing linux command in Python by using this process
Hope this will help you out