Access is denied to python from using COM3 (I want to send data to my arduino )

Thread Starter

filipmr

Joined Jan 2, 2021
64
I build a facial recognition software in python, and I want to send x and y cords of my face to my arduino uno through the Serial port(COM3 in my case).
Every time I start the program I get an error that access was denied. The error looks like this:
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)
So, can anyone help?
 

MrSalts

Joined Apr 2, 2020
2,767
How is the Arduino connected to the computer? Just a usb cable? Or are you using a usb to serial adapter? If so, which type - ftdi or other?

What software are you using to connect to Arduino - just purely Python? Post code.
 

Thread Starter

filipmr

Joined Jan 2, 2021
64
How is the Arduino connected to the computer? Just a usb cable? Or are you using a usb to serial adapter? If so, which type - ftdi or other?

What software are you using to connect to Arduino - just purely Python? Post code.
It's connected with a normal USB to B cable,that you get when u buy an arduino uno. My python code is :
Python:
import cv2
import serial
import time
cap=cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


while True:
    _,img = cap.read ()
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    for (x,y,w,h) in faces:

        cv2.rectangle( img, (x, y),(x+w,y+h),(0, 0, 255), 2 )
        serialPort = serial.Serial ( port="COM3", baudrate=9600,bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE )
        cv2.imshow ( 'img', img )
        r1=str(round ( x + w / 2 ))
        r2=str(round ( y + h / 2 ))

        data="X:"+r1+"Y:"+r2
        serialPort.write ( bytes(data, encoding='utf8' ))

    k = cv2.waitKey ( 30 ) & 0xff
    if k == 27:
        break
cap.release()
and this is my arduino code:
C:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
String data;
void setup() {
while(1){
  Serial.begin(9600);

  data=Serial.read();
  lcd.begin(16,2);
  lcd.print(data);
  delay(1000);

}

}

void loop() {
}
 

Thread Starter

filipmr

Joined Jan 2, 2021
64
On your PC go to the Device Manager and expand Ports (COM & LPT). It should show to which COMn port your USB is configured.
yeah, I know that, My arduino is connected to COM3 , the problem is that arduino keeps blocking python from using COM3(I need to send data from Python to arduino)
 

djsfantasi

Joined Apr 11, 2010
9,156
yeah, I know that, My arduino is connected to COM3 , the problem is that arduino keeps blocking python from using COM3(I need to send data from Python to arduino)
Where does this error appear? And where are you running your Python code? Is it on a Windows machine? It may not be the Arduino blocking COM3... it looks like it’s Windows. See the reference to WinError() in the error message you posted.

What version of Windows are you running?

I suggest that you execute your Python code as administrator and see if the error disappears.
 

Thread Starter

filipmr

Joined Jan 2, 2021
64
Where does this error appear? And where are you running your Python code? Is it on a Windows machine? It may not be the Arduino blocking COM3... it looks like it’s Windows. See the reference to WinError() in the error message you posted.

What version of Windows are you running?

I suggest that you execute your Python code as administrator and see if the error disappears.
how do i execute it as a administrator
 

djsfantasi

Joined Apr 11, 2010
9,156
how do i execute it as a administrator
From your other post, I am going to assume PyCharm is the Windows program used to execute Python. If not, apply this to whatever program you use.

Right-click on the icon used to run Python. There should be an option to run as Administrator. If this works, you can right-click again, select Properties, go to the Security tab and check off “Run as Administrator “.
 

Thread Starter

filipmr

Joined Jan 2, 2021
64
From your other post, I am going to assume PyCharm is the Windows program used to execute Python. If not, apply this to whatever program you use.

Right-click on the icon used to run Python. There should be an option to run as Administrator. If this works, you can right-click again, select Properties, go to the Security tab and check off “Run as Administrator “.
Yeah, it doesn't work
 

sagor

Joined Mar 10, 2019
903
Put a 10uF capacitor between the RST and GND pins on the UNO. When a device opens the UNO serial port, it often does a DTR reset to trigger the UNO bootloader code. That can lock up the UNO.
The 10uF capacitor across the RST to GND prevents that DTR reset....
 

Ian Rogers

Joined Dec 12, 2012
1,136
Just for information... I use Lazarus on win10 I have downloaded synaser from synapse... This works very well with USB..
I just leave it in the working directory and include it as a module... Some windows serial drivers are badly written.

There are five pascal modules... synaser, synachar, synacode, synafpc and synautil Job done..

Ignore me.... I read pascal.... Oh the joys of getting old...
 
Last edited:

Thread Starter

filipmr

Joined Jan 2, 2021
64
Put a 10uF capacitor between the RST and GND pins on the UNO. When a device opens the UNO serial port, it often does a DTR reset to trigger the UNO bootloader code. That can lock up the UNO.
The 10uF capacitor across the RST to GND prevents that DTR reset....
I tried that, but it didnt work
 

sagor

Joined Mar 10, 2019
903
Check the USB COM port properties in Device manager, port settings, Advanced. Some software has issues when Serial Enumerator is on. Also, maybe try to lengthen the Timeouts in the bottom left of that screen.
 

John P

Joined Oct 14, 2008
2,025
Is the Arduino programming/interface code running on your computer, connected to the Arduino? I don't know what would happen if you tried to run that at the same time as your Python stuff, Pycharm or whatever. They'd probably fight over which one got to control the USB port. I would certainly make sure that the only software trying to use the port is the Python program.
 

Thread Starter

filipmr

Joined Jan 2, 2021
64
Is the Arduino programming/interface code running on your computer, connected to the Arduino? I don't know what would happen if you tried to run that at the same time as your Python stuff, Pycharm or whatever. They'd probably fight over which one got to control the USB port. I would certainly make sure that the only software trying to use the port is the Python program.
Yrah, that is the problem, but i dont know how to fix it.
 

John P

Joined Oct 14, 2008
2,025
Filipmr, I think you may have the idea that if you've got an Arduino running, it has to be connected to the Arduino software in your computer. That isn't true--you only need the Arduino interface to compile and load code. Once that's done, you can stop the Arduino interface on the PC, and allow the serial port to be used by other software, like your Python program. The Arduino doesn't know or care what it's talking to. But of course, you have to write code for it so that communication happens the way you want it to!
 

Thread Starter

filipmr

Joined Jan 2, 2021
64
Filipmr, I think you may have the idea that if you've got an Arduino running, it has to be connected to the Arduino software in your computer. That isn't true--you only need the Arduino interface to compile and load code. Once that's done, you can stop the Arduino interface on the PC, and allow the serial port to be used by other software, like your Python program. The Arduino doesn't know or care what it's talking to. But of course, you have to write code for it so that communication happens the way you want it to!
Yeah, I closed the arduino IDE, but it still didnt work, thank you anyway
 

MrSalts

Joined Apr 2, 2020
2,767
Close the Arduino IDE and unplug the USB cable from the computer - then connect that to a phone charger. At this point, the Arduino only needs power.
 
Top