积累修改;

This commit is contained in:
何 泽隆
2024-12-12 22:02:09 +08:00
parent 38d6ff30a3
commit 810bac464f
12 changed files with 599 additions and 234 deletions

View File

@@ -21,6 +21,10 @@ ParamMap_LaminaController = {
# 7 - numberList
0x00: ["绝缘检测正电阻", 6],
0x02: ["绝缘检测负电阻", 6],
0x04: ["负对地绝缘检测电压(动作)" , 2, 100],
0x05: ["负对地绝缘检测电压(未动作)" , 2, 100],
0x06: ["正对地绝缘检测电压(动作)" , 2, 100],
0x07: ["正对地绝缘检测电压(未动作)" , 2, 100],
0x0B: ["事件标志", 1],
0x0C: ["告警字1", 1],
0x0D: ["告警字2", 1],
@@ -117,16 +121,17 @@ ParamMap_LaminaController = {
MemoryMap_280039 = {
'image_size': 2*0x030000, # 镜像文件大小
'app_size': 2*0x004000, # 应用程序大小
'boot_size': 2*0x014000, # Boot程序大小
'boot_size': 2*0x004000, # Boot程序大小
'app_size': 2*0x014000, # 应用程序大小
'boot_addr': 0x000000, # Boot程序地址
'main_header': 0x006000, # main信息地址
'back_header': 0x007000, # back信息地址
'main_info': 0x088000, # main版本地址
'back_info': 0x007000, # back版本地址
'main_addr': 0x008000, # main程序地址
'back_addr': 0x01C000, # back程序地址
'main_info': 0x088000, # main版本地址
'back_info': 0x09C000, # back版本地址
}
class LaminaController:
@@ -553,44 +558,49 @@ class LaminaController_new(DeviceSerial):
self.flag_print = False
self.retry = 3
self.time_out = 1.5
self.block['file_block_size'] = 240
# 读取config
self.block['type'] = 'record_cfg'
self.block['step'] = 'start'
frame_master = function.protocols.make_frame_modbus(self.block)
try:
status = 'init' # 初始化
self.block['file_block_size'] = 240
ret, pbar = True, None
frame_data_cfg = []
while ret:
if ret := self.__transfer_data(frame_master):
frame_data_cfg.append(self.output['record'])
if self.output['record']['seq'] == 0:
pbar = tqdm(total=self.output['record']['total'] + 1, desc="Record Config Reading")
self.block['step'] = 'next'
frame_master = function.protocols.make_frame_modbus(self.block)
elif (self.output['record']['seq']) >= self.output['record']['total']:
ret = False
pbar and pbar.update()
pbar and pbar.close()
status = 'read_config' # 读取config
self.block['type'] = 'record_cfg'
self.block['step'] = 'start'
ret, pbar = True, None
frame_data_cfg = []
while ret:
if ret := self._transfer_data():
frame_data_cfg.append(self.output['record'])
if self.output['record']['seq'] == 0:
pbar = tqdm(total=self.output['record']['total'] + 1, desc="Record Config Reading")
self.block['step'] = 'next'
elif (self.output['record']['seq']) >= self.output['record']['total']:
ret = False
pbar and pbar.update()
pbar and pbar.close()
# 读取data
self.block['type'] = 'record_data'
self.block['step'] = 'start'
frame_master = function.protocols.make_frame_modbus(self.block)
ret, pbar = True, None
frame_data_record = []
while ret:
if ret := self.__transfer_data(frame_master):
frame_data_record.append(self.output['record'])
if self.output['record']['seq'] == 0:
pbar = tqdm(total=self.output['record']['total'] + 1, desc="Record Data Reading")
self.block['step'] = 'next'
frame_master = function.protocols.make_frame_modbus(self.block)
elif (self.output['record']['seq']) >= self.output['record']['total']:
ret = False
pbar and pbar.update()
pbar and pbar.close()
status = 'read_data' # 读取data
self.block['type'] = 'record_data'
self.block['step'] = 'start'
ret, pbar = True, None
frame_data_record = []
while ret:
if ret := self._transfer_data():
frame_data_record.append(self.output['record'])
if self.output['record']['seq'] == 0:
pbar = tqdm(total=self.output['record']['total'] + 1, desc="Record Data Reading")
self.block['step'] = 'next'
elif (self.output['record']['seq']) >= self.output['record']['total']:
ret = False
pbar and pbar.update()
pbar and pbar.close()
except Exception as ex:
""" 通用异常处理 """
self.flag_print, self.retry, self.time_out = param_saved
report = f'Record Fail: {status}'
report += f', Frame in {self.block["record"]["seq"]}' if status == 'read_config' or status == 'read_data' else ''
report += f'\n Error by {ex}' if not isinstance(ex, AssertionError) else ''
print(report)
return False
self.flag_print, self.retry, self.time_out = param_saved
if (len(frame_data_record) != 32) or (len(frame_data_cfg) != 3):
@@ -602,7 +612,7 @@ class LaminaController_new(DeviceSerial):
config_record = {'LinePos': []}
pos = 0
if data_cfg_bare[pos+1] != 0xF1 or data_cfg_bare[pos] != 0xF1:
Warning("config 配置文件格式异常")
raise Warning("config 配置文件格式异常")
pos += 2
config_record['LinePos'].append(pos)
len_faultword = (data_cfg_bare[3] * 0x100 + data_cfg_bare[2])
@@ -621,7 +631,7 @@ class LaminaController_new(DeviceSerial):
config_record['Standard'] = data_cfg_bare[pos+1] * 0x100 + data_cfg_bare[pos]
pos += 2
if data_cfg_bare[pos+1] != 0xF2 or data_cfg_bare[pos] != 0xF2:
Warning("config 配置文件格式异常")
raise Warning("config 配置文件格式异常")
pos += 2
config_record['LinePos'].append(pos)
config_record['ChannelNum'] = data_cfg_bare[pos+1] * 0x100 + data_cfg_bare[pos]
@@ -631,7 +641,7 @@ class LaminaController_new(DeviceSerial):
config_record['ChNum_Dgt'] = data_cfg_bare[pos+1] * 0x100 + data_cfg_bare[pos]
pos += 2
if data_cfg_bare[pos+1] != 0xF3 or data_cfg_bare[pos] != 0xF3:
Warning("config 配置文件格式异常")
raise Warning("config 配置文件格式异常")
pos += 2
config_record['LinePos'].append(pos)
config_record['ChannelDescription'] = []
@@ -648,7 +658,7 @@ class LaminaController_new(DeviceSerial):
config_record['ChannelDescription'].append(bytearray(ch_desc).decode())
config_record['ChannelCoefficient'].append(ch_coe)
if data_cfg_bare[pos+1] != 0xF4 or data_cfg_bare[pos] != 0xF4:
Warning("config 配置文件格式异常")
raise Warning("config 配置文件格式异常")
pos += 2
config_record['LinePos'].append(pos)
config_record['SysFreq'] = data_cfg_bare[pos+1] * 0x100 + data_cfg_bare[pos]
@@ -664,7 +674,7 @@ class LaminaController_new(DeviceSerial):
config_record['TimeFactor'] = data_cfg_bare[pos+1] * 0x100 + data_cfg_bare[pos]
pos += 2
if data_cfg_bare[pos+1] != 0xF5 or data_cfg_bare[pos] != 0xF5:
Warning("config 配置文件格式异常")
raise Warning("config 配置文件格式异常")
pos += 2
config_record['LinePos'].append(pos)
time_stamp = {
@@ -726,56 +736,57 @@ class LaminaController_new(DeviceSerial):
return True
def frame_update(self, path_bin: Path, makefile: bool = False) -> bool:
def frame_update(self, path_file: Path, makefile: bool = False) -> bool:
""" 程序升级
注意: 在使用单板升级测试时, 需要关闭低电压检测功能, 否则无法启动升级流程;
"""
if makefile:
self.block['file'], file_bin = GeneratePackage_DLSP001_p280039(path_bin)
else:
self.block['file'] = path_bin.read_bytes()
self.block['header_offset'] = 184
self.block['type'] = 'update'
param_saved = self.flag_print, self.retry, self.time_out
self.retry = 5
self.retry = 3
self.time_out = 0.5
self.flag_print = False
try:
status = 'init' # 初始化
if not path_file.exists():
raise Exception("工程编译目标文件不存在.")
if makefile and self.make_package is not None:
self.block['file'] = self.make_package(path_file)
else:
self.block['file'] = path_file.read_bytes()
# 启动帧
self.block['step'] = 'start'
self.block['index'] = 0
frame_master = function.protocols.make_frame_modbus(self.block)
if not self.__transfer_data(frame_master):
self.block['type'] = 'update'
self.block['header_offset'] = 184
status = 'start' # 启动帧
self.block['step'] = 'start'
self.block['index'] = 0
assert self._transfer_data()
self.block['file_block_size'] = self.output['upgrade']['length']
# 避免接收到延迟返回报文
time.sleep(self.time_out)
status = 'trans' # 文件传输
self.retry = 3
self.time_out = 1.5
self.block['step'] = 'trans'
self.block['index'] = 0
frame_total = ceil((len(self.block['file']) - self.block['header_offset']) / self.block['file_block_size'])
for idx in tqdm(range(frame_total), desc="File Transmitting"):
self.block['index'] = idx
assert self._transfer_data()
status = 'end' # 结束升级
self.time_out = 1
self.block['step'] = 'end'
self.block['index'] += 1
assert self._transfer_data()
except Exception as ex:
""" 通用异常处理 """
self.flag_print, self.retry, self.time_out = param_saved
print('Upgrade Fail: start')
return False
self.block["data"]['file_block_size'] = self.output['upgrade']['length']
# 避免接收到延迟返回报文
time.sleep(self.time_out)
# 文件传输
self.retry = 3
self.time_out = 1.5
self.block["data"]['step'] = 'trans'
self.block['index'] = 0
frame_total = ceil((len(self.block["data"]['file']) - self.block['header_offset']) / self.block["data"]['file_block_size'])
for idx in tqdm(range(frame_total), desc="File Transmitting"):
self.block["data"]['index'] = idx
frame_master = function.protocols.make_frame_modbus(self.block)
if not self.__transfer_data(frame_master):
self.flag_print, self.retry, self.time_out = param_saved
print(f'Upgrade Fail: trans data in {idx}')
return False
# 结束升级
self.time_out = 1
self.block["data"]['step'] = 'end'
self.block["data"]['index'] += 1
frame_master = function.protocols.make_frame_modbus(self.block)
if not self.__transfer_data(frame_master):
self.flag_print, self.retry, self.time_out = param_saved
print(f'Upgrade Fail: end')
report = f'Upgrade Fail: {status}'
report += f', Frame in {self.block["index"]}' if status == 'trans' else ''
report += f'\n Error by {ex}' if not isinstance(ex, AssertionError) else ''
print(report)
return False
self.flag_print, self.retry, self.time_out = param_saved
@@ -814,7 +825,7 @@ def GeneratePackage(type_dev: str, path_hex: Path, **kwargs) -> bytearray:
print("Package generated successfully.")
print(f"File name: {path_hex.name}")
print(f"File Info:")
print(f"\t header_length={len(main_header)}, bin_length={len(bin_main)}[{hex(len(bin_main))}]")
print(f"\t header_length={len(main_header)}, bin_length={len(bin_main)}[{hex(len(bin_main) // 2)}]")
# 组装镜像
Image = [0xFF] * (len(main_header) + len(encrypt_main))
@@ -883,9 +894,9 @@ def GenerateImage(type_dev: str, path_boot: Path, path_main: Path, path_back: Pa
# Log打印
print("Merge Image generated successfully.")
print(f"Main File:")
print(f"\t header_length={len(main_header)}, bin_length={len(bin_main)}[{hex(len(bin_main))}]")
print(f"\t header_length={len(main_header)}, bin_length={len(bin_main)}[{hex(len(bin_main) // 2)}]")
print(f"Back File:")
print(f"\t header_length={len(back_header)}, bin_length={len(bin_back)}[{hex(len(bin_back))}]")
print(f"\t header_length={len(back_header)}, bin_length={len(bin_back)}[{hex(len(bin_back) // 2)}]")
# 额外文件
if 'output_header' in kwargs.keys() and kwargs['output_header']: