2014-03-04 45 views
1

對於我的程序,我需要在運行程序期間檢測到Arduino板斷開連接後重新連接。我已經完美地檢測到了斷開連接,但是當重新連接電路板時,該程序不處理它。在Python程序中重新檢測Arduino板

我試圖使該代碼(這是一個UI,我需要對板的連接狀態進行通訊):

def update(self): 
    try: 
      ser.write('P') 
      time.sleep(0.1) 
      if ser.read() == "V": 
        self.photo=PhotoImage(file ='buttongreen.gif') 
      else: 
        self.photo=PhotoImage(file ='buttonred.gif') 
        ser.close() 
    except: 
      try: 
        ser.open() 
        if ser.read() == "V": 
          self.photo=PhotoImage(file ='buttongreen.gif') 
        else: 
          self.photo=PhotoImage(file ='buttonred.gif') 
      except: 
        self.photo=PhotoImage(file ='buttonred.gif') 

pic = Canvas(self,width =64, height = 64, bg ='blue') 
pic.grid(row=1, columnspan=3,column=1,padx = 10, pady =10) 
pic.create_image(34,34, image=self.photo) 

是否有可能解決這個問題,該怎麼辦?謝謝!

編輯:我用的Tkinter,Python 2.7版和一個Arduino UNO和Leornardo

工作
+0

我猜'ser'是串口,你使用哪個庫?在哪個操作系統? – Cilyan

+0

我在其他代碼中使用了Windows 7的串行,時間和Tkinter庫。 – lukronos

回答

1

我要做的就是:

  1. list all ports
  2. 如果列表不爲空,創建一個新的串行連接

Works for me

這是否符合您的期望?

不要寫try: except:順便說一下。總是說你在抓什麼。

+0

是的,這是我的預期,現在我必須努力使它適應我的代碼,謝謝! ;) – lukronos