积累更新

This commit is contained in:
何 泽隆
2024-10-31 09:01:49 +08:00
parent eeb476a538
commit 73a36c35bb
13 changed files with 175 additions and 165 deletions

View File

@@ -3,9 +3,9 @@ import socket
import random
import hashlib
from pathlib import Path
from function.tools.ByteConv import conv_int_to_array, trans_list_to_str
from function.frame import make_frame_modbus, check_frame_modbus, print_display
from function.file_upgrade import build_header, file_encryption
from . import tools
from . import function
ParamMap_EnergyRouter = {
0x00: ['编译日期', 4, 6],
@@ -46,7 +46,7 @@ class EnergyRouter:
def __transfer_data(self, frame: bytes) -> bool:
""" 数据传输处理函数 """
if self.tcp_socket is None:
print(trans_list_to_str(frame))
print(tools.ByteConv.trans_list_to_str(frame))
return False
try:
@@ -54,15 +54,15 @@ class EnergyRouter:
time.sleep(self.time_out)
frame_recv = self.tcp_socket.recv(128)
self.output = check_frame_modbus(frame_recv, self.block)
self.output = function.frame.check_frame_modbus(frame_recv, self.block)
if self.flag_print:
print("Read Frame: ", trans_list_to_str(frame_recv))
print("Read Frame: ", tools.ByteConv.trans_list_to_str(frame_recv))
if 'Regs' in self.output.keys():
print_display(self.output['Regs'])
function.frame.print_display(self.output['Regs'])
except Exception as ex:
print("Error Info: ", ex)
if self.flag_print and frame_recv:
print("Fail Data: " , trans_list_to_str(frame_recv))
print("Fail Data: " , tools.ByteConv.trans_list_to_str(frame_recv))
self.output['result'] = False
return self.output['result']
@@ -71,7 +71,7 @@ class EnergyRouter:
self.block['type'] = 'read'
self.block['data_addr'] = daddr
self.block['data_len'] = dlen
frame = make_frame_modbus(self.block)
frame = function.frame.make_frame_modbus(self.block)
return self.__transfer_data(frame)
@@ -86,7 +86,7 @@ class EnergyRouter:
self.block['file'] = Path(path_bin).read_bytes()
self.block['header_offset'] = 128
# 启动帧
frame_master = make_frame_modbus(self.block)
frame_master = function.frame.make_frame_modbus(self.block)
if not self.__transfer_data(frame_master):
self.flag_print, self.retry, self.time_out = param_saved
@@ -116,7 +116,7 @@ class EnergyRouter:
continue
seq_window[i] = 1
self.block['index'] = seq_offset + i
seq_frame_master[i] = make_frame_modbus(self.block)
seq_frame_master[i] = function.frame.make_frame_modbus(self.block)
self.tcp_socket.send(seq_frame_master[i])
# 接收帧回复
tmp = list(zip(range(len(seq_window)), seq_window))
@@ -126,7 +126,7 @@ class EnergyRouter:
# 接收到空数据, 对端已关闭连接
if seq_frame_slave[i] == '':
raise Exception("TCP closed.")
self.output = check_frame_modbus(seq_frame_slave[i], None)
self.output = function.frame.check_frame_modbus(seq_frame_slave[i], None)
seq_current, seq_hope = self.output['upgrade']['index'], self.output['upgrade']['hope']
if seq_current < seq_offset:
raise Exception("Error.")
@@ -152,14 +152,14 @@ class EnergyRouter:
# 结束升级
self.block['step'] = 'end'
frame_master = make_frame_modbus(self.block)
frame_master = function.frame.make_frame_modbus(self.block)
while self.output['result'] is False:
self.tcp_socket.send(frame_master)
frame_slave = self.tcp_socket.recv(8)
if frame_slave == '':
raise Exception("TCP closed.")
self.output = check_frame_modbus(frame_slave[:18], self.block)
self.output = function.frame.check_frame_modbus(frame_slave[:18], self.block)
def GeneratePackage_Demo_Xilinx(path_bin: Path):
@@ -188,19 +188,19 @@ def GeneratePackage_Demo_Xilinx(path_bin: Path):
md5_ctx = hashlib.md5()
md5_ctx.update(data_bin)
config["md5"] = list(md5_ctx.digest())
config['file_length'] = conv_int_to_array(len(data_bin))
config['file_length'] = tools.ByteConv.conv_int_to_array(len(data_bin))
config['hex_name'] = list(path_bin.name.encode())[:80]
if (header:= build_header(config, 128)) is None:
if (header:= function.file_upgrade.build_header(config, 128)) is None:
raise Exception("Header tag oversize. ")
if (header_512:= build_header(config, 512)) is None:
if (header_512:= function.file_upgrade.build_header(config, 512)) is None:
raise Exception("Header tag oversize. ")
data_encrypt = file_encryption(data_bin)
data_encrypt = function.file_upgrade.file_encryption(data_bin)
print("Upgrade file generated successfully.")
print(f"\t header_length={len(header)}, bin_length={len(data_bin)}[{hex(len(data_bin))}]")
print(f"\t file md5: {trans_list_to_str(config['md5'])}")
print(f"\t file md5: {tools.ByteConv.trans_list_to_str(config['md5'])}")
file1 = path_bin.parent / (path_bin.stem + '.dat')
file1.write_bytes(header + data_bin)
file2 = path_bin.parent / (path_bin.stem + '_h512.dat')