无法配置并连接Azure IoT Edge Modbus模块求助排查
问题描述
我正尝试搭建一个模拟场景,让IoT Edge设备接收来自设备的Modbus over IP数据并发送至IoT Hub。
已完成以下操作:
- 通过X509自签名认证完成IoT Edge设备部署
- 添加Azure Marketplace提供的Modbus模块
- 修改模块孪生标识,添加SlaveConfig配置:
"SlaveConfigs": { "Slave01": { "SlaveConnection": "localhost", "HwId": "SimulatedDevice-01", "Operations": { "Op01": { "PollingInterval": "1000", "UnitId": "1", "StartAddress": "0", "Count": "2", "DisplayName": "SensorData" } } } }, "PublishInterval": "2000"
但查看Modbus模块日志时出现错误:
Desired property change: {"properties":{"desired":{"PublishInterval":"2000","SlaveConfigs":{"Slave01":{"SlaveConnection":"localhost","HwId":"SimulatedDevice-01","Operations":{"Op01":{"PollingInterval":"1000","UnitId":"1","StartAddress":"0","Count":"2","DisplayName":"SensorData"}}}}}},"$version":5} No configuration found in desired properties, look in local... No configuration found in local file. Saving reported properties: {"PublishInterval":null,"SlaveConfigs":null} IoT Hub module client initialized. Desired property change: {"properties":{"desired":{"SlaveConfigs":{"Slave01":{"SlaveConnection":"localhost","HwId":"SimulatedDevice-01","Operations":{"Op01":{"PollingInterval":"1000","UnitId":"1","StartAddress":"0","Count":"2","DisplayName":"SensorData"}}}},"PublishInterval":"2000"}},"$version":5} No configuration found in desired properties, look in local... No configuration found in local file. Saving reported properties: {"PublishInterval":null,"SlaveConfigs":null}
模拟Modbus服务器代码:
from pymodbus.server import StartTcpServer from pymodbus.device import ModbusDeviceIdentification from pymodbus.datastore import ModbusSequentialDataBlock from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext import random # Create a data block initialized with two values block = ModbusSequentialDataBlock(0, [random.randint(20, 25), random.randint(30, 40)]) # Create a slave context with the data block slaves = { 0x01: ModbusSlaveContext(di=block, co=block, hr=block, ir=block) } context = ModbusServerContext(slaves=slaves, single=True) # Create a Modbus device identification identity = ModbusDeviceIdentification() identity.VendorName = 'Mdb' identity.ProductCode = 'Modbus Server' identity.VendorUrl = 'http://github.com/username/repo' identity.ProductName = 'Pymodbus Server' identity.ModelName = 'Pymodbus Server' identity.MajorMinorRevision = '1.0' # Start the Modbus TCP server StartTcpServer(context=context, identity=identity, address=('localhost', 502))
已尝试按「启动模拟服务器→启动IoT Edge设备」的顺序操作,但问题依旧,求排查方向。
排查与解决建议
1. 修正模块孪生配置的层级结构
Azure IoT Edge Modbus模块的配置必须嵌套在ModbusMaster节点下,你的当前配置直接将SlaveConfigs和PublishInterval放在desired根节点,模块无法识别。正确配置结构如下:
"properties": { "desired": { "ModbusMaster": { "SlaveConfigs": { "Slave01": { "SlaveConnection": "localhost", "HwId": "SimulatedDevice-01", "Operations": { "Op01": { "PollingInterval": "1000", "UnitId": "1", "StartAddress": "0", "Count": "2", "DisplayName": "SensorData", "FunctionCode": "3" } } } }, "PublishInterval": "2000" } } }
- 必须添加
ModbusMaster父节点,这是模块识别配置的核心要求 - 每个操作需明确指定
FunctionCode(比如读保持寄存器用3,读离散输入用2),你的模拟服务器给所有寄存器块复用了同一数据块,建议指定FunctionCode": "3"
2. 修正SlaveConnection地址
如果模拟Modbus服务器运行在IoT Edge设备的宿主机上,而Modbus模块运行在容器内,localhost在容器内指向容器自身,无法访问宿主机服务,需替换为:
- Linux/Windows系统:
host.docker.internal(Docker Desktop环境)或宿主机的实际IP地址
3. 确保模拟服务器可被访问
修改模拟服务器代码,将绑定地址从localhost改为0.0.0.0,允许所有网卡访问:
StartTcpServer(context=context, identity=identity, address=('0.0.0.0', 502))
同时可在IoT Edge设备上执行以下命令测试连通性:
# 进入Modbus模块容器 docker exec -it <modbus-module-container-name> bash # 测试Modbus服务器连接 telnet <host-ip> 502
4. 检查模块部署的端口配置
确保Modbus模块部署时,容器网络可访问宿主机的502端口。无需额外映射端口到外部,但需确认容器和宿主机处于同一网络环境(默认bridge网络即可)。
内容的提问来源于stack exchange,提问作者Whey
相关产品推荐
相关产品推荐

