Python: pyserial 超时似乎不适用于连接
Python: pyserial timeout doesn't seem to work on connect
如果未连接 COM 端口,我试图让 python 失败:
import serial
ser = serial
print("ermrmrmrr")
try:
ser = serial.Serial(
port = 'COM6',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1,
)
except:
print("what what in the butt")
ser.close()
sys.exit(0)
print("grrrrr")
输出是:
ermrmrmrr
what what in the butt
Traceback (most recent call last):
File "C:\Projects\Personal\Alex_Quadcopter\mine\scripts\lib\GetData.py", line 21, in <module>
write_timeout = 1,
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 31, in __init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialutil.py", line 240, in __init__
self.open()
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 78, in open
self._reconfigure_port()
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
'Original message: {!r}'.format(ctypes.WinError()))
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: OSError(22, 'The semaphore timeout period has expired.', None, 121)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\PrincipalAxes.py", line 11, in <module>
from lib import GetData as gd
File "C:\Projects\Personal\Alex_Quadcopter\mine\scripts\lib\GetData.py", line 25, in <module>
print("what what in the butt")
这个输出有点正常,除了它超时 30 秒 - 尝试连接后 1 分钟,而不是超时 1 秒后。
似乎 "The semaphore timeout period has expired." 超时,而不是实际的连接尝试。
问题是您无法关闭 ser
,因为发生了致命的事情。
你应该将你的捕获分解为单独的异常,而不是捕获所有异常。例如:
except serial.SerialException as e:
#There is no new data from serial port
print str(e)
sys.exit(1)
except TypeError as e:
print str(e)
ser.port.close()
sys.exit(1)
另请注意,通常将 0 传递给 sys.exit
表示成功。您应该传递 1 或其他一些非零数字来表示失败。
如果未连接 COM 端口,我试图让 python 失败:
import serial
ser = serial
print("ermrmrmrr")
try:
ser = serial.Serial(
port = 'COM6',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1,
)
except:
print("what what in the butt")
ser.close()
sys.exit(0)
print("grrrrr")
输出是:
ermrmrmrr
what what in the butt
Traceback (most recent call last):
File "C:\Projects\Personal\Alex_Quadcopter\mine\scripts\lib\GetData.py", line 21, in <module>
write_timeout = 1,
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 31, in __init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialutil.py", line 240, in __init__
self.open()
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 78, in open
self._reconfigure_port()
File "C:\Users\dingleberry\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
'Original message: {!r}'.format(ctypes.WinError()))
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: OSError(22, 'The semaphore timeout period has expired.', None, 121)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\PrincipalAxes.py", line 11, in <module>
from lib import GetData as gd
File "C:\Projects\Personal\Alex_Quadcopter\mine\scripts\lib\GetData.py", line 25, in <module>
print("what what in the butt")
这个输出有点正常,除了它超时 30 秒 - 尝试连接后 1 分钟,而不是超时 1 秒后。
似乎 "The semaphore timeout period has expired." 超时,而不是实际的连接尝试。
问题是您无法关闭 ser
,因为发生了致命的事情。
你应该将你的捕获分解为单独的异常,而不是捕获所有异常。例如:
except serial.SerialException as e:
#There is no new data from serial port
print str(e)
sys.exit(1)
except TypeError as e:
print str(e)
ser.port.close()
sys.exit(1)
另请注意,通常将 0 传递给 sys.exit
表示成功。您应该传递 1 或其他一些非零数字来表示失败。