-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentframe.cpp
406 lines (399 loc) · 12.1 KB
/
sentframe.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "sentframe.h"
#include "widget.h"
sentframe::sentframe(QObject *parent) : QObject(parent)
{
// BDC 长消息起始帧
MSG_BDC_init.car_frame = (VCI_CAN_OBJ *) malloc(sizeof (VCI_CAN_OBJ));
// memset(BRM_init.car_frame, 0, sizeof(VCI_CAN_OBJ));
MSG_BDC_init.car_frame->SendType = gTxType;
MSG_BDC_init.car_frame->RemoteFlag = 0;
MSG_BDC_init.car_frame->DataLen = 8;
MSG_BDC_init.car_frame->Data[0] = 0x10;
MSG_BDC_init.car_frame->Data[1] = 0x1C;
MSG_BDC_init.car_frame->Data[2] = 0x00;
MSG_BDC_init.car_frame->Data[3] = 0x04;
MSG_BDC_init.car_frame->Data[4] = 0xFF;
MSG_BDC_init.car_frame->Data[5] = 0x00;
MSG_BDC_init.car_frame->Data[6] = 0x29;
MSG_BDC_init.car_frame->Data[7] = 0x00;
MSG_BDC_init.car_frame->ID = 0x1CEC56F4;
MSG_BDC_init.car_frame->ExternFlag = 1;
MSG_BDC_init.Long_sign = 0;
MSG_BDC_init.cycle_time = 250;
MSG_BDC_init.len = 1;
// BST frame
MSG_BST.car_frame = & Widget::_BST[0];
MSG_BST.Long_sign = 0;
MSG_BST.cycle_time = 10;
MSG_BST.len = 1;
// BDC frame
MSG_BDC.car_frame = & Widget::_BDC[0];
MSG_BDC.Long_sign = 1;
MSG_BDC.cycle_time = 0;
MSG_BDC.len = 4;
// BCPP frame
MSG_BCPP.car_frame = & Widget::_BCPP[0];
MSG_BCPP.Long_sign = 0;
MSG_BCPP.cycle_time = 500;
MSG_BCPP.len = 1;
// BCSP frame
MSG_BCSP.car_frame = & Widget::_BCSP[0];
MSG_BCSP.Long_sign = 0;
MSG_BCSP.cycle_time = 250;
MSG_BCSP.len = 1;
// BCL frame
MSG_BCL.car_frame = & Widget::_BCL[0];
MSG_BCL.Long_sign = 0;
MSG_BCL.cycle_time = 50;
MSG_BCL.len = 1;
// BCLP frame
MSG_BCLP.car_frame = & Widget::_BCLP[0];
MSG_BCLP.Long_sign = 0;
MSG_BCLP.cycle_time = 50;
MSG_BCLP.len = 1;
// BHM init frame
MSG_BHM.car_frame = & Widget::_BHM[0];
MSG_BHM.Long_sign = 0;
MSG_BHM.cycle_time = 250;
MSG_BHM.len = 1;
// BRO_00 frame
MSG_BRO_00.car_frame = (VCI_CAN_OBJ * ) malloc (sizeof (VCI_CAN_OBJ));
MSG_BRO_00.car_frame->SendType = gTxType;
MSG_BRO_00.car_frame->RemoteFlag = 0;
MSG_BRO_00.car_frame->DataLen = 1;
MSG_BRO_00.car_frame->Data[0] = 0x00;
MSG_BRO_00.car_frame->ID = 0x100956F4;
MSG_BRO_00.car_frame->ExternFlag = 1;
MSG_BRO_00.Long_sign = 0;
MSG_BRO_00.cycle_time = 250;
MSG_BRO_00.len = 1;
// BRO_AA frame
MSG_BRO_AA.car_frame = (VCI_CAN_OBJ * ) malloc(sizeof (VCI_CAN_OBJ));
MSG_BRO_AA.car_frame->SendType = gTxType;
MSG_BRO_AA.car_frame->RemoteFlag = 0;
MSG_BRO_AA.car_frame->DataLen = 1;
MSG_BRO_AA.car_frame->Data[0] = 0xAA;
MSG_BRO_AA.car_frame->ID = 0x100956F4;
MSG_BRO_AA.car_frame->ExternFlag = 1;
MSG_BRO_AA.Long_sign = 0;
MSG_BRO_AA.cycle_time = 250;
MSG_BRO_AA.len = 1;
// BRM 长消息起始帧
MSG_BRM_init.car_frame = (VCI_CAN_OBJ *) malloc(sizeof (VCI_CAN_OBJ));
MSG_BRM_init.car_frame->SendType = gTxType;
MSG_BRM_init.car_frame->RemoteFlag = 0;
MSG_BRM_init.car_frame->DataLen = 8;
MSG_BRM_init.car_frame->Data[0] = 0x10;
MSG_BRM_init.car_frame->Data[1] = 0x31;
MSG_BRM_init.car_frame->Data[2] = 0x00;
MSG_BRM_init.car_frame->Data[3] = 0x07;
MSG_BRM_init.car_frame->Data[4] = 0xFF;
MSG_BRM_init.car_frame->Data[5] = 0x00;
MSG_BRM_init.car_frame->Data[6] = 0x02;
MSG_BRM_init.car_frame->Data[7] = 0x00;
MSG_BRM_init.car_frame->ID = 0x1CEC56F4;
MSG_BRM_init.car_frame->ExternFlag = 1;
MSG_BRM_init.Long_sign = 0;
MSG_BRM_init.cycle_time = 250;
MSG_BRM_init.len = 1;
// BRM 长消息
MSG_BRM.car_frame = & Widget::_BRM[0];
MSG_BRM.Long_sign = 1;
MSG_BRM.cycle_time = 0;
MSG_BRM.len = 7;
// BCP 长消息起始帧
MSG_BCP_init.car_frame = (VCI_CAN_OBJ *) malloc(sizeof (VCI_CAN_OBJ));
MSG_BCP_init.car_frame->SendType = gTxType;
MSG_BCP_init.car_frame->RemoteFlag = 0;
MSG_BCP_init.car_frame->DataLen = 8;
MSG_BCP_init.car_frame->Data[0] = 0x10;
MSG_BCP_init.car_frame->Data[1] = 0x0D;
MSG_BCP_init.car_frame->Data[2] = 0x00;
MSG_BCP_init.car_frame->Data[3] = 0x02;
MSG_BCP_init.car_frame->Data[4] = 0xFF;
MSG_BCP_init.car_frame->Data[5] = 0x00;
MSG_BCP_init.car_frame->Data[6] = 0x06;
MSG_BCP_init.car_frame->Data[7] = 0x00;
MSG_BCP_init.car_frame->ID = 0x1CEC56F4;
MSG_BCP_init.car_frame->ExternFlag = 1;
MSG_BCP_init.Long_sign = 0;
MSG_BCP_init.cycle_time = 500;
MSG_BCP_init.len = 1;
// BCP 长消息
MSG_BCP.car_frame = & Widget::_BCP[0];
MSG_BCP.Long_sign = 1;
MSG_BCP.cycle_time = 0;
MSG_BCP.len = 2;
//BCS 长消息起始帧
MSG_BCS_init.car_frame = (VCI_CAN_OBJ *) malloc(sizeof (VCI_CAN_OBJ));
MSG_BCS_init.car_frame[0].SendType = gTxType;
MSG_BCS_init.car_frame[0].RemoteFlag = 0;
MSG_BCS_init.car_frame->DataLen = 8;
MSG_BCS_init.car_frame->Data[0] = 0x10;
MSG_BCS_init.car_frame->Data[1] = 0x09;
MSG_BCS_init.car_frame->Data[2] = 0x00;
MSG_BCS_init.car_frame->Data[3] = 0x02;
MSG_BCS_init.car_frame->Data[4] = 0xFF;
MSG_BCS_init.car_frame->Data[5] = 0x00;
MSG_BCS_init.car_frame->Data[6] = 0x11;
MSG_BCS_init.car_frame->Data[7] = 0x00;
MSG_BCS_init.car_frame->ID = 0x1CEC56F4;
MSG_BCS_init.car_frame->ExternFlag = 1;
MSG_BCS_init.Long_sign = 0;
MSG_BCS_init.cycle_time = 250;
MSG_BCS_init.len = 1;
// BCS 长消息
MSG_BCS.car_frame = & Widget::_BCS[0];
MSG_BCS.Long_sign = 1;
MSG_BCS.cycle_time = 0;
MSG_BCS.len = 2;
// BSM frame
MSG_BSM.car_frame = & Widget::_BSM[0];
MSG_BSM.Long_sign = 0;
MSG_BSM.cycle_time = 250;
MSG_BSM.len = 1;
// BSD frameon_checkBox1_10_stateChanged
MSG_BSD.car_frame = & Widget::_BSD[0];
MSG_BSD.Long_sign = 0;
MSG_BSD.cycle_time = 250;
MSG_BSD.len = 1;
// BEM frame
MSG_BEM.car_frame = & Widget::_BEM[0];
MSG_BEM.Long_sign = 0;
MSG_BEM.cycle_time = 250;
MSG_BEM.len = 1;
}
void sentframe::Initalize()
{
qDebug() << "TX_thread ID: " << QThread::currentThreadId();
_BaseTimer = new QTimer (this);
_AnBaseTimer = new QTimer (this);
connect(_BaseTimer, SIGNAL(timeout()), this, SLOT(Loop_Send_Msg()));
connect(_AnBaseTimer, SIGNAL(timeout()), this, SLOT(Trans_BSM()));
_BaseTimer->setTimerType(Qt::PreciseTimer);
_AnBaseTimer->setTimerType(Qt::PreciseTimer); // 定时器使用高精度类型
}
void sentframe::tx_thread(Action eve_act)
{
// qDebug() << "TX_thread ID: " << QThread::currentThreadId() << "execute action code is " << eve_act;
if (eve_act == N_A) // N_A switch state and keep can bus transmit
return;
if ((eve_act != BRM)&&(eve_act != BCS)&&(eve_act != BCP)&&(eve_act != BDC)) // 长消息帧发送时不打断原周期
{
_BaseTimer->stop();
_AnBaseTimer->stop();
}
switch (eve_act) {
case BHM:
tx_frame(MSG_BHM);
_BaseTimer->start(MSG_BHM.cycle_time);
break;
case BRM_INIT:
tx_frame(MSG_BRM_init);
_BaseTimer->start(MSG_BRM_init.cycle_time);
break;
case BRM:
tx_frame(MSG_BRM);
break;
case BCP_INIT:
tx_frame(MSG_BCP_init);
if (Widget::V2G_Mode_Flag)
{
msleep(10);
tx_frame(MSG_BCPP); // BCP and BCPP message transmit together
}
_BaseTimer->start(MSG_BCPP.cycle_time);
break;
case BCP:
tx_frame(MSG_BCP);
break;
case BRO_00:
tx_frame(MSG_BRO_00);
emit feedbackBRO_00();
_BaseTimer->start(MSG_BRO_00.cycle_time);
break;
case BSD:
tx_frame(MSG_BSD);
emit feedbackBST_BSD();
_BaseTimer->start(MSG_BSD.cycle_time);
break;
case BST:
tx_frame(MSG_BST);
emit feedbackBST_BSD();
_BaseTimer->start(MSG_BST.cycle_time);
break;
case BRO_AA:
tx_frame(MSG_BRO_AA);
_BaseTimer->start(MSG_BRO_AA.cycle_time);
break;
case BCL:
tx_frame(MSG_BCL);
_BaseTimer->start(MSG_BCL.cycle_time);
tx_frame(MSG_BSM);
if (Widget::V2G_Mode_Flag)
{
msleep(10);
tx_frame(MSG_BCSP);
}
tx_frame(MSG_BCS_init);
_AnBaseTimer->start(MSG_BSM.cycle_time);
break;
case BCLP:
tx_frame(MSG_BCLP);
_BaseTimer->start(MSG_BCLP.cycle_time);
tx_frame(MSG_BSM);
if (Widget::V2G_Mode_Flag)
{
msleep(10);
tx_frame(MSG_BCSP);
}
tx_frame(MSG_BCS_init);
_AnBaseTimer->start(MSG_BSM.cycle_time);
break;
case BCS_INIT:
tx_frame(MSG_BCS_init);
break;
case BCS:
tx_frame(MSG_BCS);
break;
case Busleep:
// 总线休眠, 原先设置500ms会关闭总线, 与故障注入功能相关
break;
case BDC:
tx_frame(MSG_BDC);
break;
case BDC_INIT:
tx_frame(MSG_BDC_init);
_BaseTimer->start(MSG_BDC_init.cycle_time);
break;
case BEM:
tx_frame(MSG_BEM);
_BaseTimer->start(MSG_BEM.cycle_time);
break;
default:
break;
}
}
void sentframe::Loop_Send_Msg()// Priodic message
{
switch (stateMachine.state) {
case V1:
tx_frame(MSG_BDC_init);
break;
case H1:
tx_frame(MSG_BHM);
break;
case H2:
tx_frame(MSG_BRM_init);
break;
case C1:
tx_frame(MSG_BCP_init); // BCP and BCPP message transmit together
if (Widget::V2G_Mode_Flag)
{
msleep(10);
tx_frame(MSG_BCPP);
}
break;
case P1:
if (Widget::TimeoutBCL_Flag)
tx_frame(MSG_BCL);
break;
case P2:
tx_frame(MSG_BCLP);
break;
case R0:
tx_frame(MSG_BRO_00);
break;
case R1:
tx_frame(MSG_BRO_AA);
break;
case R2:
tx_frame(MSG_BRO_AA);
break;
case E1:
tx_frame(MSG_BST);
break;
case S1:
tx_frame(MSG_BSD);
break;
case T1:
tx_frame(MSG_BEM);
break;
case T2:
tx_frame(MSG_BEM);
break;
case T3:
tx_frame(MSG_BEM);
break;
default:
break;
}
}
void sentframe::Trans_BSM() // another timer for BSM & BCS & BCSP
{
switch (stateMachine.state) {
case P1:
tx_frame(MSG_BSM);
msleep(10);
tx_frame(MSG_BCSP);
tx_frame(MSG_BCS_init);
break;
case P2:
tx_frame(MSG_BSM);
msleep(10);
tx_frame(MSG_BCSP);
tx_frame(MSG_BCS_init);
break;
default:
break;
}
}
void sentframe::tx_frame(CAN_Messages Msg)
{
// QTime startime = QTime::currentTime();
if (Widget::transFree)
return;
else {
Widget::transFree = true;
}
if (!Msg.Long_sign)
{
Auto_transmit(&Msg.car_frame[0]);
}
else {
for (uint j = 0; j < Msg.len; j++) {
Auto_transmit(&Msg.car_frame[j]);
QEventLoop loop;
QTimer::singleShot(10, Qt::PreciseTimer, &loop, SLOT(quit())); // 长消息报文帧间隔时间10ms, 高精度类型
loop.exec();
}
}
Widget::transFree = false;
// QTime endtime = QTime::currentTime();
// qDebug() << "This CAN frames spend time is " << startime.msecsTo(endtime) << "ms";
}
void sentframe::Auto_transmit(VCI_CAN_OBJ * vco)
{
for (uint i = 0; i < 4; i++) {
if ((gChMask & (1 << i)) == 0) {
continue;
}
m_mutex.lock(); // _BSM 全局变量互锁
while (!VCI_Transmit(gDevType, gDevIdx, i, vco, 1)) {
err++;
continue;
}
m_mutex.unlock();
QString Rece_log = QDateTime::currentDateTime().toString("hh:mm:ss.zzz ") + QString::asprintf("CAN TX successed: ID=0x%08x, Data=0x", vco->ID);
for (int ii = 0; ii < vco->DataLen; ii++) {
Rece_log = Rece_log + QString::asprintf("%02x", vco->Data[ii]);
}
qDebug() << Rece_log;// QDateTime::currentMSecsSinceEpoch()表示ms总计时
if (err == 256) // 2^8
{
qDebug() << "TX_transmit stop error : total errors times =" << err;
emit Shoot_Error(err);
emit finished();
}
}
}