-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
309 lines (305 loc) · 16.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import sys
import re
if hasattr(sys, 'frozen'):
os.environ['PATH'] = sys._MEIPASS + ";" + os.environ['PATH']
from PyQt5.QtWidgets import QWidget,QApplication,QFileDialog,QMessageBox
from mainWin import *
import time
import Message
import Signal
class Main(QWidget,Ui_MainWin):
def __init__(self):
super(Main, self).__init__()
self.setupUi(self)
self.lineEdit.setText(os.path.abspath(os.path.dirname(os.getcwd())) + '\配置.txt')
self.pushButton_openfile.clicked.connect(self.OpenDBC)
self.pushButton_startcreat.clicked.connect(self.StartCreat)
self.pushButton_filesave.clicked.connect(self.choiceDir)
self.checkBox_Evbus.stateChanged.connect(self.checkBox_Evbus_checked)
self.checkBox_Pbus.stateChanged.connect(self.checkBox_Pbus_checked)
self.comboBox_choseNode.currentIndexChanged[str].connect(self.Get_NodeName)
#self.timeCurrent = time.strftime('%Y-%m-%d',time.localtime(time.time()))
self.lineEdit_codesave_path.setText(os.path.abspath(os.path.dirname(os.getcwd())))
self.saveDir = self.lineEdit_codesave_path.text()
self.show()
#复选框选中处理函数
def checkBox_Evbus_checked(self):
if self.checkBox_Evbus.isChecked() == True:
self.busChl = 'Evbus'
self.checkBox_Pbus.setChecked(False)
def checkBox_Pbus_checked(self):
if self.checkBox_Pbus.isChecked() == True:
self.busChl = 'Pbus'
self.checkBox_Evbus.setChecked(False)
#打开需要读取的DBC文件
def OpenDBC(self):
self.fname = QFileDialog.getOpenFileName(self,'打开文件','./',('DBC文件(*.dbc)'))
if self.fname[0]:
self.lineEdit.setText(self.fname[0]) #记录文件位置
if 'DBC文件(*.dbc)'== self.fname[1]:
self.Get_signalInfo(self.lineEdit.text()) #获取dbc文件内容
self.comboBox_choseNode.addItems(self.nodeName_array)
#获取下拉框内当前选择节点的名称
def Get_NodeName(self,nodeName):
self.nodeName = nodeName
for msg_info in self.msg_array:
if nodeName == msg_info.msg_node:
msg_info.msg_dirc = 'Tx'
else:
msg_info.msg_dirc = 'Rx'
#选择生成代码存放目录
def choiceDir(self):
Dir_path = QFileDialog.getExistingDirectory(self,'请选择保存源代码路径','./')
if Dir_path:
self.lineEdit_codesave_path.setText(Dir_path)
self.saveDir = self.lineEdit_codesave_path.text()
#开始生成代码
def StartCreat(self):
#尝试打开选定路径文件,若是文件不存在则弹窗警告
try:
f_read = open(self.lineEdit.text(),'r',encoding='utf-8')
except FileNotFoundError:
QMessageBox.warning(self,'提示','并未找到所选路径配置文件,请检查路径')
return
try:
self.Creat_Rte_c()
self.Creat_ComCfg_h()
self.Creat_Rte_h()
self.Creat_ComCfg_c()
#os.system('start explorer ' + self.lineEdit_codesave_path.text().replace('/','\\'))
QMessageBox.information(self,'提示','代码生成完毕')
os.system('start explorer ' + self.lineEdit_codesave_path.text().replace('/','\\'))
except Exception as e:
QMessageBox.warning(self,'提示','代码生成失败:' + e.args[0])
#以下部分为Rte_Com_Can.c文件生成
def Creat_Rte_c(self):
#创建一个新的文件,将静态变量定义写入到文件开头
try:
with open(self.saveDir + '\Rte_Com_Can.c','w') as f_write:
for msg_Info in self.msg_array:
valdef_write_str = 'static TsRTECOMCAN_h_' + self.busChl + msg_Info.msg_dirc + msg_Info.msg_node + msg_Info.msg_id + '_MsgType'\
+ '\t' + 'SsRTECOMCAN_h_' + self.busChl + msg_Info.msg_dirc + msg_Info.msg_node + msg_Info.msg_id + '_Msg'\
+ '\t\t' + '= {0,};\n'
f_write.write(valdef_write_str)
except Exception:
pass
#变量定义完以后再进行函数定义
'''
try:
with open('.\demo.txt','r',encoding = 'utf-8') as f:
demo_str = f.read()
except Exception:
QMessageBox.warning(self,'警告','软件安装路径未找到模板文件,终止生成代码')
os.remove(self.saveDir + '\Rte_Com_Can.c')
raise Exception
'''
demo_str = '''/*------------------------------------------------------------------------------
| Function Name : GetRTECOMMCAN_($Channel$)($Dic$)($Node$)0x($ID$)_Msg / SetRTECOMMCAN_($Channel$)($Dic$)($Node$)0x($ID$)_Msg
| Called by :
| Preconditions :
| Input Parameters: pMsg : message info pointer
| Return Value :
| Description : Get / Set ($Channel$)($Dic$)($Node$)0x($ID$) message
| History :
| DATE AUTHOR Description
| ---------- -------------- ------------------------------------------------
| ($Date$) ($Author$)
------------------------------------------------------------------------------*/
void GetRTECOMMCAN_($Channel$)bus($Dic$)($Node$)0x($ID$)_Msg(const TsRTECOMMCAN_h_($Channel$)($Dic$)($Node$)0x($ID$)_MsgType **pMsg)
{
assert_param(NULL != pMsg);
*pMsg = &(SsRTECOMMCAN_h_($Channel$)($Dic$)($Node$)0x($ID$)_Msg);
}
void SetRTECOMMCAN_($Channel$)($Dic$)($Node$)0x($ID$)_Msg(const TsRTECOMMCAN_h_($Channel$)($Dic$)($Node$)0x($ID$)_MsgType *pMsg)
{
assert_param(NULL != pMsg);
SsRTECOMMCAN_h_($Channel$)($Dic$)($Node$)0x($ID$)_Msg = *pMsg;
}\n'''
f_write = open(self.saveDir + '\Rte_Com_Can.c','a')
for config in self.config_list:
fundef_write_str = demo_str.replace('($Channel$)',config[2]).replace('($Dic$)',config[3]).replace('($Node$)',config[1])\
.replace('($ID$)',config[0]).replace('($Date$)',self.timeCurrent).replace('($Author$)',self.lineEdit_authorName.text())
f_write.write(fundef_write_str)
f_write.close()
#以下函数生成Rte_Com_Can.h文件
def Creat_Rte_h(self):
Tpdef_str = '''typedef struct TsRTECOMMCAN_h_(Node)_0x(ID)_msgTypeTag
{
} TsRTECOMMCAN_h_(Chl)(Dic)(Node)0x(ID)_MsgType;\n'''
Fundec_str ='''extern void GetRTECOMMCAN_(Chl)(Dic)(Node)0x(ID)_Msg(const TsRTECOMMCAN_h_(Chl)(Dic)(Node)0x(ID)_MsgType **pMsg);
extern void SetRTECOMMCAN_(Chl)(Dic)(Node)0x(ID)_Msg(const TsRTECOMMCAN_h_(Chl)(Dic)(Node)0x(ID)_MsgType *pMsg);\n'''
with open(self.saveDir + '\Rte_Com_Can.h','w') as f:
for config in self.config_list:
write_str = Tpdef_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])
f.write(write_str)
with open(self.saveDir + '\Rte_Com_Can.h','a') as f:
for config in self.config_list:
write_str = Fundec_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])
f.write(write_str)
f.write('\n')
def Creat_ComCfg_c(self):
RsvFundef_str = '''/*******************************************************************************
| Function Name : MngCOMCFGCAN_RcvMsg_(Chl)(Dic)(Node)0x(ID)
| Called by :
| Preconditions :
| Input Parameters: pCanMsg : not used
| Return Value :
| Description : Receive CAN (Chl)(Dic)(Node)0x(ID) message from buffer
| ------------------------------------------------------------------------------
| History:
| DATE AUTHOR Description
| ---------- -------------- ------------------------------------------------
| ($Date$) ($Author$)
*******************************************************************************/
void MngCOMCFGCAN_RcvMsg_(Chl)(Dic)(Node)0x(ID)(const void *pCanMsg)
{
TsRTECOMMCAN_h_(Chl)(Dic)(Node)0x(ID)_MsgType LpCOMCFGCAN_h_(Chl)(Dic)(Node)0x(ID)_Msg;
// TODO: to be checked
SetRTECOMMCAN_(Chl)(Dic)(Node)0x(ID)_Msg(&LpCOMCFGCAN_h_(Chl)(Dic)(Node)0x(ID)_Msg);
}\n'''
TrsFundef_str = '''/*******************************************************************************
| Function Name : MngCOMCFGCAN_TrsMsg_(Chl)(Dic)(Node)0x(ID)
| Called by :
| Preconditions :
| Input Parameters: none
| Return Value : E_OK / E_NOT_OK
| Description : Send Transmit CAN (Chl)(Dic)(Node)0x(ID) message into buffer
| ------------------------------------------------------------------------------
| History:
| DATE AUTHOR Description
| ---------- -------------- ------------------------------------------------
| ($Date$) ($Author$)
*******************************************************************************/
Std_ReturnType MngCOMCFGCAN_TrsMsg_(Chl)(Dic)(Node)0x(ID)(void)
{
const TsRTECOMMCAN_h_(Chl)(Dic)(Node)0x(ID)_MsgType *LpCOMCFGCAN_h_(Chl)(Dic)(Node)0x(ID)_Msg = NULL;
GetRTECOMMCAN_(Chl)(Dic)(Node)0x(ID)_Msg(&LpCOMCFGCAN_h_(Chl)(Dic)(Node)0x(ID)_Msg);
return (E_OK);
}
'''
LostFundef_str="""void MngCOM_(Chl)RxLost_(Node)0x(ID)(void)
{
SetRTECOMMCAN_(Chl)(Dic)(Node)0x(ID)_Msg(&CsCOM_h_(Chl)Lost(Node)0x(ID)_Msg);
}
"""
PrecopyFundef_str='''vuint8 MngCOM_(Chl)RxPrecopy_(Node)0x(ID)(CanRxInfoStructPtr rxStruct)
{
MngCOM_ConfirmRx(CeCOM_e_(Chl)Rx(Node)0x(ID));
return (kCanCopyData);
}
'''
with open(self.saveDir + '\Com_Cfg_Can.c','w') as f:
#生成报文丢失回调函数默认值变量
for config in self.config_list:
if config[3] == 'Rx':
Lostvaldef_write_str = 'static TsRTECOMMCAN_h_' + config[2] + config[3] + config[1] + '0x' + config[0] + '_MsgType'\
+ '\t' + 'CsCOM_h_' + config[2] + "Lost" + config[1] + '0x' + config[0] + '_Msg'\
+ '\t\t' + '= {0,};\n'
f.write(Lostvaldef_write_str)
#生成主函数
for config in self.config_list:
if config[3] == 'Rx':
write_str = RsvFundef_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])\
.replace('($Date$)',self.timeCurrent).replace('($Author$)',self.lineEdit_authorName.text())
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
for config in self.config_list:
if config[3] == 'Tx':
write_str = TrsFundef_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])\
.replace('($Date$)',self.timeCurrent).replace('($Author$)',self.lineEdit_authorName.text())
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
#生成Lost函数
for config in self.config_list:
if config[3] == 'Rx':
write_str = LostFundef_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
#生成Precopy函数
for config in self.config_list:
if config[3] == 'Rx':
write_str = PrecopyFundef_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2].replace('Dic',config[3]))
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
for config in self.config_list:
if config[3] == 'Rx':
write_str = "CeCOM_e_ID_(Chl)Rx(Node)0x(ID),\n".replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2])
f.write(write_str)
def Creat_ComCfg_h(self):
RsvFundcl_str = 'extern void MngCOMCFGCAN_RcvMsg_(Chl)(Dic)(Node)0x(ID)(const void *pCanMsg);\n'
TrsFundcl_str = 'extern Std_ReturnType MngCOMCFGCAN_TrsMsg_(Chl)(Dic)(Node)0x(ID)(void);\n'
LostFundcl_str = 'extern void MngCOM_(Chl)RxLost_(Node)0x(ID)(void);\n'
PrecopyFundcl_str = 'extern vuint8 MngCOM_(Chl)RxPrecopy_(Node)0x(ID)(CanRxInfoStructPtr rxStruct);\n'
with open(self.saveDir + '\Com_Cfg_Can.h','w') as f:
#生成主函数声明代码
for config in self.config_list:
if config[3] == 'Rx':
write_str = RsvFundcl_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
for config in self.config_list:
if config[3] == 'Tx':
write_str = TrsFundcl_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2]).replace('(Dic)',config[3])
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
#生成Lost函数声明代码
for config in self.config_list:
if config[3] == 'Rx':
write_str = LostFundcl_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2])
f.write(write_str)
f.write('-----------------------------------------------------------------------------------------------------------\n')
#生成Precopy函数声明代码
for config in self.config_list:
if config[3] == 'Rx':
write_str = PrecopyFundcl_str.replace('(Node)',config[1]).replace('(ID)',config[0]).replace('(Chl)',config[2])
f.write(write_str)
def Get_signalInfo(self,dbc_path):
with open(dbc_path) as f:
dbc_str = f.readlines()
self.msg_array = []
self.nodeName_array = []
msg_array_index = 0
message_flag = False
oldtime = time.time()
for line in dbc_str:
if re.match('BO_ (\d+) (\S+): (\d+) (\S+)',line) != None:
message_flag = True
if True == message_flag:
if re.match('BO_ (\d+) (\S+): (\d+) (\S+)',line) != None:
message_groups = re.search('BO_ (\d+) (\S+): (\d+) (\S+)', line).groups()
msg_id = message_groups[0]
msg_name = message_groups[1]
msg_dlc = message_groups[2]
msg_node = message_groups[3]
message_ins = Message.Message(msg_id, msg_name, msg_dlc, msg_node)
if ('VECTOR__INDEPENDENT_SIG_MSG' == message_ins.msg_name) | ('Test_' in message_ins.msg_name):
message_flag = False
else:
self.msg_array.append(message_ins)
self.nodeName_array.append(message_ins.msg_node)
elif re.match(' SG_ (\S+) : \d+\|\d+@0\+ \((\d+\.?\d*),(-?\d+)\) \[(-?\d+\.?\d*)\|(-?\d+\.?\d*)\] "(\S+)"*',line) != None:
signal_groups = re.search(' SG_ (\S+) : \d+\|(\d+)@0\+ \((\d+\.?\d*),(-?\d+)\) \[(-?\d+\.?\d*)\|(-?\d+\.?\d*)\] "(\S+)"*', line).groups()
signal_name = signal_groups[0]
signal_dlc = signal_groups[1]
signal_factor = signal_groups[2]
signal_offset = signal_groups[3]
signal_minValue = signal_groups[4]
signal_maxValue = signal_groups[5]
signal_unit = signal_groups[6]
signal_ins = Signal.signal(signal_name,signal_dlc,signal_factor,signal_offset,signal_minValue,signal_maxValue,signal_unit)
self.msg_array[msg_array_index].__signal_add(signal_ins)
pass
elif '\n' == line:
msg_array_index = msg_array_index + 1
message_flag = False
if re.match('CM_ .*',line) != None:
self.nodeName_array = list(set(self.nodeName_array))
break
if __name__ == '__main__':
try:
app = QApplication(sys.argv)
MainFun = Main()
sys.exit(app.exec_())
except Exception as ex:
print(ex.args)