serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Access is denied.', None, 5)

问题描述 投票:0回答:0

我在互联网上经常看到这个错误,但我找不到接近我的问题的东西。 我目前正在为我的电子项目开发一个 Windows 应用程序。我以前的版本(代码)运行良好,我可以连接并使用它。但是当它到达新版本时,我得到了这个错误。

serial.serialutil.SerialException: could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)

新的代码和以前的代码存储在不同的地方,但是处理蓝牙发送/接收的代码(文件)具有相同的代码。

代码:

import time
import serial

class CMD:
    global SERIAL, command
    SERIAL = serial.Serial("COM4", 9600, timeout = 0.2)
    if not SERIAL.isOpen():
        SERIAL.open()
        print('com4 is open', SERIAL.isOpen())
    command = SERIAL.write

    @staticmethod
    def move_motor_left() -> None:
        command(b'M.DC.L.')
        time.sleep(0.1)

    @staticmethod
    def move_motor_right() -> None:
        command(b'M.DC.R.')
        time.sleep(0.1)
    
    @staticmethod
    def stop_motor() -> None:
        command(b'S.M.')
        time.sleep(0.1)

    @staticmethod
    def turn_laser() -> None:
        command(b'T.L.')
        time.sleep(0.1) 

两个代码的一个区别是之前的代码是在python 3.9.x上运行的,新的代码是在python 3.11.x上运行的

我尝试断开与 HC-06 模块的连接,重新启动 VScode 并重新启动 Raspberry Pi pico 和 Thotty 编辑器,但没有任何效果。

python python-3.x pyserial
© www.soinside.com 2019 - 2024. All rights reserved.