-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhexcommander.cpp
More file actions
366 lines (309 loc) · 11.8 KB
/
hexcommander.cpp
File metadata and controls
366 lines (309 loc) · 11.8 KB
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
#include "hexcommander.h"
HexCommander::HexCommander(SerialPortActions *serial, QWidget *parent)
: QDialog(parent)
, ui(new Ui::DataTerminal)
{
ui->setupUi(this);
this->serial = serial;
// Set initial values
ui->klineProtocol->addItem("SSM");
ui->klineProtocol->addItem("iso14230");
ui->klineBaudRate->setText("4800");
ui->klineDataBits->addItem("7");
ui->klineDataBits->addItem("8");
ui->klineDataBits->addItem("9");
ui->klineDataBits->setCurrentIndex(1);
ui->klineStopBits->addItem("1");
ui->klineStopBits->addItem("2");
ui->klineParity->addItem("None");
ui->klineParity->addItem("Odd");
ui->klineParity->addItem("Even");
ui->klineTesterId->setText("F0");
ui->klineTargetId->setText("10");
ui->canProtocol->addItem("CAN");
ui->canProtocol->addItem("iso15765");
ui->canProtocol->setCurrentIndex(1);
ui->canBaudRate->setText("500000");
ui->canIdLength->addItem("11bit");
ui->canIdLength->addItem("29bit");
ui->canTesterId->setText("7E0");
ui->canTargetId->setText("7E8");
connect(ui->klineProtocol, SIGNAL(currentIndexChanged(int)), this, SLOT(protocolTypeChanged(int)));
connect(ui->klineListen, SIGNAL(clicked(bool)), this, SLOT(listenInterface()));
connect(ui->sendKlineMessage, SIGNAL(clicked(bool)), this, SLOT(sendToInterface()));
connect(ui->canProtocol, SIGNAL(currentIndexChanged(int)), this, SLOT(protocolTypeChanged(int)));
connect(ui->canListen, SIGNAL(clicked(bool)), this, SLOT(listenInterface()));
connect(ui->sendCanMessage, SIGNAL(clicked(bool)), this, SLOT(sendToInterface()));
this->show();
}
HexCommander::~HexCommander()
{
delete ui;
}
void HexCommander::protocolTypeChanged(int)
{
qDebug() << "Change protocol type";
QObject *obj = sender();
QString interfaceTypeName = obj->objectName();
QComboBox *protocolType = (QComboBox*)obj;
if (protocolType)
{
if (interfaceTypeName == "klineProtocol")
qDebug() << "K-Line protocol type changed to:" << protocolType->currentText();
else if (interfaceTypeName == "canProtocol")
qDebug() << "Can protocol type changed to:" << protocolType->currentText();
}
}
void HexCommander::listenInterface()
{
QObject *obj = sender();
QString interfaceTypeName = obj->objectName();
QPushButton *btn = (QPushButton*)obj;
if (btn->isChecked())
if (interfaceTypeName == "klineProtocol")
qDebug() << "Start listening K-Line interface";
else
qDebug() << "Start listening CANbus interface";
else
if (interfaceTypeName == "klineProtocol")
qDebug() << "Stop listening K-Line interface";
else
qDebug() << "Stop listening CANbus interface";
}
void HexCommander::sendToInterface()
{
bool serialOk = true;
bool ok = false;
bool readFile = false;
QObject *obj = sender();
QString interfaceTypeName = obj->objectName();
qDebug() << "Send data to interface";
QFile file;
QString msg;
QStringList msgList;
if (interfaceTypeName.startsWith("sendKlineMessage"))
msg = ui->klineMsgToSend->text();
else
msg = ui->canMsgToSend->text();
if (msg == "")
{
QMessageBox::warning(this, tr("Data terminal"), "Add message bytes or file to send");
return;
}
if (msg.at(0) != '.' && msg.at(0) != '/')
{
qDebug() << "Read message from lineedit";
msgList.append(msg);
}
else
{
qDebug() << "Read message from file";
readFile = true;
QFile file(msg);
if (!file.open(QIODevice::ReadOnly ))
{
QMessageBox::warning(this, tr("Data terminal"), "Unable to open datastream file '" + file.fileName() + "' for reading");
emit LOG_I("Unable to open datastream file '" + file.fileName() + "' for reading", true, true);
qDebug() << "Unable to open datastream file '" + file.fileName() + "' for reading";
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
msgList.append(line);
}
file.close();
}
if (interfaceTypeName.startsWith("sendKlineMessage"))
{
qDebug() << "Send message via K-Line";
qDebug() << "Checking protocol:" << ui->klineProtocol->currentText();
if (ui->klineProtocol->currentText() == "SSM")
serial->set_is_iso14230_connection(false);
else if (ui->klineProtocol->currentText() == "iso14230")
serial->set_is_iso14230_connection(true);
else
serialOk = false;
qDebug() << "Checking baudrate:" << ui->klineBaudRate->text();
if (ui->klineBaudRate->text().toDouble() >=300 && ui->klineBaudRate->text().toDouble() <=2000000)
serial->set_serial_port_baudrate(ui->klineBaudRate->text());
else
serialOk = false;
qDebug() << "Checking tester id:" << ui->klineTesterId->text();
serial->set_iso14230_tester_id(ui->klineTesterId->text().toUInt(&ok, 16));
qDebug() << "Checking target id:" << ui->klineTargetId->text();
serial->set_iso14230_target_id(ui->klineTargetId->text().toUInt(&ok, 16));
if (serialOk)
{
qDebug() << "All good, setting interface...";
serial->set_iso14230_startbyte(0x80);
serial->set_is_can_connection(false);
serial->set_is_iso15765_connection(false);
serial->set_is_29_bit_id(false);
qDebug() << "Opening interface...";
serial->open_serial_port();
}
QStringList msg;// = ui->klineMsgToSend->text().split(" ");
QByteArray output;
QByteArray received;
int rspDelay = 10;
qDebug() << "Append message to serial output" << msg;
for (int j = 0; j < msgList.length(); j++)
{
output.clear();
received.clear();
rspDelay = 10;
if (!msgList.at(j).startsWith("delay"))
{
msg = msgList.at(j).split(" ");
for (int i = 0; i < msg.length(); i++)
{
output.append(msg.at(i).toUInt(&ok, 16));
}
if (ui->klineProtocol->currentText() == "SSM")
output = add_ssm_header(output, ui->klineTesterId->text().toUInt(&ok, 16), ui->klineTargetId->text().toUInt(&ok, 16), false);
qDebug() << "Message to send:" << parse_message_to_hex(output);
}
if (msgList.length() > (j+1))
{
if (msgList.at(j+1).startsWith("delay"))
{
qDebug() << "Set delay";
delay(msgList.at(j+1).split(")").at(1).split("(").at(0).toUInt());
j++;
}
}
serial->write_serial_data_echo_check(output);
delay(rspDelay);
received = serial->read_serial_data(serial_read_short_timeout);
}
serial->reset_connection();
}
else if (interfaceTypeName.startsWith("sendCanMessage"))
{
qDebug() << "Send message via CAN / iso15765";
qDebug() << "Checking protocol:" << ui->canProtocol->currentText();
serial->set_is_can_connection(false);
serial->set_is_iso15765_connection(false);
if (ui->canProtocol->currentText() == "CAN")
serial->set_is_can_connection(true);
else if (ui->canProtocol->currentText() == "iso15765")
serial->set_is_iso15765_connection(true);
else
serialOk = false;
qDebug() << "Checking baudrate:" << ui->canBaudRate->text();
if (ui->canBaudRate->text().toDouble() >=300 && ui->canBaudRate->text().toDouble() <=2000000)
serial->set_can_speed(ui->canBaudRate->text());
else
serialOk = false;
qDebug() << "Checking CAN ID length:" << ui->canIdLength->currentText();
serial->set_is_29_bit_id(ui->canIdLength->currentIndex());
qDebug() << "Checking tester id:" << ui->canTesterId->text();
serial->set_iso15765_source_address(ui->canTesterId->text().toUInt(&ok, 16));
qDebug() << "Checking target id:" << ui->canTargetId->text();
serial->set_iso15765_destination_address(ui->canTargetId->text().toUInt(&ok, 16));
if (serialOk)
{
qDebug() << "All good, setting interface...";
qDebug() << "Opening interface...";
serial->open_serial_port();
}
QStringList msg;// = ui->canMsgToSend->text().split(" ");
QByteArray output;
QByteArray received;
int rspDelay = 100;
for (int j = 0; j < msgList.length(); j++)
{
output.clear();
received.clear();
rspDelay = 10;
if (!msgList.at(j).startsWith("delay"))
{
msg = msgList.at(j).split(" ");
if (ui->canProtocol->currentText() == "CAN")
{
if (msg.length() > 8)
{
qDebug() << "ERROR: CAN message too long (8 message bytes)";
QMessageBox::warning(this, tr("CAN message"), "ERROR: CAN message too long (use 4 ID bytes + 8 message bytes)");
}
qDebug() << "Append message to CAN output" << msg;
}
for (int i = 3; i >= 0; i--)
{
output.append(((ui->canTesterId->text().toUInt(&ok, 16) >> (i * 8)) & 0xff));
}
for (int i = 0; i < msg.length(); i++)
{
output.append(msg.at(i).toUInt(&ok, 16));
}
qDebug() << "Message to send:" << parse_message_to_hex(output);
}
if (msgList.length() > (j+1))
{
if (msgList.at(j+1).startsWith("delay"))
{
qDebug() << "Set delay";
rspDelay = msgList.at(j+1).split(")").at(1).split("(").at(0).toUInt();
j++;
}
}
serial->write_serial_data_echo_check(output);
delay(rspDelay);
received = serial->read_serial_data(serial_read_short_timeout);
}
serial->reset_connection();
}
}
/*
* Add SSM header to message
*
* @return parsed message
*/
QByteArray HexCommander::add_ssm_header(QByteArray output, uint8_t tester_id, uint8_t target_id, bool dec_0x100)
{
uint8_t length = output.length();
qDebug() << "Append SSM header for message:" << parse_message_to_hex(output) << "length:" << QString::number(length);
output.insert(0, (uint8_t)0x80);
output.insert(1, target_id & 0xFF);
output.insert(2, tester_id & 0xFF);
output.insert(3, length);
output.append(calculate_checksum(output, dec_0x100));
qDebug() << "Constructed SSM message:" << parse_message_to_hex(output);
return output;
}
/*
* Calculate SSM checksum to message
*
* @return 8-bit checksum
*/
uint8_t HexCommander::calculate_checksum(QByteArray output, bool dec_0x100)
{
uint8_t checksum = 0;
for (uint16_t i = 0; i < output.length(); i++)
checksum += (uint8_t)output.at(i);
if (dec_0x100)
checksum = (uint8_t) (0x100 - checksum);
return checksum;
}
/*
* Parse QByteArray to readable form
*
* @return parsed message
*/
QString HexCommander::parse_message_to_hex(QByteArray received)
{
QString msg;
for (int i = 0; i < received.length(); i++)
{
msg.append(QString("%1 ").arg((uint8_t)received.at(i),2,16,QLatin1Char('0')).toUtf8());
}
return msg;
}
void HexCommander::delay(int timeout)
{
QTime dieTime = QTime::currentTime().addMSecs(timeout);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 1);
}