python检测光电感烟火灾探测报警器

2021/5/6 14:55:17

本文主要是介绍python检测光电感烟火灾探测报警器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import serial  # 引入库
import threading  # python 定时器
import platform  # 系统
import modbus_tk.modbus_rtu as rtu
import modbus_tk.defines as cst
try:
    sys = platform.system()
    if sys == "Windows":
        ser = serial.Serial(port='COM6', baudrate=9600, bytesize=8, parity="N", stopbits=1, timeout=0.1)
        # 设置电脑端为主机(Master)
        master = rtu.RtuMaster(ser)
        master.set_timeout(1.0)
        master.set_verbose(True)
    elif sys == "Linux":
        ser = serial.Serial(port='/dev/ttyUSB1', baudrate=9600, bytesize=8, parity="N", stopbits=1, timeout=0.1)
        # 设置电脑端为主机(Master)
        master = rtu.RtuMaster(ser)
        master.set_timeout(1.0)
        master.set_verbose(True)
    else:
        print("系统错误")
except Exception as e:
    print("串口系统错误")
def fun_timer():
    read_values = master.execute(1, cst.HOLDING_REGISTERS, 0x01, 12)      # 烟感设备地址 1
    # print(read_values)  # (0, 0, 0, 0, 0, 0, 259, 0, 0, 0, 0, 0)
    # print(read_values[2])  # (0, 0, 1, 0, 0, 0, 259, 0, 0, 0, 0, 0)
    # 报警器状态说明:
    # 报警器状态代码: 0 或 1
    # 报警器状态: 0 代表 正常没有烟雾 1 代表有烟报警
    if read_values[2] == 0:
        print('检测没有烟雾正常')
    else:
        print('检测到烟雾并报警')
    global timer
    timer = threading.Timer(1, fun_timer)
    timer.start()
timer = threading.Timer(1, fun_timer)
timer.start()




这篇关于python检测光电感烟火灾探测报警器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程