Good Morning
I want to connect my Arduino board to the pc using pyFirmata lib but a strange problem has occured. Premise: pySerial and pyFirmata are succesfully installed on my computer. I have windows 8.0 64 bit. The drivers of the Arduino and the USB ports are working fine (since I can upload every sketch to Arduino).
The code I want run is very simple:
If a button (connected to the pin 4, configured as an input by the method.get_pin() from firmata library) is pushed a red led will blink, otherwise a green led will do. I make them blink with an easy function:
from time import sleep
def Blink(pin):
board.digital(pin).write(1)
sleep(1)
board.digital(pin).write(0)
sleep(1)
board is defined in the global scope as pyfirmata.Arduino('com3')
All contact works fine since i have test them with firmata_test.exe and I'm sure that none serial connections are open before I start python.
Here the strange thing:
If I write every command on the python shell the whole thing works fine, leds blink right!! BUT if I write all commands on a module and then I run it it gives out this error:
"Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
board = pyfirmata.Arduino('com3')
File "C:\Python27\lib\site-packages\pyfirmata\__init__.py", line 16, in __init__
super(Arduino, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\pyfirmata\pyfirmata.py", line 89, in __init__
self.sp = serial.Serial(port, baudrate, timeout=timeout)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
SerialException: could not open port 'com3': WindowsError(5, 'Access denied.')"
That is, it cannot open the port. I have tried some debugging but it didn't help me.
If I check the status of the port by
por = serial.Serial()
por.port = 'com3'
por.isOpen()
>>False
BUT if i use
por = serial.Serial('com3')
it pops up the same error.
Looking Arduino the TX led blink regularly so I can imagine that it is sending some data to the serial port. If I open arduino app it tells me that the port is busy. I can't fiure out if it is a problem of administrator permits, because I run py IDLE as admin. Even if I run the script from CMD in admin mode the error remains.
Thank you very much for every answer.
Guys I solved the problem! I rebooted the pc then (FIRST OF ALL) I started CMD in admin mode and ran the module.. Now it Works! Don't know why, it seems that until now I was running the first time without admin permissions. After that even though I open CMD in admin mode the error continued to appear.
Hope this can be helpful to someone.
Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With