Python Forum

Full Version: Serial Python and Arduino
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am trying to write a base program that will connect to an Arduino, Via Serial, and will send and receive data. 

For some reason though, I can't seem to get Python to connect with the Arduino Uno. 

Here is the code I have, and the error that I get. 

If anyone knows a possible solution, any help would be great!


import serial

connected = False

ser = serial.Serial("COM3", 19200)

while not connected:
    serin = ser.read()
    connected = True

myStr = "1"
asBytes = str.encode(myStr)

ser.write(asBytes)

while ser.read() =='1':
    ser.read()

ser.close
Error:
runfile('C:/Users/Christopher Martone/Desktop/pythonArduino.py', wdir='C:/Users/Christopher Martone/Desktop') Traceback (most recent call last):   File "<ipython-input-3-f2cfc99aed65>", line 1, in <module>     runfile('C:/Users/Christopher Martone/Desktop/pythonArduino.py', wdir='C:/Users/Christopher Martone/Desktop')   File "D:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile     execfile(filename, namespace)   File "D:\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile     exec(compile(f.read(), filename, 'exec'), namespace)   File "C:/Users/Christopher Martone/Desktop/pythonArduino.py", line 12, in <module>     ser = serial.Serial("COM3", 19200)   File "D:\Anaconda\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialwin32.py", line 31, in __init__     super(Serial, self).__init__(*args, **kwargs)   File "D:\Anaconda\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialutil.py", line 240, in __init__     self.open()   File "D:\Anaconda\lib\site-packages\pyserial-3.3-py3.6.egg\serial\serialwin32.py", line 62, in open     raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)
Hello!
Try to run the script with administrative privileges. See the bottom line of the error message.
Another possibility is you have Serial monitor opened in Arduino IDE on same port. Then your Python program can't use this port at same time, so you should close the Serial monitor first.
Getting a serial port to work for you can take some time
in the beginning. Once working you forget about it as the reliability
factor is high.

The handshaking must be correct.

I use putty (http://www.putty.org/) to get it right in the beginning. You can play with the
various protocol like baud rate, parity, word length, number of stop bits etc.

without having to rewrite code, Once you get it right, write your code.
(Mar-22-2017, 12:58 PM)Larz60+ Wrote: [ -> ]Getting a serial port to work for you can take some time
in the beginning. Once working you forget about it as the reliability
factor is high.

The handshaking must be correct.

I use putty (http://www.putty.org/) to get it right in the beginning. You can play with the
various protocol like baud rate, parity, word length, number of stop bits etc.

without having to rewrite code, Once you get it right, write your code.

This is helpful, but do you know of any good documentation on how to use the software?

(Mar-22-2017, 12:07 AM)wavic Wrote: [ -> ]Hello!
Try to run the script with administrative privileges. See the bottom line of the error message.

Suggestions on code that would run it with permissions?
Putty user manual: https://the.earth.li/~sgtatham/putty/0.63/htmldoc/
Another thing that I noticed you are trying to open com3.
Usually the first com port is number 1, you normally only use com3 if you actually have more
that two com ports (two is common, 3 is not unheard of).
Make sure that you actually have a com3.
One way to find out is to look at your device drivers.
And while doing do, if you have a com3, test the driver (there is an option in the drivers menu)
(Mar-22-2017, 07:50 PM)Larz60+ Wrote: [ -> ]Putty user manual: https://the.earth.li/~sgtatham/putty/0.63/htmldoc/
Another thing that I noticed you are trying to open com3.
Usually the first com port is number 1, you normally only use com3 if you actually have more
that two com ports (two is common, 3 is not unheard of).
Make sure that you actually have a com3.
One way to find out is to look at your device drivers.
And while doing do, if you have a com3,  test the driver (there is an option in the drivers menu)

Thank you for the manual, 

The Arduino board is listed under COM3 and I have another connected to COM7
With putty, you should within seconds be able to see if you can talk to the device on the other side.
Only seconds (1 hour = 3600 sceonds)