【语音告警】博灵智能语音报警灯Modbus TCP触发告警实例-语音报警灯|声光报警器|网络信号灯

发布于:2025-07-02 ⋅ 阅读:(22) ⋅ 点赞:(0)

功能说明

本文将以Python代码为例,讲解如何通过Python代码调用博灵语音通知终端A4实现声光语音告警。

本代码实现Python触发Modbus写多寄存器和写单寄存器实现调用通知终端模板播报功能(通知终端内置TTS语音合成技术,本案例不讲解如何文本转语音)。

代码实现

本文使用环境

  • python 3.13
  • pymodbus v3.9.2

在报警灯后台开启Modbus TCP服务开关,可以自定义端口

在这里插入图片描述
创建模版指令,播报模式支单次、周期、循环播报
在这里插入图片描述

写单寄存器

执行以下脚本,即可向报警灯发送写单寄存器

from pymodbus.client import ModbusTcpClient
from pymodbus.exceptions import ModbusException
from pymodbus.pdu import ExceptionResponse


def write_single_register():
    # 创建Modbus TCP客户端
    client = ModbusTcpClient("192.168.0.88", port=502)

    try:
        # 连接到Modbus服务器
        if not client.connect():
            print("无法连接到Modbus服务器")
            return

        # 地址
        addr = 99
        # 值
        val = 0x000A

        # 写入单个寄存器
        # 参数: 寄存器地址, 值, 从站ID
        response = client.write_register(address=addr, value=val, slave=1)

        # 检查响应
        if isinstance(response, ExceptionResponse):
            print(f"Modbus异常: {response}")
        elif response.isError():
            print(f"Modbus错误: {response}")
        else:
            print(f"成功写入寄存器{addr},值: {val}")

    except ModbusException as e:
        print(f"Modbus错误: {e}")
    finally:
        # 关闭连接
        client.close()


if __name__ == "__main__":
    write_single_register()

写多寄存器

执行以下脚本,即可向报警灯发送写多寄存器

from pymodbus.client import ModbusTcpClient
from pymodbus.exceptions import ModbusException
from pymodbus.pdu import ExceptionResponse


def write_multiple_registers():
    # 创建Modbus TCP客户端
    client = ModbusTcpClient("192.168.0.88", port=502)

    try:
        # 连接到Modbus服务器
        if not client.connect():
            print("无法连接到Modbus服务器")
            return

        # 地址
        addr = 100

        # 准备要写入的值列表(使用整数而不是字符串)
        values = [
            0x000F,  # 15
            0x008A,  # 138
            0x2BE2,  # 11234
            0x0000,  # 0
            0x0000,  # 0
            0x0000,  # 0
            0x0000,  # 0
            0x0000,  # 0
            0x0000,  # 0
            0x0000,  # 0
            0x0001,  # 1
            0x0000,  # 0
            0x0301,  # 769
            0x0000,  # 0
            0x0000,  # 0
            0x00E6,  # 230
            0xACA2,  # 44194
            0x00E8,  # 232
            0xBF8E,  # 49038
            0x00E4,  # 228
            0xBDBF,  # 48575
            0x00E7,  # 231
            0x94A8,  # 38056
        ]

        # 写入多个寄存器
        # 参数: 起始地址, 值列表, 从站ID
        response = client.write_registers(address=addr, values=values, slave=1)

        # 检查响应
        if isinstance(response, ExceptionResponse):
            print(f"Modbus异常: {response}")
        elif response.isError():
            print(f"Modbus错误: {response}")
        else:
            print(f"成功写入寄存器{addr}-{addr+len(values)-1},值: {values}")

    except ModbusException as e:
        print(f"Modbus错误: {e}")
    except Exception as e:
        print(f"其他错误: {e}")
    finally:
        # 关闭连接
        client.close()


if __name__ == "__main__":
    write_multiple_registers()


网站公告

今日签到

点亮在社区的每一天
去签到