2013-04-04 84 views
1

這個Python腳本可以正常工作幾天然後崩潰,你知道爲什麼會發生這種情況或如何調試嗎? 它連接到一個Arduino,當它通過串行接收'1'時發送一個高電平。 腳本在計算機啓動時啓動,應該永遠運行。如果它確實崩潰了,我沒有辦法重新啓動腳本,因爲電腦位於遠程位置。是這個Python腳本爲什麼會崩潰?

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 

工作?

+5

你得到一個錯誤信息和/或一個堆棧跟蹤? – RichieHindle 2013-04-04 18:04:04

+3

看起來你應該實現一些日誌記錄,特別是如果它要運行很長時間。 – BenDundee 2013-04-04 18:25:59

回答

5

我看不到任何錯誤處理。所以,任何出錯都會導致腳本退出。例如,如果Internet連接或Twitter發生故障,則urlopen調用將失敗,整個腳本將停止工作。這是添加錯誤處理最明顯的地方,但是如果斷開串口的連接,會發生什麼?

添加錯誤處理來處理潛在錯誤,否則Python將處理錯誤,並且您不會喜歡這樣做的方式。

1

可以保護urllib.urlopen通話用try...except

try: 
    response = urllib.urlopen('http://search.twitter.com/search.json?q=%23workrestandplayground&result_type=recent&rpp=1&filter:retweets') 
except IOError: 
    # This includes urllib.ContentTooShortError 
    time.sleep(60) 
    continue 
j =json.loads(response.read())