Arduino与Python串口通信时出现COM4端口权限拒绝错误求助
权限错误:无法打开Arduino串口COM4(PermissionError 13)
报错信息
File "C:\Users\IRONSERVITOR\PycharmProjects\More pygame 23\11\22\venv\lib\site-packages\serial\serialwin32.py", line 64, in open raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)
Arduino代码
#include <ezButton.h> #define VRX_PIN A0 // Arduino pin connected to VRX pin #define VRY_PIN A1 // Arduino pin connected to VRY pin #define SW_PIN 2 // Arduino pin connected to SW pin ezButton button(SW_PIN); int xVal = 0; // To store value of the X axis int yVal = 0; // To store value of the Y axis int bVal = 0; // To store value of the button void setup() { Serial.begin(9600) ; button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { // reading the analog X and Y analog values xVal = analogRead(VRX_PIN); yVal = analogRead(VRY_PIN); // Read the button value bVal = button.getState(); // print data to Serial if (xVal == 1023){ Serial.println(" "); Serial.print('1'); delay(100); } if (xVal == 0){ Serial.println(" "); Serial.print("2"); delay(100); } if (yVal == 1023){ Serial.println(" "); Serial.print("down "); } if (yVal == 0){ Serial.println(" "); Serial.print("up "); } if (bVal == 0){ Serial.println(" "); Serial.print("hide "); } }
Python代码
import serial ard = serial.Serial(port='COM4', baudrate=9600, timeout=0.1) while True: command = ard.read() if command: ard.flushInput() if str(command) == '1': print('right') elif str(command) == '2': print('left') ard.close()
问题说明
单独运行Arduino代码或Python代码均正常,但同时运行时就会触发上述权限错误,需要解决该问题。
内容的提问来源于stack exchange,提问作者IronServitor
相关产品推荐
相关产品推荐

