将读取文件添加PLC的名字

This commit is contained in:
您çšpengqi
2025-09-29 14:41:04 +08:00
parent 262e76c5db
commit 9fa100070e
4 changed files with 17 additions and 10 deletions

View File

@ -54,7 +54,7 @@ class Snap7Client:
self.last_connect_attempt = current_time
try:
with self.lock:
with self.lock: # 进入锁,其他线程需等待
self.client.connect(self.ip, self.rack, self.slot)
# 验证连接是否真正有效
@ -94,7 +94,7 @@ class Snap7Client:
return None # 返回None而不是零填充数据
try:
with self.lock:
with self.lock: # 进入锁,其他线程需等待
data = self.client.db_read(db_number, offset, size)
# 验证返回数据的有效性
if data is None or len(data) != size:
@ -456,7 +456,7 @@ class Snap7Client:
self.logger.error(f"❌ 读取DB{db_number}缓存文件异常: {e}", exc_info=True)
return None
def read_generic(self, db_number, offset, data_type, count=1):
def read_generic(self, db_number, offset, data_type, cache_file, count=1):
"""
通用读取接口(改进):
- DB100优先从缓存文件读取 → 再内存缓存 → 最后PLC
@ -465,7 +465,7 @@ class Snap7Client:
# 1、处理DB数据块优先从缓存文件读取
raw_data = None
if raw_data is None:
cache_file = f"plc_area_DB{db_number}.log"
cache_file = cache_file
raw_data = self.read_db_from_file_cache(db_number, offset, count, cache_file)
if raw_data is not None:
print(f"从缓存文件中读取DB{db_number}的数据")