Skip to content

Commit a95c093

Browse files
committed
修复测点映射失效bug
1 parent c20c3af commit a95c093

5 files changed

Lines changed: 30 additions & 22 deletions

File tree

src/device/core/data/data_reader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def _log(self):
5656
def _get_change_source(self) -> ChangeSource:
5757
"""根据协议处理器类型获取变更来源
5858
59-
服务端设备:外部客户端通过协议写入,属于协议远程修改
60-
客户端设备:从远程服务器读取数据变化,属于客户端读取
59+
服务端设备:get_slave_values 是从自己的内存寄存器读取值,
60+
不是远程修改,应使用 INTERNAL。
61+
真正的远程客户端写入通过 _on_modbus_client_write 回调处理。
62+
客户端设备:从远程服务器读取数据变化,属于客户端读取。
6163
"""
62-
if isinstance(self._handler, ServerHandler):
63-
return ChangeSource.PROTOCOL
64-
elif isinstance(self._handler, ClientHandler):
64+
if isinstance(self._handler, ClientHandler):
6565
return ChangeSource.CLIENT_READ
6666
return ChangeSource.INTERNAL
6767

src/device/protocol/modbus_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def write_value(self, point: BasePoint, value: Any) -> bool:
134134
self._server.setValueByAddress(
135135
point.func_code, slave_id, point.address, write_val, point.decode
136136
)
137-
self._log.info(f"Modbus 客户端写入测点 {point.code} 成功: value={write_val}")
137+
self._log.info(f"写入测点 {point.code} 成功: value={write_val}")
138138
return True
139139

140140
return False

src/device_controller.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,6 @@ async def import_device_from_db(self):
188188
)
189189
general_device.name = channel_name
190190

191-
# 仅服务端自动启动数据更新线程(用于同步内存变更)
192-
# 客户端需要手动开启或点击自动读取,避免自动轮询外部设备
193-
is_client = channel_protocol_type in [
194-
ProtocolType.ModbusTcpClient,
195-
ProtocolType.Iec104Client,
196-
ProtocolType.Dlt645Client,
197-
ProtocolType.Iec61850Client,
198-
]
199-
200-
if not is_client:
201-
general_device.data_update_thread.start()
202-
203191
self.device_list.append(general_device)
204192
self.device_map[general_device.name] = general_device
205193

src/proto/pyModbus/server/modbus_server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,11 @@ def setValueByAddress(
556556
registers[0] = ((registers[0] & 0xFF) << 8) | ((registers[0] >> 8) & 0xFF)
557557

558558
# 设置寄存器值
559-
if func_code == 10:
560-
func_code = 6
559+
# 遥测点 func_code=3 (读保持寄存器) 写入时需要转为 func_code=6 (写单寄存器)
560+
# 遥调点 func_code=6 (写单寄存器) 直接使用
561+
# func_code=10/16 (写多寄存器) 转为 func_code=3
562+
if func_code in (3, 10, 16):
563+
func_code = 3
561564

562565
slave_ctx = self.slaves[rtu_addr]
563566
if isinstance(slave_ctx, CallbackDeviceContext):

src/web/channel/channel_controller.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,17 @@ async def create_and_start_device(req: CreateAndStartDeviceRequest, request: Req
392392
is_start=True,
393393
)
394394
general_device.name = channel_name
395-
general_device.data_update_thread.start()
395+
396+
# 仅客户端设备启动数据更新线程(从远程服务器读取数据)
397+
# 服务端设备不需要,因为数据由用户手动设置或远程客户端写入
398+
is_client = channel_protocol_type in [
399+
ProtocolType.ModbusTcpClient,
400+
ProtocolType.Iec104Client,
401+
ProtocolType.Dlt645Client,
402+
ProtocolType.Iec61850Client,
403+
]
404+
if is_client:
405+
general_device.data_update_thread.start()
396406

397407
# 添加到设备控制器
398408
device_controller = request.app.state.device_controller
@@ -598,7 +608,14 @@ async def _reload_device_instance(device_controller, channel_id: int, is_start:
598608
)
599609
new_device.name = device_name
600610

601-
if is_start:
611+
# 仅客户端设备启动数据更新线程
612+
is_client = channel_protocol_type in [
613+
ProtocolType.ModbusTcpClient,
614+
ProtocolType.Iec104Client,
615+
ProtocolType.Dlt645Client,
616+
ProtocolType.Iec61850Client,
617+
]
618+
if is_start and is_client:
602619
new_device.data_update_thread.start()
603620

604621
device_controller.device_list.append(new_device)

0 commit comments

Comments
 (0)