最近时常需要合并HEX文件,趁着端午节的闲暇做了一个合并工具,界面如下:
简单介绍一下,使用IntelHex对HEX文件进行读写和合并操作;数据地址冲突时报错并终止合并;右键 -> 文件段落,显示segments方便了解文件结构;HEX文件的Start Linear Address重写为None;使用dropEvent实现HEX文件的拖放;qss定制黑色界面。
代码如下:
# -*- coding: utf-8 -*-
import os
import sys
from intelhex import IntelHex
from PyQt5.QtCore import QSettings
from PyQt5.QtGui import QCursor, QFont, QFontDatabase, QIcon
from PyQt5.QtWidgets import QAction, QApplication, QFileDialog, QLabel, QMainWindow, QMenu, QMessageBox, QTableWidgetItem
from PyQt5.uic import loadUi
sys.path.append('..\\..\\MyStyleSheet')
from darkstyle import load_stylesheet_pyqt5
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
loadUi('.\\pyhexmerge.ui', self)
self.setWindowIcon(QIcon('.\\layer.ico'))
self.setWindowTitle(self.tr('HEX文件合并工具'))
self.statusBar().addPermanentWidget(QLabel('Version 2018.06.22 '))
self.tableWidget.setHorizontalHeaderLabels((self.tr('文件'), ))
self.setAcceptDrops(True)
# Menu
self.menu_about = QMenu()
self.actionAbout = QAction(QIcon('.\\about_64.png'), self.tr('关于'), self)
self.menu_about.addAction(self.actionAbout)
self.menu_segments = QMenu()
self.actionSegments = QAction(QIcon('.\\layer_64.png'), self.tr('文件段落'), self)
self.menu_segments.addAction(self.actionSegments)
# Restore
settings = QSettings('.\\settings.ini', QSettings.IniFormat)
if settings.contains('geometry'):
self.restoreGeometry(settings.value('geometry'))
# Connect
self.pushButton_add.clicked.connect(self._slot_add)
self.pushButton_remove.clicked.connect(self._slot_remove)
self.pushButton_merge.clicked.connect(self._slot_merge)
self.tableWidget.customContextMenuRequested.connect(self._slot_show_menu_segments)
self.customContextMenuRequested.connect(self._slot_show_menu_about)
self.actionSegments.triggered.connect(self._slot_action_segments)
self.actionAbout.triggered.connect(self._slot_action_about)
def closeEvent(self, event):
settings = QSettings('.\\settings.ini', QSettings.IniFormat)
settings.setValue('geometry', self.saveGeometry())
QMainWindow.closeEvent(self, event)
def dragEnterEvent(self, event):
if not event.mimeData().hasFormat('text/uri-list'):
return
else:
for urls in event.mimeData().urls():
if not os.path.splitext(urls.fileName())[1] == '.hex':
return
event.acceptProposedAction()
def dropEvent(self, event):
for urls in event.mimeData().urls():
file_path = urls.toLocalFile()
row_count = self.tableWidget.rowCount()
self.tableWidget.setRowCount(row_count + 1)
self.tableWidget.setItem(row_count, 0, QTableWidgetItem(file_path))
def _slot_add(self):
file_path = QFileDialog.getOpenFileName(self, self.tr('打开文件'), '.\\', 'Data files (*.hex)')[0]
if file_path:
row_count = self.tableWidget.rowCount()
self.tableWidget.setRowCount(row_count + 1)
self.tableWidget.setItem(row_count, 0, QTableWidgetItem(file_path))
def _slot_remove(self):
items = self.tableWidget.selectedItems()
for i in items:
self.tableWidget.removeRow(i.row())
def _slot_merge(self):
row_count = self.tableWidget.rowCount()
if row_count < 2:
self.statusbar.showMessage(self.tr('待合并文件少于2'))
return
last = IntelHex()
for x in range(row_count):
new = IntelHex(self.tableWidget.item(x, 0).data(0))
new.start_addr = None
try:
last.merge(new, overlap='error')
except Exception as e:
QMessageBox.warning(self, self.tr('错误信息'), str(e))
self.statusbar.showMessage(self.tr('合并失败'))
return
file_path = QFileDialog.getSaveFileName(self, self.tr('保存文件'), '.\\', 'Data files (*.hex)')[0]
if file_path:
last.write_hex_file(file_path)
self.statusbar.showMessage(self.tr('合并成功'))
def _slot_show_menu_segments(self):
if not self.tableWidget.currentItem() is None:
self.menu_segments.exec(QCursor.pos())
def _slot_show_menu_about(self):
self.menu_about.exec(QCursor.pos())
def _slot_action_segments(self):
tmp_str = ''
segments = IntelHex(self.tableWidget.currentItem().data(0)).segments()
for s in segments:
tmp_str += '(' + hex(s[0]) + ', ' + hex(s[1]) + ')\n'
QMessageBox.information(self, self.tr('文件段落'), tmp_str)
def _slot_action_about(self):
QMessageBox.about(self, self.tr('关于'), self.tr('意见反馈:\n')+'yy123xiang@163.com')
app = QApplication(sys.argv)
app.setStyleSheet(load_stylesheet_pyqt5())
if os.path.exists('.\\msyh.ttf'):
msyh = QFontDatabase.applicationFontFamilies(QFontDatabase.addApplicationFont('.\\msyh.ttf'))[0]
font = QFont(msyh, 9)
app.setFont(font)
w = MainWindow()
w.show()
sys.exit(app.exec_())
附上使用PyInstaller打包的可执行文件,资源地址。