Windows 7 在Python中临时COM端口断开后幸存
当我从PC上拔下USB/Arduino时,从中删除的Python脚本崩溃Windows 7 在Python中临时COM端口断开后幸存,windows-7,python-2.7,serial-port,arduino,Windows 7,Python 2.7,Serial Port,Arduino,当我从PC上拔下USB/Arduino时,从中删除的Python脚本崩溃 #The connection to the Arduino ser = serial.Serial('COM19',9600) #Not connected to Arduino before connection is made connected = False #Loop until the Arduino is connected while not connected: serin = ser.re
#The connection to the Arduino
ser = serial.Serial('COM19',9600)
#Not connected to Arduino before connection is made
connected = False
#Loop until the Arduino is connected
while not connected:
serin = ser.read()
connected = True
#Debug Arduino connection
if connected == True:
pprint('connected to arduino')
我曾尝试使用TRY/EXCEPT来确保连接重新连接,但当Arduino再次插入时,它将无法识别COM端口,直到计算机复位
from pprint import pprint
import time
import serial
while True:
#The connection to the Arduino
try:
ardResponse = serial.Serial('COM19',9600)
except IOError:
connected = False
pprint('Arduino not connected to COM3')
time.sleep(10)
continue
serin = ser.read()
connected = True
pprint('Connected to Arduino')
ser.write('1')
while ser.read() == '1':
ser.read()
pprint ('Arduino has finished being flashed')
ser.close()
pprint('Connection closed')
time.sleep(5)
这是因为COM端口保持打开状态,因此脚本无法重用它吗?有没有办法避免此问题?尝试在except:块中执行ser.close()
然后提示用户进行修复,并在再次尝试ser.open()之前按enter键