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

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,物联网)