Python 3.x 为什么我的COM端口忙?
我有一个python程序,我正在使用它与微控制器对话。它会打开com端口,如下所示:Python 3.x 为什么我的COM端口忙?,python-3.x,serial-port,usb,Python 3.x,Serial Port,Usb,我有一个python程序,我正在使用它与微控制器对话。它会打开com端口,如下所示: def STM32_connect(): ports = list(serial.tools.list_ports.comports()) for p in ports: if "STM32" in p.description: connection = serial.Serial(p.device, timeout = .01)
def STM32_connect():
ports = list(serial.tools.list_ports.comports())
for p in ports:
if "STM32" in p.description:
connection = serial.Serial(p.device, timeout = .01)
return(connection)
print("ERROR: No STM32 Device Found")
sys.exit()
serial_connection = STM32_connect()
然后做很多事情,发送和接收数据,直到我用键盘输入关闭程序:
except:
print("\n Program Interrupted...")
finally:
print("\n Closing Serial Port\n")
serial_connection.close()
这一切都很好。我的问题是,当python脚本因断开USB电缆或关闭主板电源而被不正确地终止时,我无法再通过任何COM端口连接到micro。我收到这个错误:
Traceback (most recent call last):
File "/home/---/.local/lib/python3.5/site-packages/serial/serialposix.py", line 265, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
OSError: [Errno 16] Device or resource busy: '/dev/ttyACM2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "flextester.py", line 1, in <module>
from flex_usb_class import *
File "/home/---/Code/RobotFlexTester/flex_usb_class.py", line 30, in <module>
serial_connection = STM32_connect()
File "/home/---/Code/RobotFlexTester/flex_usb_class.py", line 25, in STM32_connect
connection = serial.Serial(p.device, timeout = .01)
File "/home/---/.local/lib/python3.5/site-packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/home/janey/.local/lib/python3.5/site-packages/serial/serialposix.py", line 268, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 16] could not open port /dev/ttyACM2: [Errno 16] Device or resource busy: '/dev/ttyACM2'
回溯(最近一次呼叫最后一次):
打开文件“/home/--/.local/lib/python3.5/site packages/serial/serialposix.py”,第265行
self.fd=os.open(self.portstr,os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
OSError:[Errno 16]设备或资源正忙:'/dev/ttyACM2'
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“flextester.py”,第1行,在
从flex_usb_类导入*
文件“/home/--/Code/RobotFlexTester/flex_usb_class.py”,第30行,在
串行连接=STM32\u连接()
文件“/home/--/Code/RobotFlexTester/flex_usb_class.py”,第25行,在STM32_connect中
连接=串行。串行(p.设备,超时=.01)
文件“/home/--/.local/lib/python3.5/site packages/serial/serialutil.py”,第240行,在__
self.open()
打开文件“/home/janey/.local/lib/python3.5/site packages/serial/serialposix.py”,第268行
raise SerialException(msg.errno,“无法打开端口{}:{}”。格式(self.\u port,msg))
serial.serialutil.SerialException:[Errno 16]无法打开端口/dev/ttyACM2:[Errno 16]设备或资源正忙:'/dev/ttyACM2'
我可以更改COM端口,但在不同的ttyACM端口收到完全相同的错误。但是如果我尝试运行不同的python脚本,我可以连接到同一端口上的同一设备。问题似乎以某种方式锁定在初始测试脚本上,但是
ps-a
没有显示它仍在运行。大约30秒-1分钟后,我什么也不做,问题就消失了。30秒到1分钟的延迟是由于RS232协议的内部原因造成的。请参阅setserial
命令()中的closing_wait
选项(默认30秒),当断开USB电缆并断开连接时,协议将等待closing_wait
选项中指定的时间,直到关闭端口/会话<代码>会话锁定禁止将第二个进程连接到打开的端口