1.第一步安装 python-can 模块
2.第二部编写python脚本
2.1 头文件部分
#coding=utf-8
from Ui_out import Ui_Form
import sys
import sys, os
if hasattr(sys, 'frozen'):
os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
import can
import csv
import time as t
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
log_output = []
filename = ''
savefilename = ''
2.2 转换线程
class calcTheard(QThread):
_calcSignal = pyqtSignal(str)
def __init__(self):
super().__init__()
def run(self):
global log_output
global filename
# message包含以下属性
# arbitration_id:199
# bitrate_switch:False
# channel:0
# data:bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x04')
# dlc:8
# error_state_indicator:False
# id_type:False
# is_error_frame:False
# is_extended_id:False
# is_fd:False
# is_remote_frame:False
# timestamp:1617861021.10497
# str(message):
# 'Timestamp: 1617861021.104970 ID: 00c7 S DLC: 8 00 00 00 00 00 00 04 04 Channel: 0'
log_output = []
log = can.BLFReader(filename)
log = list(log)
time = t.strftime('%Y-%m-%d %H:%M:%S', t.localtime(log[0].timestamp))
log_output.append([time, 'Channel', 'CAN / CAN FD', 'Frame Type', 'CAN ID(HEX)', 'DLC', 'Data(HEX)'])
for msg in log:
time_secs = msg.timestamp - log[0].timestamp
time_secs = '%f' % (time_secs)
if msg.is_fd:
can_fd = 'CAN FD'
else:
can_fd = 'CAN'
if msg.bitrate_switch:
can_fd = can_fd + ': Bitrate Switch'
if msg.is_error_frame:
frame_type = 'Error'
elif msg.is_remote_frame:
frame_type = 'Remote'
else:
frame_type = 'Data'
if msg.is_extended_id:
can_id = '0x{:08X}'.format(msg.arbitration_id)
else:
can_id = '0x{:03X}'.format(msg.arbitration_id)
data = ''
for byte in msg.data:
data = data + '{:02X}'.format(byte) + ' '
log_output.append([time_secs, msg.channel, can_fd, frame_type, can_id, msg.dlc, data])
self._calcSignal.emit("")
2.3文件写入线程
class wirteThread(QThread):
_wirtesignal = pyqtSignal(str)
def __init__(self):
super().__init__()
def run(self):
global log_output
global savefilename
with open(savefilename,"w",newline='') as f:
writer = csv.writer(f, dialect='excel')
writer.writerows(log_output)
self._wirtesignal.emit("")
2.4主线程
class MyApp(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.CovertBtn.clicked.connect(self.covert)
self.openBtn.clicked.connect(self.openfile)
def defaultName(self):
self.CovertBtn.setText("转换")
def covert(self):
self.CovertBtn.setText("处理中。。。。")
self.CovertBtn.setEnabled(False)
self.cal = calcTheard()
self.cal._calcSignal.connect(self.writeOut)
self.cal.start()
def writeOut(self):
global savefilename
openfile,ok = QFileDialog.getSaveFileName(self,"Save CSV file","./","CSV (*.csv);;All Files (*)")
if(openfile):
savefilename = openfile
self.CovertBtn.setText("保存中 ....")
self.out = wirteThread()
self.out._wirtesignal.connect(self.finishconfig)
self.out.start()
else:
QMessageBox.critical(self, '提示', '未选择文件')
self.CovertBtn.setText("转 换")
self.CovertBtn.setEnabled(True)
#完成所有设置
def finishconfig(self):
QMessageBox.information(self, '提示', '完成转换')
self.CovertBtn.setText("转 换")
self.CovertBtn.setEnabled(True)
# 打开文件处理
def openfile(self):
print("print openfile")
global filename
global openfile
openfile,ftype = QFileDialog.getOpenFileName(self,"Open blf File","./","BLF(*.blf);;All Files(*)")
if openfile:
print(openfile)
self.lineEdit.setText(openfile)
filename = openfile
2.5 main 函数
if __name__ == "__main__":
codec = QTextCodec.codecForName("utf-8")
QTextCodec.setCodecForLocale(codec)
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
3.使用auto py to exe 进行打包py 文件(pip install auto-py-to-exe)导入json
{
"version": "auto-py-to-exe-configuration_v1",
"pyinstallerOptions": [
{
"optionDest": "noconfirm",
"value": true
},
{
"optionDest": "filenames",
"value": "C:/Users/lenovo/Desktop/blf\u8f6c\u6362/out.py"
},
{
"optionDest": "onefile",
"value": true
},
{
"optionDest": "console",
"value": false
},
{
"optionDest": "icon_file",
"value": "C:/Users/lenovo/Desktop/blf\u8f6c\u6362/out.ico"
},
{
"optionDest": "ascii",
"value": false
},
{
"optionDest": "clean_build",
"value": false
},
{
"optionDest": "strip",
"value": false
},
{
"optionDest": "noupx",
"value": false
},
{
"optionDest": "disable_windowed_traceback",
"value": false
},
{
"optionDest": "embed_manifest",
"value": true
},
{
"optionDest": "uac_admin",
"value": false
},
{
"optionDest": "uac_uiaccess",
"value": false
},
{
"optionDest": "win_private_assemblies",
"value": false
},
{
"optionDest": "win_no_prefer_redirects",
"value": false
},
{
"optionDest": "bootloader_ignore_signals",
"value": false
},
{
"optionDest": "argv_emulation",
"value": false
},
{
"optionDest": "datas",
"value": "C:/Users/lenovo/Desktop/blf\u8f6c\u6362/Ui_out.py;."
}
],
"nonPyinstallerOptions": {
"increaseRecursionLimit": true,
"manualArguments": ""
}
}