脚本不断升旗不符合设置条件

2024-05-20 22:04:32 发布

您现在位置:Python中文网/ 问答频道 /正文

以下脚本用于发送电子邮件警报,由于某些原因,它一直绕过我设置的标志。。 你知道吗

############################################################
##                       MAIN                             ##
############################################################

Open_serial()

while True:

    line = ser.readline()
    if line:
        tmp = line.split()
        if 'tank' in line:
            tank_temp = tmp[1]
            print 'Tank temperature:',tank_temp
            if tank_temp >= 27:
                flag = raise_flag()
                if flag:
                    send_email('Tank temperature overheat',tank_temp)
                    flag = False
            elif tank_temp <= 23:
                flag = raise_flag()
                if flag:
                    send_email('Tank temperature too low',tank_temp)
                    flag = False

        if 'led' in line:
            led_temp = tmp[1]
            print 'Lights temperature:',led_temp
            if led_temp >= 55:
                flag = raise_flag()
                if flag:
                    send_email('Lights temperature Overheat',led_temp)
                    flag = False

        if 'White' in line:

            white_pwm = tmp[1]
            print 'White lights PWM value:',white_pwm
            white_per = white_percent(white_pwm)
            print 'White lights % value:',white_per

        if 'Blue' in line:
            blue_pwm = tmp[1]
            print 'Blue lights PWM:',blue_pwm
            blue_per = blue_percent(blue_pwm)
            print 'Blue lights % value:',blue_per

下面是flag函数: 你知道吗

def raise_flag():
    global start
    interval = 900
    if start > interval:
        start = 0
        flag = True
        return flag
    else:
        flag = False
        start = start + 1
        time.sleep(1)
        return flag

脚本不断发送电子邮件时,温度范围是白色的,即25°C,我不知道为什么它不断发送电子邮件?我放置了标志,所以当条件满足时,它不会发送1000封电子邮件,每15分钟只发送1封。。。你知道吗


Tags: inlediflinebluestarttemptmp
1条回答
网友
1楼 · 发布于 2024-05-20 22:04:32

readline()传入行。拆分()将生成字符串列表。因此,您正在比较字符串(数据)和整数(27)。这不是个好主意。见this explanation of how sting/int comparison works。你知道吗

要解决此问题,只需在操作数据之前将其转换为如下浮点值:

tank_temp = float(tmp[1])

或者像这样比较字符串:

if tank_temp >= '27':

我会选择前者,因为你的数据应该是浮点数,所以把它们转换成浮点数是有意义的。你知道吗

相关问题 更多 >