tino / pyFirmata

Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet.
MIT License
578 stars 191 forks source link

check if port is opened #49

Open bartonekJ opened 8 years ago

bartonekJ commented 8 years ago

Hi, i'd like to control arduino from animation package called Autodesk Maya. I'm also quite new to python, so forgive me stupid questions, please.

Lets say I'm running this script in Maya:

from pyfirmata import ArduinoMega, util
port = "COM3"
board = ArduinoMega(port)

and then some "for loop"

But if i'll run that script again, maya retuns: SerialException: could not open port 'COM3': WindowsError(5, 'Access is denied.')

So id like to check if COM3 is already connected.I've tried following code, but it's obviously nonsense:

port = "COM3"
if ArduinoMega(None):     # is there some bolean which tells me if i've already assigned board to COM3 or not ? 
     board = ArduinoMega(port)
     print board
else:
     print "already connected"

Is there some way how to do it, please ?

tino commented 8 years ago

You should probably properly close the serial connection at the end of the script. If you exit the script there is not way to maintain a state that would tell you there is already a connection.

You'll want to call board.exit() at the end of the script, or when exiting.