调整设备文件结构;
This commit is contained in:
148
source/device/EnergyRouter.py
Normal file
148
source/device/EnergyRouter.py
Normal file
@@ -0,0 +1,148 @@
|
||||
import time
|
||||
import socket
|
||||
import random
|
||||
from pathlib import Path
|
||||
from tools.ByteConv import trans_list_to_str
|
||||
from func_frame import make_frame_modbus, check_frame_modbus
|
||||
|
||||
modbus_map = {
|
||||
0x00: ['编译日期', 4, 6],
|
||||
0x06: ['编译时间', 4, 5],
|
||||
}
|
||||
|
||||
class EnergyRouter:
|
||||
def __init__(self, ip="192.168.100.10", port=7, adddr_modbus=0x01):
|
||||
self._ip = ip
|
||||
self._port = port
|
||||
self._addr_modbus =adddr_modbus
|
||||
|
||||
self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.tcp_socket.connect((self._ip, self._port))
|
||||
|
||||
self.block = {
|
||||
'addr_dev' : self._addr_modbus,
|
||||
'data_define': modbus_map,
|
||||
}
|
||||
|
||||
def frame_read(self, daddr=0x00, dlen=0x10):
|
||||
self.block['type'] = 'read'
|
||||
self.block['data_addr'] = daddr
|
||||
self.block['data_len'] = dlen
|
||||
frame = make_frame_modbus(self.block)
|
||||
|
||||
if self.tcp_socket is None:
|
||||
print(trans_list_to_str(frame))
|
||||
return
|
||||
|
||||
# self.tcp_socket.recv(8)
|
||||
self.tcp_socket.send(bytearray(frame))
|
||||
time.sleep(0.5)
|
||||
frame_recv = self.tcp_socket.recv(128)
|
||||
output_text = check_frame_modbus(frame_recv, self.block)
|
||||
print(output_text)
|
||||
|
||||
|
||||
def frame_update(self, path_bin):
|
||||
""" 程序升级
|
||||
|
||||
"""
|
||||
self.block['type'] = 'update'
|
||||
self.block['step'] = 'start'
|
||||
self.block['file'] = Path(path_bin).read_bytes()
|
||||
self.block['header_offset'] = 128
|
||||
# 启动帧
|
||||
frame_master = bytearray(make_frame_modbus(self.block))
|
||||
|
||||
# 等待擦除完成返回
|
||||
time.sleep(0.4)
|
||||
self.tcp_socket.send(frame_master)
|
||||
frame_slave = self.tcp_socket.recv(32)
|
||||
if frame_slave == '':
|
||||
raise Exception("TCP closed.")
|
||||
|
||||
self.block['file_block_size'] = check_frame_modbus(frame_slave, self.block)
|
||||
|
||||
if self.block['file_block_size'] == 0:
|
||||
raise Exception("Error slave response.")
|
||||
|
||||
# 避免接收到延迟返回报文
|
||||
time.sleep(0.4)
|
||||
|
||||
# 文件传输
|
||||
self.block['index'] = 0
|
||||
self.block['step'] = 'trans'
|
||||
data_remain = len(self.block['file']) - self.block['header_offset']
|
||||
seq_offset = 0
|
||||
seq_window = [0, 0, 0, 0]
|
||||
seq_frame_master = [None, None, None, None]
|
||||
seq_frame_slave = [None, None, None, None]
|
||||
while data_remain > 0:
|
||||
# 更新并发送帧
|
||||
tmp = list(zip(range(len(seq_window)), seq_window))
|
||||
random.shuffle(tmp)
|
||||
for i, s in tmp:
|
||||
if s == 0:
|
||||
if (data_remain - i * self.block['file_block_size']) < 0:
|
||||
""" 避免生成越界帧 """
|
||||
continue
|
||||
seq_window[i] = 1
|
||||
self.block['index'] = seq_offset + i
|
||||
seq_frame_master[i] = bytearray(make_frame_modbus(self.block))
|
||||
self.tcp_socket.send(seq_frame_master[i])
|
||||
# 接收帧回复
|
||||
tmp = list(zip(range(len(seq_window)), seq_window))
|
||||
for i, s in tmp:
|
||||
if s == 1:
|
||||
seq_frame_slave[i] = self.tcp_socket.recv(8)
|
||||
# 接收到空数据, 对端已关闭连接
|
||||
if seq_frame_slave[i] == '':
|
||||
raise Exception("TCP closed.")
|
||||
result, seq_current, seq_hope = check_frame_modbus(seq_frame_slave[i], None)
|
||||
if seq_current < seq_offset:
|
||||
raise Exception("Error.")
|
||||
elif result:
|
||||
seq_window[seq_current - seq_offset] = 2
|
||||
data_remain -= self.block['file_block_size']
|
||||
if seq_hope is not None and seq_hope < seq_offset:
|
||||
print("Frame Index out of order.")
|
||||
seq_offset = seq_hope
|
||||
|
||||
# 移动发送窗口
|
||||
while seq_window[0] == 2:
|
||||
seq_window[0] = seq_window[1]
|
||||
seq_window[1] = seq_window[2]
|
||||
seq_window[2] = seq_window[3]
|
||||
seq_window[3] = 0
|
||||
seq_offset += 1
|
||||
|
||||
# 设置发送失败内容重发标志
|
||||
for i, s in enumerate(seq_window):
|
||||
if s == 1:
|
||||
seq_window[i] = 0
|
||||
|
||||
# 结束升级
|
||||
ret = 0
|
||||
self.block['step'] = 'end'
|
||||
frame_master = bytearray(make_frame_modbus(self.block))
|
||||
|
||||
while ret == 0:
|
||||
self.tcp_socket.send(frame_master)
|
||||
frame_slave = self.tcp_socket.recv(8)
|
||||
if frame_slave == '':
|
||||
raise Exception("TCP closed.")
|
||||
ret = check_frame_modbus(frame_slave[:18], self.block)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
""" 升级测试 """
|
||||
path_file = Path("F:\\Work\\FPGA\\Test\\Vivado\\test_update\\test_update.vitis\\upgrade_system\\Debug\\sd_card\\BOOT.dat")
|
||||
|
||||
dev_ep = EnergyRouter()
|
||||
|
||||
dev_ep.frame_read()
|
||||
|
||||
dev_ep.frame_update(path_file)
|
||||
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user