在Python程序中重新检测Arduino板

2024-04-19 13:08:52 发布

您现在位置: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、Python2.7和Arduino Uno和Leornardo


Tags: self程序readifgifelsearduinoser
1条回答
网友
1楼 · 发布于 2024-04-19 13:08:52

我要做的是:

  1. list all ports
  2. 如果列表不为空,请创建新的串行连接

Works for me

这符合你的期望吗?你知道吗

顺便说一下,千万不要写try: except:。总是说你在抓什么。你知道吗

相关问题 更多 >