Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 断开连接后继续从Arduino接收数据_Python_Python 3.x_Arduino_Arduino Uno - Fatal编程技术网

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

Python 断开连接后继续从Arduino接收数据,python,python-3.x,arduino,arduino-uno,Python,Python 3.x,Arduino,Arduino Uno,我制作了一个仪表板,从Arduino uno上的光传感器输入的光被转换成一个百分比。我正在尝试在我的Arduino断开连接后继续接收输入 try: ser = Serial( port='COM3', baudrate=19200, parity=PARITY_NONE, stopbits=STOPBITS_ONE, bytesize=EIGHTBITS, timeout=0) except:

我制作了一个仪表板,从Arduino uno上的光传感器输入的光被转换成一个百分比。我正在尝试在我的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时,仪表板中de的百分比变为N/A

但我要做的是,一旦Arduino再次连接,就可以再次接收数据


知道怎么做吗?

我将尝试表达我想到的可能解决方案的伪代码。这也许会有所帮助

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

我会尝试用伪代码来表达我想到的可能的解决方案。这也许会有所帮助

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