Pages: [1]   Go Down
Author Topic: Control Arduino from WIndows using Python  (Read 2654 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1st post attempt failed so this one is not as long winded

This code
Code:
from firmata import *
import time
a = Arduino("COM32", baudrate=57600) # Baudrate must match rate set in sketch
#time.sleep(3)
a.pin_mode(12, firmata.OUTPUT)

print "wait"
time.sleep(2)
print "Go"
b=1
while True:
    a.digital_write(12, firmata.HIGH)
    time.sleep(1)
    a.digital_write(12, firmata.LOW)
    time.sleep(1)
    print "count" , b
    b= b +1

#a.serial.close()

Stops after 5 counts - ctrl-c takes tens secs to hand back control and then it give this
Code:
Traceback (most recent call last):
  File "E:\Dropbox\Private\arduino\py\firmatatest.py", line 12, in <module>
    a.digital_write(12, firmata.HIGH)
  File "build\bdist.win32\egg\firmata\firmata.py", line 91, in digital_write
    self.serial.write(chr(self.digital_output_data[port_number] >> 7))
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 268, in write
    err = win32.GetOverlappedResult(self.hComPort, self._overlappedWrite, ctypes.byref(n), True)
KeyboardInterrupt

any ideas why and what to do about it or alternative working methods of switching pins off and on using Python from Windows?

regards
Simon
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
a = Arduino("COM32"
32? You need to shitcan whatever application is abusing your COM ports. I never get a number above 8 when connecting my Arduinos.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"Ditch" might be the nice term to use over this side of the pond smiley

i don't think that the high com port num is the problem as the Firmate_Test.exe prog works just fine (unless Python on Windows can't handle it???)

(I'm running an 8 Yr old XP machine here that has had more USB kit plugged into it over the years that I've had hot dinners)

I did reset a com port to a lower number a few years ago  so might try doing it again.

regards
Simon
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

made no diff - switched it down to com2 - still stopped after 5 iterations smiley-sad

Simon
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is getting wierder !
I'd left the python prog running since last post and just noticed my LED on in 12 going off and on - switched to my Python console and the count is now up to 43!

Simon
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


So consider solved, but not understood?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not at all smiley
Over an hour the count should have got to 1800 smiley

Simon
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally after many days of googling and trying I've manged to get a continous blink and read an analog input program running smiley
Code:
from pyfirmata import Arduino, util
import time
a = Arduino("COM2", baudrate=57600) # Baudrate must match rate set in sketch

print "wait"
time.sleep(2)
print "Go"
it = util.Iterator(a)
it.start()
a.analog[5].enable_reporting()
while True:
    a.digital[12].write(1)
    time.sleep(1)
    a.digital[12].write(0)
    time.sleep(1)
    print "analog 5" , a.analog[5].read()

I downloaded pyfirmata from here
https://bitbucket.org/tino/pyfirmata
and added to my python 2.7 install by cd ing to my download location
Code:
E:\downloads\tino-pyfirmata-4bed4280dd31\tino-pyfirmata-4bed4280dd31
and then typing
Code:
c:\Python27\python.exe setup.py install

regards
Simon
« Last Edit: November 18, 2012, 04:10:26 pm by simplesi » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for sharing!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: