1

For my program, I need to detect an Arduino board once it has been disconnected then reconnected, during the run of my program. I have perfectly detected the disconnection, but when reconnect the board, the program doesn't deal with it.

I tried to make this code for that (this is an UI, and I need to communicate about the connection state of the board) :

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)

Is it possible to solve this problem and how do that? Thanks!

Edit : I'm working with Tkinter, Python 2.7 and an Arduino Uno and Leornardo

lukronos
  • 75
  • 1
  • 11

1 Answers1

1

What I do is:

  1. list all ports
  2. if the list is not empty, create a new serial connection

Works for me

Does this meet your expectation?

Never write try: except: by the way. Always say what you are catching.

Community
  • 1
  • 1
User
  • 14,131
  • 2
  • 40
  • 59