断开连接后继续从Arduino接收数据

时间:2017-11-10 12:18:58

标签: python python-3.x arduino arduino-uno

我制作了一个仪表板,其中来自Arduino uno上的lightsensor的光输入被转换为百分比。在Arduino断开连接后,我正在尝试继续接收输入。

try:
    ser = Serial(
        port='COM3',
        baudrate=19200,
        parity=PARITY_NONE,
        stopbits=STOPBITS_ONE,
        bytesize=EIGHTBITS,
        timeout=0)
except:
    print("Disconnected")

def updateTick():
    try:
        f.root.after(1000, updateTick)
        value = ser.read()
        min = 25                #min light value
        max = 50                #max light value
        if value:
            lightNum = int.from_bytes(value, byteorder='little')
            print(lightNum)
            lightToPercentage = round((lightNum - min) * 100 / (max - min))
            #print(lightToPercentage)
            view.l1.lightLabelCount.config(text="{}%".format(lightToPercentage))
            checkPreset(lightToPercentage)
            updateGraph(lightToPercentage)
    except:
        view.l1.lightLabelCount.config(text="N/A")
        view.l1.lightLabelPreset.config(text="[Restart]")

因此,当我启动仪表板时,我从Arduino接收数据,当我断开我的arduino时,仪表板中的百分比变为N / A.

但我想要做的是在Arduino再次连接后再次接收数据。

知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

我会尝试表达可能的解决方案的伪代码。它可能会有所帮助。

if(check if serial comm. exist)
   read received value
   write the value to dashboard
else
   write last received value to dashboard
   while(!(check if serial comm. exist))
      keep writing last received value to dashboard
//when comm. come back, it'll automaticly update the value