功能说明
本文将以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 ExceptionResponsedef 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# 写入单个寄存器# 参数: 寄存器地址, 值, 从站IDresponse = 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 ExceptionResponsedef 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, # 150x008A, # 1380x2BE2, # 112340x0000, # 00x0000, # 00x0000, # 00x0000, # 00x0000, # 00x0000, # 00x0000, # 00x0001, # 10x0000, # 00x0301, # 7690x0000, # 00x0000, # 00x00E6, # 2300xACA2, # 441940x00E8, # 2320xBF8E, # 490380x00E4, # 2280xBDBF, # 485750x00E7, # 2310x94A8, # 38056]# 写入多个寄存器# 参数: 起始地址, 值列表, 从站IDresponse = 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()