0

I have been trying to use the MCP23017 along with my beaglebone.. I have however not received my devices yet, but I have started to get my program ready... I am programming the GPIO pins now.. Here I have tried to read and write the pins using i2c commands as follows: for write--

a=('i2cset', '-y', '0', '0x20', '0x14', '0x01')
subprocess.call(a, shell=True)

similarly using i2cget for reading.. However when I try to run it , it give me a notification on my screen saying

Usage: i2cset [-f] [-y I2CBUS CHIP-ADDRESS [DATA-ADDRESS [MODE]]
I2CBUS is an integer or an I2C bus name
ADDRESS is an integer (0x03- 0x77)

Do I get this notification only because I don't have my device connected yet? Or is it a problem because of using the subprocess module?

Any help is appreciated,

Namita.

Goldengirl
  • 957
  • 4
  • 10
  • 30

1 Answers1

0

Assuming that you've tried the command in your shell and it worked. If you set shell=True in subprocess.call(), it's recommended to use a string instead of a sequence (a tuple in your case) as the first argument.

If you use shell=True and pass a sequence as the first argument, the arguments from the second onwards will be treated as the parameters for the shell itself.

Please refer to this answer for more details: https://stackoverflow.com/a/15109975/870658

You can rewrite as below

cmd = 'i2cset -y 0 0x20 0x14 0x01'
subprocess.call(cmd, shell=True)
Community
  • 1
  • 1
fbessho
  • 1,462
  • 1
  • 14
  • 19
  • thank you for that response .. I actually tried that too but I still get the same notification.. Is it because of the unconnected device? – Goldengirl Apr 08 '15 at 13:37
  • @NamitaRaju What happens when you run the `i2cset -y 0 0x20 0x14 0x01` in the shell? If it works, then the problem is in `subprocess` module otherwise it's not the problem in Python. – fbessho Apr 08 '15 at 13:44
  • And when I try it the way you suggested I get a error saying i2cset-y00x200x0A0x40 : not found.. Shouldn't it be written as cmd='i2cset','-y','0', '0x20','0x14','0x01'); subprocess.call(cmd, shell=True)? – Goldengirl Apr 08 '15 at 13:51
  • When I try it in the shell it says 'Error: write failed'( since there is no device to write or read into) – Goldengirl Apr 08 '15 at 13:53
  • it's `' '.join(cmd)`, not `''.join(cmd)` (a space is needed inside of quotations).. – fbessho Apr 08 '15 at 13:53
  • @NamitaRaju Actually I've deleted the one with `join()` as I thought it's just confusing. The `subprocess.call() ` should return the same error message as you get in your shell. I'd recommend to retry `subprocess.call('i2cset -y 0 0x20 0x14 0x01')` again (it's equivalent to the one in my answer) and confirm you'll see the same output as what you see in the shell. – fbessho Apr 08 '15 at 13:58
  • I rectified my mistake and I get the same error as my shell.. Thank you for all the help :) – Goldengirl Apr 08 '15 at 14:10