| 354 |
Kevin |
1 |
#include "SerialWidget.h"
|
|
|
2 |
|
|
|
3 |
SerialWidget::SerialWidget(QWidget *parent) : QWidget(parent)
|
|
|
4 |
{
|
|
|
5 |
// Serial connection UI
|
| 362 |
Kevin |
6 |
btnConnect = new QPushButton("&Connect");
|
|
|
7 |
btnRefresh = new QPushButton("&Refresh");
|
|
|
8 |
labelPort = new QLabel("Serial Port:");
|
|
|
9 |
labelSpeed = new QLabel("Baud Rate:");
|
|
|
10 |
labelDataBits = new QLabel("Data:");
|
|
|
11 |
labelStopBits = new QLabel("Stop:");
|
|
|
12 |
labelParity = new QLabel("Parity Bit:");
|
|
|
13 |
labelFlowControl = new QLabel("Flow Control:");
|
|
|
14 |
cboxPort = new QComboBox();
|
|
|
15 |
cboxPort->setMinimumWidth(80);
|
|
|
16 |
cboxSpeed = new QComboBox();
|
|
|
17 |
cboxSpeed->setEditable(true);
|
|
|
18 |
cboxSpeed->setValidator(new QIntValidator(0, 100000000, this));
|
|
|
19 |
cboxDataBits = new QComboBox();
|
|
|
20 |
cboxDataBits->setMaximumWidth(60);
|
|
|
21 |
cboxStopBits = new QComboBox();
|
|
|
22 |
cboxStopBits->setMaximumWidth(60);
|
|
|
23 |
cboxParity = new QComboBox();
|
|
|
24 |
cboxFlowControl = new QComboBox();
|
|
|
25 |
cboxFlowControl->setMinimumWidth(70);
|
| 354 |
Kevin |
26 |
|
|
|
27 |
QGridLayout *serialSettingsLayout = new QGridLayout();
|
| 362 |
Kevin |
28 |
serialSettingsLayout->addWidget(btnConnect, 0, 0);
|
|
|
29 |
serialSettingsLayout->addWidget(labelPort, 0, 1);
|
|
|
30 |
serialSettingsLayout->addWidget(cboxPort, 0, 2);
|
|
|
31 |
serialSettingsLayout->addWidget(labelDataBits, 0, 3);
|
|
|
32 |
serialSettingsLayout->addWidget(cboxDataBits, 0, 4);
|
|
|
33 |
serialSettingsLayout->addWidget(labelParity, 0, 5);
|
|
|
34 |
serialSettingsLayout->addWidget(cboxParity, 0, 6);
|
| 354 |
Kevin |
35 |
|
| 362 |
Kevin |
36 |
serialSettingsLayout->addWidget(btnRefresh, 1, 0);
|
|
|
37 |
serialSettingsLayout->addWidget(labelSpeed, 1, 1);
|
|
|
38 |
serialSettingsLayout->addWidget(cboxSpeed, 1, 2);
|
|
|
39 |
serialSettingsLayout->addWidget(labelStopBits, 1, 3);
|
|
|
40 |
serialSettingsLayout->addWidget(cboxStopBits, 1, 4);
|
|
|
41 |
serialSettingsLayout->addWidget(labelFlowControl, 1, 5);
|
|
|
42 |
serialSettingsLayout->addWidget(cboxFlowControl, 1, 6);
|
| 354 |
Kevin |
43 |
|
|
|
44 |
setLayout(serialSettingsLayout);
|
|
|
45 |
|
|
|
46 |
serialHelper = new SerialHelper();
|
|
|
47 |
serialThread = new QThread();
|
|
|
48 |
serialHelper->moveToThread(serialThread);
|
|
|
49 |
|
|
|
50 |
connect(serialHelper, SIGNAL(UpdateStatus(QString)), this, SIGNAL(UpdateStatus(QString)));
|
|
|
51 |
|
| 362 |
Kevin |
52 |
connect(this, SIGNAL(QueryParameters()), serialHelper, SLOT(QueryParameters()));
|
|
|
53 |
connect(serialHelper, SIGNAL(UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)),
|
|
|
54 |
this, SLOT(UpdateParameters(QStringList,QStringList,QStringList,QStringList,QStringList,QStringList)));
|
| 354 |
Kevin |
55 |
|
| 362 |
Kevin |
56 |
connect(this, SIGNAL(Connect(QString,QString,QString,QString,QString,QString)),
|
|
|
57 |
serialHelper, SLOT(Connect(QString,QString,QString,QString,QString,QString)));
|
|
|
58 |
connect(serialHelper, SIGNAL(Connected()), this, SIGNAL(Connected()));
|
|
|
59 |
connect(serialHelper, SIGNAL(Connected()), this, SLOT(LocalConnected()));
|
| 354 |
Kevin |
60 |
|
| 362 |
Kevin |
61 |
connect(this, SIGNAL(Disconnect()), serialHelper, SLOT(Disconnect()));
|
|
|
62 |
connect(serialHelper, SIGNAL(Disconnected()), this, SIGNAL(Disconnected()));
|
|
|
63 |
connect(serialHelper, SIGNAL(Disconnected()), this, SLOT(LocalDisconnected()));
|
| 354 |
Kevin |
64 |
|
| 362 |
Kevin |
65 |
connect(this, SIGNAL(TransmitByteArray(QByteArray)), serialHelper, SLOT(TransmitByteArray(QByteArray)));
|
| 354 |
Kevin |
66 |
|
| 362 |
Kevin |
67 |
connect(serialHelper, SIGNAL(ReceivedByte(QByteArray)), this, SIGNAL(ReceivedByte(QByteArray)));
|
| 354 |
Kevin |
68 |
|
|
|
69 |
connect(serialThread, SIGNAL(finished()), serialHelper, SLOT(deleteLater()));
|
|
|
70 |
serialThread->start();
|
|
|
71 |
|
| 362 |
Kevin |
72 |
connect(btnConnect, SIGNAL(clicked()), this, SLOT(ConnectToggleBtn()));
|
|
|
73 |
connect(btnRefresh, SIGNAL(clicked()), this, SIGNAL(QueryParameters()));
|
| 354 |
Kevin |
74 |
|
| 362 |
Kevin |
75 |
emit QueryParameters();
|
|
|
76 |
LocalDisconnected();
|
| 354 |
Kevin |
77 |
}
|
|
|
78 |
|
|
|
79 |
SerialWidget::~SerialWidget()
|
|
|
80 |
{
|
|
|
81 |
serialThread->quit();
|
|
|
82 |
}
|
|
|
83 |
|
| 362 |
Kevin |
84 |
void SerialWidget::UpdateParameters(QStringList ports, QStringList speeds, QStringList dataBits,
|
| 354 |
Kevin |
85 |
QStringList stopBits, QStringList parity, QStringList flowControl)
|
|
|
86 |
{
|
| 362 |
Kevin |
87 |
QString currPort = cboxPort->currentText();
|
|
|
88 |
cboxPort->clear();
|
|
|
89 |
cboxPort->addItems(ports);
|
| 354 |
Kevin |
90 |
if (currPort != "" && ports.contains(currPort))
|
| 362 |
Kevin |
91 |
cboxPort->setCurrentText(currPort);
|
| 354 |
Kevin |
92 |
|
| 362 |
Kevin |
93 |
QString currSpeed = cboxSpeed->currentText();
|
|
|
94 |
cboxSpeed->clear();
|
|
|
95 |
cboxSpeed->addItems(speeds);
|
|
|
96 |
if (currSpeed != "") cboxSpeed->setCurrentText(currSpeed);
|
| 354 |
Kevin |
97 |
|
| 362 |
Kevin |
98 |
int currData = cboxDataBits->currentIndex();
|
|
|
99 |
cboxDataBits->clear();
|
|
|
100 |
cboxDataBits->addItems(dataBits);
|
|
|
101 |
if (currData >= 0) cboxDataBits->setCurrentIndex(currData);
|
| 354 |
Kevin |
102 |
|
| 362 |
Kevin |
103 |
int currStop = cboxStopBits->currentIndex();
|
|
|
104 |
cboxStopBits->clear();
|
|
|
105 |
cboxStopBits->addItems(stopBits);
|
|
|
106 |
if (currStop >= 0) cboxStopBits->setCurrentIndex(currStop);
|
| 354 |
Kevin |
107 |
|
| 362 |
Kevin |
108 |
int currParity = cboxParity->currentIndex();
|
|
|
109 |
cboxParity->clear();
|
|
|
110 |
cboxParity->addItems(parity);
|
|
|
111 |
if (currParity >= 0) cboxParity->setCurrentIndex(currParity);
|
| 354 |
Kevin |
112 |
|
| 362 |
Kevin |
113 |
int currFlow = cboxFlowControl->currentIndex();
|
|
|
114 |
cboxFlowControl->clear();
|
|
|
115 |
cboxFlowControl->addItems(flowControl);
|
|
|
116 |
if (currFlow >= 0) cboxFlowControl->setCurrentIndex(currFlow);
|
| 354 |
Kevin |
117 |
}
|
|
|
118 |
|
| 362 |
Kevin |
119 |
void SerialWidget::LocalConnected()
|
| 354 |
Kevin |
120 |
{
|
| 362 |
Kevin |
121 |
btnConnect->setText("&Disconnect");
|
| 354 |
Kevin |
122 |
// cboxSerialPort->setEnabled(false);
|
|
|
123 |
// cboxSerialSpeed->setEnabled(false);
|
|
|
124 |
// cboxSerialDataBits->setEnabled(false);
|
|
|
125 |
// cboxSerialStopBits->setEnabled(false);
|
|
|
126 |
// cboxSerialParity->setEnabled(false);
|
|
|
127 |
// cboxSerialFlowControl->setEnabled(false);
|
|
|
128 |
// btnSerialRefresh->setEnabled(false);
|
| 362 |
Kevin |
129 |
btnRefresh->hide();
|
|
|
130 |
labelPort->hide();
|
|
|
131 |
labelSpeed->hide();
|
|
|
132 |
labelDataBits->hide();
|
|
|
133 |
labelStopBits->hide();
|
|
|
134 |
labelParity->hide();
|
|
|
135 |
labelFlowControl->hide();
|
|
|
136 |
cboxPort->hide();
|
|
|
137 |
cboxSpeed->hide();
|
|
|
138 |
cboxDataBits->hide();
|
|
|
139 |
cboxStopBits->hide();
|
|
|
140 |
cboxParity->hide();
|
|
|
141 |
cboxFlowControl->hide();
|
| 354 |
Kevin |
142 |
}
|
|
|
143 |
|
| 362 |
Kevin |
144 |
void SerialWidget::LocalDisconnected()
|
| 354 |
Kevin |
145 |
{
|
| 362 |
Kevin |
146 |
btnConnect->setText("&Connect");
|
| 354 |
Kevin |
147 |
// cboxSerialPort->setEnabled(true);
|
|
|
148 |
// cboxSerialSpeed->setEnabled(true);
|
|
|
149 |
// cboxSerialDataBits->setEnabled(true);
|
|
|
150 |
// cboxSerialStopBits->setEnabled(true);
|
|
|
151 |
// cboxSerialParity->setEnabled(true);
|
|
|
152 |
// cboxSerialFlowControl->setEnabled(true);
|
|
|
153 |
// btnSerialRefresh->setEnabled(true);
|
| 362 |
Kevin |
154 |
btnRefresh->show();
|
|
|
155 |
labelPort->show();
|
|
|
156 |
labelSpeed->show();
|
|
|
157 |
labelDataBits->show();
|
|
|
158 |
labelStopBits->show();
|
|
|
159 |
labelParity->show();
|
|
|
160 |
labelFlowControl->show();
|
|
|
161 |
cboxPort->show();
|
|
|
162 |
cboxSpeed->show();
|
|
|
163 |
cboxDataBits->show();
|
|
|
164 |
cboxStopBits->show();
|
|
|
165 |
cboxParity->show();
|
|
|
166 |
cboxFlowControl->show();
|
| 354 |
Kevin |
167 |
}
|
|
|
168 |
|
| 362 |
Kevin |
169 |
void SerialWidget::ConnectToggleBtn()
|
| 354 |
Kevin |
170 |
{
|
|
|
171 |
if (serialHelper->connected) {
|
| 362 |
Kevin |
172 |
emit Disconnect();
|
| 354 |
Kevin |
173 |
} else {
|
| 362 |
Kevin |
174 |
if (cboxPort->currentText() != "" && cboxSpeed->currentText() != "") {
|
|
|
175 |
emit Connect(cboxPort->currentText(), cboxSpeed->currentText(), cboxDataBits->currentText(),
|
|
|
176 |
cboxStopBits->currentText(), cboxParity->currentText(), cboxFlowControl->currentText());
|
| 354 |
Kevin |
177 |
}
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
//void SerialController::Serial_ProcessIncomingData()
|
|
|
182 |
//{
|
|
|
183 |
// char tmpBuffer[SERIAL_BUFFER_SIZE];
|
|
|
184 |
// int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
|
|
|
185 |
// while (len > 0) {
|
|
|
186 |
// for (int i = 0; i < len; i++) {
|
|
|
187 |
// // Save received data into local buffer
|
|
|
188 |
// bufferIn[bufferInIndex] = tmpBuffer[i];
|
|
|
189 |
|
|
|
190 |
// // If newline char is received, end current string
|
|
|
191 |
// if (tmpBuffer[i] == '\n') {
|
|
|
192 |
// if (bufferInOverflow)
|
|
|
193 |
// currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
|
|
|
194 |
// else
|
|
|
195 |
// currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
|
|
|
196 |
// bufferInOverflow = false;
|
|
|
197 |
// bufferInIndex = 0;
|
|
|
198 |
// emit Serial_ReceivedString(currString);
|
|
|
199 |
// } else {
|
|
|
200 |
// bufferInIndex++;
|
|
|
201 |
// }
|
|
|
202 |
|
|
|
203 |
// // If received string is larger than our serial buffer, append to previous data
|
|
|
204 |
// if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
|
|
|
205 |
// bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
|
|
|
206 |
// if (bufferInOverflow)
|
|
|
207 |
// currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
|
|
|
208 |
// else
|
|
|
209 |
// currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
|
|
|
210 |
// bufferInOverflow = true;
|
|
|
211 |
// bufferInIndex = 0;
|
|
|
212 |
// }
|
|
|
213 |
// }
|
|
|
214 |
// // Check if there is more data to be read from the serial port
|
|
|
215 |
// len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
|
|
|
216 |
// }
|
|
|
217 |
//}
|