Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serial Python and Arduino
#1
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)
Reply
#2
Hello!
Try to run the script with administrative privileges. See the bottom line of the error message.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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?
Reply
#6
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)
Reply
#7
(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
Reply
#8
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,908 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Python error on mentioned Arduino port name dghosal 5 849 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 819 Mar-21-2023, 10:06 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,058 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,704 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  python serial port barryjo 2 1,654 Dec-27-2021, 11:09 PM
Last Post: barryjo
  serial connection to Arduino Jack9 4 2,465 Oct-22-2021, 10:18 AM
Last Post: Jack9
  Serial loopback with Arduino doesn't work ThomasS 3 2,772 Sep-19-2020, 12:47 PM
Last Post: deanhystad
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,358 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  About Arduino and Python usb webcam tuts Simurg 1 2,155 Mar-20-2020, 07:25 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020