积累更新

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

@@ -1,6 +1,7 @@
import struct
from crc import Calculator, Crc16
from tools.ByteConv import trans_list_to_str, trans_str_to_list, display_hex
from .. import tools
modbus_map = {
# 1 - Hex
@@ -332,14 +333,14 @@ def display_data(address: int, data: bytes, modbus_map: dict=modbus_map) -> dict
data_label, data_len = "未知数据", 1
if idx not in modbus_map.keys():
item = convert_regs_to_int(data, 1, signed=False)
item = display_hex(item, 4)
item = tools.ByteConv.display_hex(item, 4)
else:
current_map = modbus_map[idx]
data_label = current_map[0]
if current_map[1] == 1:
""" Hex字符表示 """
item = convert_regs_to_int(data, 1, signed=False)
item = display_hex(item, 4)
item = tools.ByteConv.display_hex(item, 4)
elif current_map[1] == 2:
""" 16位数值表示 """
item = convert_regs_to_int(data, 1)
@@ -359,12 +360,12 @@ def display_data(address: int, data: bytes, modbus_map: dict=modbus_map) -> dict
item = item.decode()
except Exception as ex:
item_len = sum([any(item[i:]) for i in range(len(item))])
item = trans_list_to_str(item[:item_len])
item = tools.ByteConv.trans_list_to_str(item[:item_len])
elif current_map[1] == 5:
""" 载波地址表示 """
data_len = current_map[2]
item = swapping_words(data, data_len)
item = trans_list_to_str(item)
item = tools.ByteConv.trans_list_to_str(item)
elif current_map[1] == 6:
""" 浮点数值表示 """
data_len = 2
@@ -388,7 +389,7 @@ def print_display(output_data: dict):
for key, value in output_data.items():
label = value[0]
data = "-".join(map(str, value[1])) if type(value) == list else value[1]
print(f"{display_hex(key, 4)}: {data:<{data_len_max}} {label:<{label_len_max}}")
print(f"{tools.ByteConv.display_hex(key, 4)}: {data:<{data_len_max}} {label:<{label_len_max}}")