【问题标题】:Arduino/Python Serial Error 'can't open the device '\\.\COM5': Access is deniedArduino/Python 串行错误'无法打开设备'\\.\COM5':访问被拒绝
【发布时间】:2019-05-24 03:30:45
【问题描述】:

我正在尝试使用 OpenCV 制作带有 python 和 Arduino 的面部跟踪相机。我在序列号出现此错误时遇到问题:

'avrdude: ser_open(): 无法打开设备“\.\COM5”:访问被拒绝。'

我不确定如何防止这种情况发生。如果python程序已经打开它就不会运行,如果我打开Arduino然后python它会运行但不会工作。

import cv2
import serial

ser = serial.Serial('COM5',baudrate = 52000)

def detectface(camera):
    cap = cv2.VideoCapture(camera)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
    faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    print(cap.isOpened())

    while(cap.isOpened()):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        face = faceDetect.detectMultiScale(gray, 1.3, 5)
        for(x, y, w, h) in face:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
            center = [(x+x+w)/2, (y+y+h)/2]
            center[0] = int(center[0])
            center[1] = int(center[1])
            if(center[0]<235):
                ser.write(b'x')
            elif(center[0]>245):
                ser.write(b'w')
            if(center[1]>335):
                ser.write(b'h')
            elif(center[1]<305):
                ser.write(b'y')
            cv2.circle(frame,(center[0],center[1]),25,(0,0,2),3,8,0)
        cv2.imshow('face', frame)
        if(cv2.waitKey(1) & 0xFF == ord('q')):
            break
    cap.release()
    cv2.destroyAllWindows()


detectface(0)
#include <Servo.h>
char tiltChannel=0,panChannel=1;
char serialChar=0;
int center1;
int center2;

char pyInput;
Servo servoTilt, servoPan;
void setup() {
  Serial.begin(52000);
  servoTilt.attach(9);
  servoPan.attach(10);
  servoTilt.write(90);
  servoPan.write(90);  
}

void loop() {
  int currentRotationX = 90;
  int currentRotationY = 90;
  if(Serial.available()>0){
    pyInput = Serial.read();
    if(pyInput == 'x'){
      servoTilt.write(currentRotationX++);
      currentRotationX=currentRotationX++;
    }
    else if(pyInput == 'w'){
      servoTilt.write(currentRotationX--);
      currentRotationX=currentRotationX--;
    }
    if(pyInput=='y'){
      servoTilt.write(currentRotationY++);
      currentRotationY=currentRotationY++;
    }
    else if(pyInput == 'h'){
      servoTilt.write(currentRotationY--);
      currentRotationY=currentRotationY--;
    }
  }
}

【问题讨论】:

  • 您在哪一步收到该错误?请记住,在 Python 代码运行时,您无法对 Arduino 进行编程或打开其串行监视器。

标签: python arduino port


【解决方案1】:

首先尝试打开 arduino IDE 并查看是否在某个 COM 端口中检测到您的板。如果您在那里看到打开了一个 com 端口,请将其复制并粘贴到您的代码中。 如果您使用的是 anaconda,请尝试以这种方式安装串行:

conda install -c anaconda pyserial 

然后试试 i.g:

ser = serial.Serial('COM10', 9600)

最好的问候。

【讨论】:

  • 你的电脑无法识别com口,尝试安装arduino的驱动。
  • 如果我只使用 arduino 命令,arduino 就可以工作,它只是不适用于串行。我安装了 arduino 驱动程序,但它仍然无法通过串行与 python 对话
猜你喜欢
  • 2021-09-12
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多