i2cscan2.py 782 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/python3
  2. import smbus, sys
  3. bus = 1
  4. ''' you can call this app with arguments: either a single small digit (0,1,2) or
  5. the device name /dev/i2c-1 '''
  6. if len(sys.argv)>1:
  7. if sys.argv[1].isdigit():
  8. bus = int(sys.argv[1])
  9. if sys.argv[1].startswith("/dev/i2c"):
  10. bus = int(sys.argv[1][-1])
  11. s=smbus.SMBus(bus)
  12. print("Scan I2C bus /dev/i2c-{} for devices".format(bus))
  13. print(" 0 1 2 3 4 5 6 7 8 9 a b c d e f",end='')
  14. def check_i2c(n):
  15. try:
  16. s.read_byte(n)
  17. return True
  18. except:
  19. return False
  20. for i in range(3,120):
  21. if (i % 16 == 0) or (i == 3):
  22. print("\n"+"{:02x}:".format(i),end='',flush=True)
  23. if i == 3:
  24. print(" ",end='')
  25. print(" {:02x}".format(i) if check_i2c(i) else " --",end='',flush=True)
  26. print()