-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
259 lines (220 loc) · 9.21 KB
/
mainwindow.cpp
File metadata and controls
259 lines (220 loc) · 9.21 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "CANSettingDialog.h"
#include <PCANBasic.h>
#include <QTime>
#include <QKeyEvent>
PCANChannelComboboxHandle::PCANChannelComboboxHandle(QObject *parent)
: QObject(parent)
{
Q_ASSERT(qobject_cast<QComboBox*>(parent) != nullptr);
}
PCANChannelComboboxHandle::~PCANChannelComboboxHandle()
{
}
/**
* @brief PCAN Channel Combobox 클릭하여 Show 됐을 때 처리할 Handler 함수
* @param object
* @param event
* @return true
* @return false
*/
bool PCANChannelComboboxHandle::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress )
{
auto comboboxPtr = qobject_cast<QComboBox*>(object);
if ( comboboxPtr->isEnabled() ) {
comboboxPtr->clear();
addPCANChanneltoComboBox(comboboxPtr);
}
}
return false;
}
void PCANChannelComboboxHandle::addPCANChanneltoComboBox(QComboBox* pComboBox)
{
int nChannelCount = 0;
if ( CAN_GetValue(PCAN_NONEBUS, PCAN_ATTACHED_CHANNELS_COUNT, &nChannelCount, sizeof(nChannelCount)) == PCAN_ERROR_OK ) {
if ( nChannelCount > 0 ) {
TPCANChannelInformation* pChannels = new TPCANChannelInformation[nChannelCount];
if ( CAN_GetValue(PCAN_NONEBUS, PCAN_ATTACHED_CHANNELS, pChannels, nChannelCount * sizeof(TPCANChannelInformation)) == PCAN_ERROR_OK) {
for (int i=0; i<nChannelCount; i++ ) {
if ( pChannels[i].channel_condition == PCAN_CHANNEL_PCANVIEW ) {
int nUSBChannelID = 0;
if ( pChannels[i].channel_handle >= PCAN_USBBUS1 && pChannels[i].channel_handle <= PCAN_USBBUS8 ) {
nUSBChannelID = pChannels[i].channel_handle - 0x50U;
} else if ( pChannels[i].channel_handle >= PCAN_USBBUS9 && pChannels[i].channel_handle <= PCAN_USBBUS16 ) {
nUSBChannelID = pChannels[i].channel_handle - 0x509U;
} else {
continue;
} // PCAN USB Handle 값이 1~8 / 9~16이 달라서 채널 값만 빼기 위해 분기처리
QString sChannelName = QString::asprintf("%1 %2")
.arg(pChannels[i].device_name).arg(nUSBChannelID);
pComboBox->addItem(sChannelName, pChannels[i].channel_handle);
}
}
}
}
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, settingParam()
, comm(nullptr)
, readMsgQueue(nullptr)
, bIsRunning(false)
, cmdhistoryBuf(new CommandHistoryBuf(HISTORY_BUF_SIZE))
{
ui->setupUi(this);
setWindowTitle("CAN Chat Program");
initForm();
initConnect();
loadCANSettingParam();
}
MainWindow::~MainWindow()
{
bIsRunning = false;
delete ui;
delete cmdhistoryBuf;
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if ( event->modifiers() == Qt::ControlModifier ) {
if ( event->key() == Qt::Key_Return ) {
writeMessage();
} else if ( event->key() == Qt::Key_Up ) {
ui->textEdit->setPlainText(cmdhistoryBuf->getPreCommand());
} else if ( event->key() == Qt::Key_Down ) {
ui->textEdit->setPlainText(cmdhistoryBuf->getNextCommand());
}
}
}
void MainWindow::initForm()
{
ui->cbChannel->installEventFilter(new PCANChannelComboboxHandle(ui->cbChannel));
PCANChannelComboboxHandle::addPCANChanneltoComboBox(ui->cbChannel);
ui->gbSendCAN->setEnabled(false);
ui->CANStream->setEnabled(false);
}
void MainWindow::initConnect()
{
connect ( ui->btnSetting, &QPushButton::clicked, [&] {
CANSettingDialog* pDialog = new CANSettingDialog(this);
pDialog->setParam(settingParam);
pDialog->exec();
settingParam = pDialog->getParam();
delete pDialog;
writeSetting(Key::Bitrate, QString(settingParam.sBitrate));
writeSetting(Key::BlockSize, settingParam.BlockSize);
writeSetting(Key::STmin, settingParam.STmin);
writeSetting(Key::CANID, settingParam.CANID);
writeSetting(Key::FlowID, settingParam.FlowID);
writeSetting(Key::CANDL, settingParam.CANDL);
writeSetting(Key::AddressType, settingParam.addrStandard);
});
connect ( ui->btnOpen, &QPushButton::clicked, [&] {
if ( ui->btnOpen->text().toLower() == "open" ) {
comm = new PCANComm(this);
if ( comm->Open(static_cast<cantp_handle>(ui->cbChannel->currentData().toInt()), settingParam) ) {
readMsgQueue = new QQueue<cantp_msg>();
comm->setReadDataQueue(readMsgQueue);
ui->btnOpen->setText("close");
ui->gbSendCAN->setEnabled(true);
ui->CANStream->setEnabled(true);
ui->cbChannel->setEnabled(false);
ui->btnSetting->setEnabled(false);
bIsRunning = true;
readDataFuture = std::async(std::launch::async, [&] {
while(bIsRunning) {
comm->Read();
}
});
readQueueFuture = std::async(std::launch::async, [&] {
while(bIsRunning) {
while ( !readMsgQueue->isEmpty() ) {
auto rx_msg = readMsgQueue->dequeue();
QString sMsg = QTime::currentTime().toString("[hh:mm:ss.zzz] (ID : %1,").arg(QString::asprintf("0x%X", rx_msg.can_info.can_id));
if ( rx_msg.type == PCANTP_MSGTYPE_CANFD ) {
sMsg.append(QString(" %1) - %2")
.arg("CANFD")
.arg(QString(reinterpret_cast<char*>(rx_msg.msgdata.canfd->data))));
// CAN-FD로 받는 경우 데이터 뒤에 "\0" 데이터가 들어와서 따로 처리 필요 X
} else if ( rx_msg.type == PCANTP_MSGTYPE_ISOTP ) {
auto nSize = rx_msg.msgdata.any->length;
QString sData = QString(reinterpret_cast<char*>(rx_msg.msgdata.isotp->data));
sData.resize(nSize);
sMsg.append(QString(" %1) - %2")
.arg("ISOTP")
.arg(sData));
}
CANTP_MsgDataFree_2016(&rx_msg);
QMetaObject::invokeMethod(this, [&, sMsg] {
ui->CANStream->append(sMsg);
});
}
}
});
} else {
if ( comm != nullptr ) {
delete comm;
}
}
} else if ( ui->btnOpen->text().toLower() == "close" ) {
bIsRunning = false;
readQueueFuture.get();
readDataFuture.get();
if ( comm != nullptr ) {
comm->Close();
delete comm;
}
if ( readMsgQueue != nullptr ) { delete readMsgQueue; }
ui->btnOpen->setText("Open");
ui->gbSendCAN->setEnabled(false);
ui->CANStream->setEnabled(false);
ui->cbChannel->setEnabled(true);
ui->btnSetting->setEnabled(true);
}
});
connect ( ui->btnSend, &QPushButton::clicked, [&] {
writeMessage();
});
}
void MainWindow::loadCANSettingParam()
{
settingParam.sBitrate = getSetting(Key::Bitrate).toString();
settingParam.BlockSize = getSetting(Key::BlockSize).toInt();
settingParam.STmin = getSetting(Key::STmin).toInt();
settingParam.CANID = getSetting(Key::CANID).toInt();
settingParam.FlowID = getSetting(Key::FlowID).toInt();
settingParam.CANDL = getSetting(Key::CANDL).toInt();
settingParam.addrStandard = getSetting(Key::AddressType).toBool();
if ( getSetting(Key::CANType).toString() == "CANFD" ) {
ui->rbCANFD->setChecked(true);
} else if ( getSetting(Key::CANType).toString() == "ISOTP") {
ui->rbISOTP->setChecked(true);
} else {
ui->rbISOTP->setChecked(true);
}
}
void MainWindow::writeMessage()
{
if ( comm == nullptr || ui->textEdit->toPlainText().isEmpty() ) { return; }
comm->Write(ui->textEdit->toPlainText().toLocal8Bit().data() + 0x00,
ui->textEdit->toPlainText().toLocal8Bit().size() + 1,
ui->rbCANFD->isChecked());
// 데이터 뒤에 "\n" 추가
cmdhistoryBuf->insert( ui->textEdit->toPlainText() );
ui->textEdit->clear();
writeSetting(Key::CANType, ui->rbISOTP->isChecked() ? "ISOTP" : "CANFD" );
}
void MainWindow::writeSetting(const QString &sKey, const QVariant &qValue)
{
QSettings setting(saveFileName, QSettings::IniFormat);
setting.setValue(sKey, qValue);
}
QVariant MainWindow::getSetting(const QString &sKey)
{
QSettings setting(saveFileName, QSettings::IniFormat);
return setting.value(sKey);
}