# -*- coding: utf-8 -*-
import sys
import os
import time
from tkinter import messagebox
from intelhex import IntelHex
from PyQt5 import QtWidgets,QtGui
from PyQt5.QtWidgets import *
from Ui_HexFileEdit import Ui_HexFileEditer
# Must be in this place, after setting path, to not need to install
import qdarkstyle
from qdarkstyle.dark.palette import DarkPalette
#from qdarkstyle.light.palette import LightPalette
ORIG_HEX_FILE_PATH_STR = 0
ORIG_HEX_FILE = IntelHex()
#***********************************************************
# 自定义功能函数
#***********************************************************
# judge is hex char or not
def isHexChar(char):
if char in '0123456789ABCDEFabcdef':
return True
else:
return False
#***********************************************************
#继承PyQT5的类
#***********************************************************
class PyQt5_HexEditer(QtWidgets.QWidget, Ui_HexFileEditer):
def __init__(self):
super(PyQt5_HexEditer, self).__init__()
self.setupUi(self)
self.init()
# 初始化函数
def init(self):
#加载按键
self.pb_load_file.clicked.connect(self.openload_hexfile)
#清空按键
self.pb_file_clear.clicked.connect(self.clear_hexfile)
#修改文件
self.pb_start_edit.clicked.connect(self.edit_hexfile)
# 功能按键函数
# 加载hex文件
def openload_hexfile(self):
global ORIG_HEX_FILE
global ORIG_HEX_FILE_PATH_STR
file_path = QFileDialog.getOpenFileName(self, "打开文件", "/", "*.hex")
try:
ORIG_HEX_FILE_PATH_STR = str(file_path[0])
ORIG_HEX_FILE = IntelHex(ORIG_HEX_FILE_PATH_STR)
#显示文件路径文件名
self.le_hexfile_path.clear() #清空当前文本
self.le_hexfile_path.setText(ORIG_HEX_FILE_PATH_STR)
self.pb_start_edit.setEnabled(True) #功能键使能
except:
pass
# 清空信息
def clear_hexfile(self):
self.le_hexfile_path.clear() #清空当前文本
self.te_edit_text.clear() #清空当前文本
self.pb_start_edit.setEnabled(False) #功能键不使能
# 修改文件
def edit_hexfile(self):
global ORIG_HEX_FILE
global ORIG_HEX_FILE_PATH_STR
hexfile_edit_setaddr = int(self.le_edit_addr.text(),base=16)
hexfile_edit_setsize = int(self.le_edit_size.text(),base=10)
# HEX模式
if self.cb_hex_mode.isChecked():
#删除所有非十六进制的字符
input_s = ''
text_tmp = self.te_edit_text.toPlainText()
text_tmp = text_tmp.replace('0x','')
text_tmp = text_tmp.replace('0X','')
for char in text_tmp:
if isHexChar(char):
input_s += char
input_s_len = len(input_s)
if input_s_len % 2:
QMessageBox.critical(self, "输入内容长度错误", "长度为奇数,请输入正确长度的内容!")
elif input_s_len == 0:
origbinfile_path_str = ORIG_HEX_FILE_PATH_STR[:ORIG_HEX_FILE_PATH_STR.find(".")] + ".bin"
ORIG_HEX_FILE.tobinfile(origbinfile_path_str) #save as binfile
QMessageBox.information(self, "转换成功", "未修改HEX文件仅转换为bin文件!")
else:
hexbyte_list = []
len_index = 0
while len_index < input_s_len:
byte_temp = int(input_s[len_index:len_index+2],base=16)
hexbyte_list.append(byte_temp)
len_index = len_index + 2
hexbyte_list_len = len(hexbyte_list)
if hexbyte_list_len > hexfile_edit_setsize:
QMessageBox.critical(self, "输入内容长度错误", "长度过长,请输入正确长度的内容!")
else:
current_addr = hexfile_edit_setaddr
for i in range(hexbyte_list_len):
ORIG_HEX_FILE[current_addr+i] = hexbyte_list[i]
diff_size = hexfile_edit_setsize - hexbyte_list_len
if diff_size > 0:
current_addr = hexfile_edit_setaddr + hexbyte_list_len
for i in range(diff_size):
ORIG_HEX_FILE[current_addr+i] = 0xFF
#save as new hexfile
newhexfile_path_str = ORIG_HEX_FILE_PATH_STR[:ORIG_HEX_FILE_PATH_STR.find(".")] + "_Fota.hex"
ORIG_HEX_FILE.tofile(newhexfile_path_str, format='hex')
#save as new binfile
newbinfile_path_str = newhexfile_path_str[:newhexfile_path_str.find(".")] + ".bin"
ORIG_HEX_FILE.tobinfile(newbinfile_path_str)
QMessageBox.information(self, "修改成功", "已成功修改HEX文件并另存,同时生成bin文件!")
# ASCII码模式
else:
hexfile_edit_context = self.te_edit_text.toPlainText()
hexfile_edit_context_len = len(hexfile_edit_context)
if hexfile_edit_context_len > hexfile_edit_setsize:
QMessageBox.critical(self, "输入内容长度错误", "长度过长,请输入正确长度的内容!")
elif hexfile_edit_context_len == 0:
origbinfile_path_str = ORIG_HEX_FILE_PATH_STR[:ORIG_HEX_FILE_PATH_STR.find(".")] + ".bin"
ORIG_HEX_FILE.tobinfile(origbinfile_path_str) #save as binfile
QMessageBox.information(self, "转换成功", "未修改HEX文件仅转换为bin文件!")
else:
ORIG_HEX_FILE.putsz(hexfile_edit_setaddr,hexfile_edit_context)
diff_size = hexfile_edit_setsize - hexfile_edit_context_len
if diff_size > 0:
current_addr = hexfile_edit_setaddr + hexfile_edit_context_len
for i in range(diff_size):
ORIG_HEX_FILE[current_addr+i] = 0xFF
#save as new hexfile
newhexfile_path_str = ORIG_HEX_FILE_PATH_STR[:ORIG_HEX_FILE_PATH_STR.find(".")] + "_Fota.hex"
ORIG_HEX_FILE.tofile(newhexfile_path_str, format='hex')
#save as new binfile
newbinfile_path_str = newhexfile_path_str[:newhexfile_path_str.find(".")] + ".bin"
ORIG_HEX_FILE.tobinfile(newbinfile_path_str)
QMessageBox.information(self, "修改成功", "已成功修改HEX文件并另存,同时生成bin文件!")
#***********************************************************
#主函数
#***********************************************************
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
MyQt_Windows = PyQt5_HexEditer()
'''
# 使用qdarkstyle库修改样式
# palette=DarkPalette
# palette=LightPalette
'''
style = qdarkstyle.load_stylesheet(palette=DarkPalette)
app.setStyleSheet(style)
MyQt_Windows.show()
sys.exit(app.exec_())
评论1