Pascal
Posts: 70
Joined: Sat Oct 20, 2012 7:23 am

Make controle script.

Sun Feb 10, 2013 10:57 pm

I have several units build from scratch to work in my home domotica.
All my units work with the I²C pins (SDA/SCL)
You can check if a device is present on the bus by using the i2cdetect program from the i2ctools package- i2cdetect 0 -y(raspberry pi R1) or i2cdetect 1 -y (raspberry pi R2).

Now my question is if it is possible to make a python code that first checks if there are units connected on the I²C pins and if so show it on screen.

Thx.

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: Make controle script.

Mon Feb 11, 2013 5:34 am

you could try something like this, just a bunch of try/except statements:

Code: Select all

locations=['locations that you'd scan for addresses', 'end']
bus = smbus.SMBus(1)
for device in locations:
	try:
		(commands to try location on the i2c bus.)
		break
	except:
		try:
                        another try statement stuff to try to search the i2c bus
                except:
                        try:
                              and on and on....
                if device == 'end':
			print "Nothing found, Please check cable connections."
			exit()
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

Pascal
Posts: 70
Joined: Sat Oct 20, 2012 7:23 am

Re: Make controle script.

Mon Feb 11, 2013 11:03 am

I don't realy understand your code.
Maybe you have a working example?

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: Make controle script.

Mon Feb 11, 2013 11:20 am

no working example, I was just giving a concept. you have a list of all addresses used by the i2c, and what the code does is search through them using a for loop to loop through all of the combinations, and if it finds one, you can assign a varable to 1 or something like that to tell that the location is being used, then after all locations have been checked, it'd print out whatever locations it shows something at.
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

Pascal
Posts: 70
Joined: Sat Oct 20, 2012 7:23 am

Re: Make controle script.

Mon Feb 11, 2013 2:02 pm

In your code

Code: Select all

locations=['locations that you'd scan for addresses', 'end']
The word 'end' do you mean the last address or just the word 'end' ?

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: Make controle script.

Mon Feb 11, 2013 4:04 pm

just the word end, I use it as kinda a stopper for my projects. here's an example of what I mean, this is for auto searching and connecting to a serial port so it works on any system.

Code: Select all

import serial
locations=['/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1', 'end']

for device in locations:
	try:
		#print "Trying...",device
		serialport = serial.Serial(device, 2400, timeout = 0)
		break
	except:
		#print "Failed to connect on",device
		if device == 'end':
			print "Unable to find Serial Port, Please plug in cable or check cable connections."
			exit()
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Make controle script.

Mon Feb 11, 2013 5:21 pm

Hmm, as long as it works... But might be better to code without the 'end' string - something along this:

Code: Select all

import serial
locations=['/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1']

deviceFound = False
for device in locations:
   try:
      #print "Trying...",device
      serialport = serial.Serial(device, 2400, timeout = 0)
      deviceFound = True
      break
   except:
      #print "Failed to connect on",device

if not deviceFound:
    print "Unable to find Serial Port, Please plug in cable or check cable connections."
    exit()
...just to avoid doing the call serial.Serial('end', ...)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”