虚拟串口COM5权限错误:环回通信读写脚本无法同时运行求助
虚拟串口环回通信权限错误问题
我使用两个基础测试脚本(读脚本、写脚本)通过虚拟串口实现环回通信:读脚本处于while循环中持续接收字节,写脚本运行时发送单个字节。通过VSPD(虚拟串口驱动)创建的COM5串口搭建环回环境,但遇到权限问题:
- 读脚本可以正常连接并打开COM5端口
- 写脚本尝试打开COM5写入时,抛出以下错误:
serial.serialutil.SerialException: could not open port 'COM5': PermissionError(13, 'Access is denied.', None, 5)
已尝试的排查操作:
- 在设备管理器中禁用并重新启用该串口
- 重启VSPD驱动程序
- 重启电脑
通过VSPD监控可以看到串口正常开闭;当读脚本未运行时,写脚本能够正常发送字节。
写脚本代码
# Serial write Script import serial # Configure the serial port ser = serial.Serial( port='COM5', baudrate=9600, bytesize=serial.EIGHTBITS, # 8 bits per byte parity=serial.PARITY_NONE, # No parity stopbits=serial.STOPBITS_ONE, # 1 stop bit timeout=0.5 # Timeout in seconds ) # Send a byte byte_to_send = b'\x41' # Example byte to send (here, ASCII 'A') ser.write(byte_to_send) print(f"Sent byte: {byte_to_send}") # Close the serial port ser.close()
读脚本代码
#Serial read script import serial import time # Configure the serial port ser = serial.Serial( port='COM5', baudrate=9600, bytesize=serial.EIGHTBITS, # 8 bits per byte parity=serial.PARITY_NONE, # No parity stopbits=serial.STOPBITS_ONE, # 1 stop bit timeout=0.5 # Timeout in seconds ) try: while True: # Read a byte byte_received = ser.read(1) # Read one byte print(f"Received byte: {byte_received}") # Optional: Add a small delay to reduce CPU usage time.sleep(0.1) # Adjust as needed except KeyboardInterrupt: print("Interrupted") finally: # Ensure the serial port is properly closed if ser.is_open: ser.close() print("Serial port closed")
VSPD配置界面

内容的提问来源于stack exchange,提问作者Fisch
相关产品推荐
相关产品推荐

