Skip to main content
 首页 » 编程设计

python之为什么这个 Python 脚本会崩溃

2025年05月04日16Leo_wl

这个 Python 脚本可以正常运行几天然后崩溃,您知道为什么会发生这种情况或如何调试它吗? 它连接到一个 Arduino,当它通过串行接收到“1”时,它会发送一个高电平引脚。 该脚本在计算机启动时启动并且应该永远运行。但是,如果它确实崩溃了,我没有办法重新启动脚本,因为计算机位于远程位置。

import json 
import urllib 
from pprint import pprint 
import time 
import serial 
 
#to collect the first tweet without telling the arduino 
countTweet = 0 
tweet= 0 
noTweet= 0 
 
#the infinate loop 
while True: 
    #the connection to the arduino 
    ser = serial.Serial('COM3',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') 
 
    #j contains the JSON 
    j =json.loads(urllib.urlopen('http://search.twitter.com/search.json?q=%23workrestandplayground&result_type=recent&rpp=1&filter:retweets').read()) 
 
    #Debug JSON from twitter (for faults on the Twitter end or possible GET limit id below 15 seconds per request) 
    pprint(j) 
 
    #find the text and the tweet id 
    if j['results']: 
        text = j['results'][0]['text'] 
        id = j['results'][0]['id'] 
        #how many times the Json is correct 
        tweet+= 1 
    else: 
        #How many times the Json is false 
        noTweet += 1 
 
    #print the text and id to the screen 
#    pprint(text) 
#    pprint(id) 
 
    #to isolate the first loop, if the first ID has been stored already (count == 1) 
    if countTweet != 0: 
        pprint ("new loop") 
        #if lastID is not equal to ID 
        if lastID != id: 
        #Tell Arduino to Vend 
            ser.write('1') 
            #ser.write('0') 
            #loop until the arduino tells us it is done vending 
            while ser.read() == '1': 
                ser.read() 
            #Debug 
            pprint(text) 
            pprint(id) 
            #Make lastID equal to ID 
            lastID = id 
            pprint ('lastID updated') 
        #if no new tweets, print      
        else: 
            pprint ('no new tweets') 
    #If it's the first loop, confirm by printing to the screen 
    else: 
        pprint("First loop complete") 
        lastID = id 
        pprint(lastID) 
 
 
    #make count not equal to 0 after first loop 
    countTweet += 1 
 
    pprint ('closing arduino connection') 
    ser.close() 
 
    #wait 
    pprint('waiting 15 seconds') 
    pprint ('Number of Tweets') 
    pprint (countTweet) 
    pprint('Working JSON') 
    pprint(tweet) 
    pprint('Broken JSON') 
    pprint(noTweet) 
    time.sleep(15) 

报错信息如下

    Traceback (most recent call last): 
  File "C:\Users\3d Exposure\Desktop\M001.py", line 19, in <module> 
    ser = serial.Serial('COM3',9600) 
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 31, in __init__ 
    SerialBase.__init__(self, *args, **kwargs) 
  File "C:\Python27\lib\site-packages\serial\serialutil.py", line 261, in __init__ 
    self.open() 
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 71, in open 
    self._reconfigurePort() 
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 186, in _reconfigurePort 
    raise ValueError("Cannot configure port, some setting was wrong. Original message: %s" % ctypes.WinError()) 
ValueError: Cannot configure port, some setting was wrong. Original message: [Error 31] A device attached to the system is not functioning. 

我认为这是这个声明的一个问题

while ser.read() == '1': 
    ser.read() 

我被告知这将忽略所有其他串行数据。 我怎样才能写这个,这样它就不会遗漏任何东西? 将

while ser.read() == '0': 
   break 

工作?

请您参考如下方法:

我没有看到任何错误处理。因此,任何出错都会导致脚本退出。例如,如果 Internet 连接或 Twitter 出现故障,urlopen 调用将失败,整个脚本将停止工作。这是添加错误处理的最明显的地方,但是如果断开串行端口会发生什么情况?

添加错误处理以处理潜在的错误,否则 Python 将处理错误,而您不会喜欢它的处理方式。