这是接下来的制作,在这个车载显示屏中,加上UDP、TCP、蓝牙界面相关的代码以及跳转。接下来,会讲述,开发过程中的问题以及自己添加的修改,部分(源代码基于正点原子,接来下,就不在重复说明!!!)
添加TCP
TCP相关的知识,这里不再介绍,需要的自行去百度查阅相关资料,写这个系列饿初衷,不是用来教学,使用展示,后续需要则会进行专门的讲解,这边只会添加自己制作上遇到的问题点。
制作过程中,发现开发板与TCP\UDP测试软件无法正常收发,经过分析是由于TCP代码中写死IP地址,造成无法做服务端监听测试软件的地址或者做客户端连接测试软件的IP地址。分析出原因,但是Qt控件还未使用明白,不懂如何往控件里面手动添加进入需要的IP地址,这类问题就先放下,后续有时间再来修改完整。
相关代码:
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
| #include "video.h" #include <QFile> #include <QFileInfoList> #include <QDir> #include <QCoreApplication> Video::Video(QWidget *parent) : QMainWindow(parent) { this->resize(1030, 600); this->setMinimumSize(1030, 600); this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setGeometry(0, 0, 800, 480);
tcpServer = new QTcpServer(this); tcpSocket = new QTcpSocket(this);
pushButton[0] = new QPushButton(); pushButton[1] = new QPushButton(); pushButton[2] = new QPushButton(); pushButton[3] = new QPushButton(); pushButton[4] = new QPushButton();
hBoxLayout[0] = new QHBoxLayout(); hBoxLayout[1] = new QHBoxLayout(); hBoxLayout[2] = new QHBoxLayout(); hBoxLayout[3] = new QHBoxLayout();
hWidget[0] = new QWidget(); hWidget[1] = new QWidget(); hWidget[2] = new QWidget();
vWidget = new QWidget(); vBoxLayout = new QVBoxLayout();
label[0] = new QLabel(); label[1] = new QLabel();
lineEdit = new QLineEdit(); comboBox = new QComboBox(); spinBox = new QSpinBox(); textBrowser = new QTextBrowser();
label[0]->setText("监听IP地址:"); label[1]->setText("监听端口:"); label[0]->setStyleSheet("color:red;"); label[1]->setStyleSheet("color:red;");
label[0]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); label[1]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
spinBox->setRange(10000, 99999);
pushButton[0]->setText("开始监听"); pushButton[1]->setText("停止监听"); pushButton[2]->setText("清空文本"); pushButton[3]->setText("发送消息"); pushButton[4]->setText("back"); pushButton[1]->setEnabled(false);
lineEdit->setText("www.openedv.com正点原子论坛");
hBoxLayout[0]->addWidget(pushButton[0]); hBoxLayout[0]->addWidget(pushButton[1]); hBoxLayout[0]->addWidget(pushButton[2]);
hWidget[0]->setLayout(hBoxLayout[0]);
hBoxLayout[1]->addWidget(label[0]); hBoxLayout[1]->addWidget(comboBox); hBoxLayout[1]->addWidget(label[1]); hBoxLayout[1]->addWidget(spinBox);
hWidget[1]->setLayout(hBoxLayout[1]);
hBoxLayout[2]->addWidget(lineEdit); hBoxLayout[2]->addWidget(pushButton[3]); hBoxLayout[2]->addWidget(pushButton[4]); hWidget[2]->setLayout(hBoxLayout[2]);
vBoxLayout->addWidget(textBrowser); vBoxLayout->addWidget(hWidget[1]); vBoxLayout->addWidget(hWidget[0]); vBoxLayout->addWidget(hWidget[2]);
vWidget->setLayout(vBoxLayout);
setCentralWidget(vWidget);
getLocalHostIP();
connect(pushButton[0], SIGNAL(clicked()), this, SLOT(startListen())); connect(pushButton[1], SIGNAL(clicked()), this, SLOT(stopListen())); connect(pushButton[2], SIGNAL(clicked()), this, SLOT(clearTextBrowser())); connect(pushButton[3], SIGNAL(clicked()), this, SLOT(sendMessages())); connect(pushButton[4], SIGNAL(clicked()), this, SLOT(back())); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
}
Video::~Video() { stopListen(); }
int Video::add(int a, int b) { return a+b; }
void Video::clientConnected() { tcpSocket = tcpServer->nextPendingConnection(); QString ip = tcpSocket->peerAddress().toString(); quint16 port = tcpSocket->peerPort(); textBrowser->append("客户端已连接"); textBrowser->append("客户端ip地址:" + ip); textBrowser->append("客户端端口:" + QString::number(port));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(receiveMessages())); connect(tcpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChange(QAbstractSocket::SocketState))); }
void Video::getLocalHostIP() {
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface interface, list) {
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
foreach (QNetworkAddressEntry entry, entryList) { if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { comboBox->addItem(entry.ip().toString()); comboBox->addItem("192.168.101.95"); IPlist<<entry.ip(); } } }
}
void Video::startListen() { if (comboBox->currentIndex() != -1) { qDebug()<<"start listen"<<endl; tcpServer->listen(IPlist[comboBox->currentIndex()], spinBox->value());
pushButton[0]->setEnabled(false); pushButton[1]->setEnabled(true); comboBox->setEnabled(false); spinBox->setEnabled(false);
textBrowser->append("服务器IP地址:" + comboBox->currentText()); textBrowser->append("正在监听端口:" + spinBox->text()); } }
void Video::stopListen() { qDebug()<<"stop listen"<<endl; tcpServer->close();
if (tcpSocket->state() == tcpSocket->ConnectedState) tcpSocket->disconnectFromHost();
pushButton[1]->setEnabled(false); pushButton[0]->setEnabled(true); comboBox->setEnabled(true); spinBox->setEnabled(true);
textBrowser->append("已停止监听端口:" + spinBox->text()); }
void Video::clearTextBrowser() { textBrowser->clear(); }
void Video::receiveMessages() { QString messages = "客户端:" + tcpSocket->readAll(); textBrowser->append(messages); }
void Video::sendMessages() { if(NULL == tcpSocket) return;
if(tcpSocket->state() == tcpSocket->ConnectedState) { tcpSocket->write(lineEdit->text().toUtf8().data());
textBrowser->append("服务端:" + lineEdit->text()); } }
void Video::socketStateChange(QAbstractSocket::SocketState state) { switch (state) { case QAbstractSocket::UnconnectedState: textBrowser->append("scoket状态:UnconnectedState"); break; case QAbstractSocket::ConnectedState: textBrowser->append("scoket状态:ConnectedState"); break; case QAbstractSocket::ConnectingState: textBrowser->append("scoket状态:ConnectingState"); break; case QAbstractSocket::HostLookupState: textBrowser->append("scoket状态:HostLookupState"); break; case QAbstractSocket::ClosingState: textBrowser->append("scoket状态:ClosingState"); break; case QAbstractSocket::ListeningState: textBrowser->append("scoket状态:ListeningState"); break; case QAbstractSocket::BoundState: textBrowser->append("scoket状态:BoundState"); break; default: break; } }
void Video::back() { this->close(); stopListen(); AppMainView *appMainView = new AppMainView; appMainView->show(); }
|
效果图如下:
这里添加了一个返回的按键操作。
添加UDP
这里需要区分单播、广播、组播的问题,单播比较可以理解,对于的IP地址和端口就可收发。
在调试过程中,组播存在问题,组播需要加入,组播组,这边用TCP\UDP软件做讲解,可以在同一个局域网下,进行组播发送,但是由于开发板连接另一个路由器,造成不是同一个局域网类,造成239.0.0.0~239.255.255.255 这里不可以共用,只能做到单播。
UDP中只要加入组就可以通信,没有区分客户端、服务端,当加入目标组后,既可以进行通信。
广播:即为把IP地址转变为255.255.255.255,同一个网段下,只要有端口监听发出者的端口,即可接收到信息,这样也验证通过了。
UDP的问题也全部得到解决,相关的代码如下:
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
| #include "udp.h" #include <QFile> #include <QFileInfoList> #include <QDir> #include <QCoreApplication> Udp::Udp(QWidget *parent) : QMainWindow(parent) { this->resize(1030, 600); this->setMinimumSize(1030, 600); this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setGeometry(0, 0, 800, 480);
udpSocket = new QUdpSocket(this);
pushButton[0] = new QPushButton(); pushButton[1] = new QPushButton(); pushButton[2] = new QPushButton(); pushButton[3] = new QPushButton(); pushButton[4] = new QPushButton(); pushButton[5] = new QPushButton(); hBoxLayout[0] = new QHBoxLayout(); hBoxLayout[1] = new QHBoxLayout(); hBoxLayout[2] = new QHBoxLayout(); hBoxLayout[3] = new QHBoxLayout();
hWidget[0] = new QWidget(); hWidget[1] = new QWidget(); hWidget[2] = new QWidget();
vWidget = new QWidget(); vBoxLayout = new QVBoxLayout();
label[0] = new QLabel(); label[1] = new QLabel(); label[2] = new QLabel();
lineEdit = new QLineEdit(); comboBox = new QComboBox(); spinBox[0] = new QSpinBox(); spinBox[1] = new QSpinBox(); textBrowser = new QTextBrowser();
label[0]->setText("目标IP地址:"); label[1]->setText("绑定端口:"); label[2]->setText("目标端口:");
label[0]->setStyleSheet("color:red;"); label[1]->setStyleSheet("color:red;"); label[2]->setStyleSheet("color:red;"); label[0]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); label[1]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); label[2]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
spinBox[0]->setRange(10000, 99999); spinBox[1]->setRange(10000, 99999);
pushButton[0]->setText("绑定端口"); pushButton[1]->setText("解除绑定"); pushButton[2]->setText("清空文本"); pushButton[3]->setText("发送消息"); pushButton[4]->setText("广播消息"); pushButton[5]->setText("back");
pushButton[1]->setEnabled(false);
lineEdit->setText("你好!");
hBoxLayout[0]->addWidget(pushButton[0]); hBoxLayout[0]->addWidget(pushButton[1]); hBoxLayout[0]->addWidget(pushButton[2]);
hWidget[0]->setLayout(hBoxLayout[0]);
hBoxLayout[1]->addWidget(label[0]); hBoxLayout[1]->addWidget(comboBox); hBoxLayout[1]->addWidget(label[1]); hBoxLayout[1]->addWidget(spinBox[0]); hBoxLayout[1]->addWidget(label[2]); hBoxLayout[1]->addWidget(spinBox[1]);
hWidget[1]->setLayout(hBoxLayout[1]);
hBoxLayout[2]->addWidget(lineEdit); hBoxLayout[2]->addWidget(pushButton[3]); hBoxLayout[2]->addWidget(pushButton[4]); hBoxLayout[2]->addWidget(pushButton[5]); hWidget[2]->setLayout(hBoxLayout[2]);
vBoxLayout->addWidget(textBrowser); vBoxLayout->addWidget(hWidget[1]); vBoxLayout->addWidget(hWidget[0]); vBoxLayout->addWidget(hWidget[2]);
vWidget->setLayout(vBoxLayout);
setCentralWidget(vWidget);
getLocalHostIP();
connect(pushButton[0], SIGNAL(clicked()), this, SLOT(bindPort())); connect(pushButton[1], SIGNAL(clicked()), this, SLOT(unbindPort())); connect(pushButton[2], SIGNAL(clicked()), this, SLOT(clearTextBrowser())); connect(pushButton[3], SIGNAL(clicked()), this, SLOT(sendMessages())); connect(pushButton[4], SIGNAL(clicked()), this, SLOT(sendBroadcastMessages())); connect(pushButton[5], SIGNAL(clicked()), this, SLOT(back())); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(receiveMessages())); connect(udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChange(QAbstractSocket::SocketState)));
}
Udp::~Udp() {
}
void Udp::bindPort() { quint16 port = spinBox[0]->value();
if (udpSocket->state() != QAbstractSocket::UnconnectedState) udpSocket->close();
if (udpSocket->bind(port)) { textBrowser->append("已经成功绑定端口:" + QString::number(port));
pushButton[0]->setEnabled(false); pushButton[1]->setEnabled(true); spinBox[1]->setEnabled(false); } }
void Udp::unbindPort() { udpSocket->abort();
pushButton[0]->setEnabled(true); pushButton[1]->setEnabled(false); spinBox[1]->setEnabled(true); }
void Udp::getLocalHostIP() {
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface interface, list) {
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
foreach (QNetworkAddressEntry entry, entryList) { if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { comboBox->addItem(entry.ip().toString()); IPlist<<entry.ip(); } } } }
void Udp::clearTextBrowser() { textBrowser->clear(); }
void Udp::receiveMessages() { QHostAddress peerAddr; quint16 peerPort;
while (udpSocket->hasPendingDatagrams()) { QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size(), &peerAddr, &peerPort); QString str = datagram.data();
textBrowser->append("接收来自" + peerAddr.toString() + ":" + QString::number(peerPort) + str); } }
void Udp::sendMessages() { textBrowser->append("发送:" + lineEdit->text());
QByteArray data = lineEdit->text().toUtf8();
QHostAddress peerAddr = IPlist[comboBox->currentIndex()];
quint16 peerPort = spinBox[1]->value();
udpSocket->writeDatagram(data, peerAddr, peerPort); }
void Udp::sendBroadcastMessages() { textBrowser->append("发送:" + lineEdit->text());
QByteArray data = lineEdit->text().toUtf8();
QHostAddress peerAddr = QHostAddress::Broadcast;
quint16 peerPort = spinBox[1]->text().toInt();
udpSocket->writeDatagram(data, peerAddr, peerPort); }
void Udp::socketStateChange(QAbstractSocket::SocketState state) { switch (state) { case QAbstractSocket::UnconnectedState: textBrowser->append("scoket状态:UnconnectedState"); break; case QAbstractSocket::ConnectedState: textBrowser->append("scoket状态:ConnectedState"); break; case QAbstractSocket::ConnectingState: textBrowser->append("scoket状态:ConnectingState"); break; case QAbstractSocket::HostLookupState: textBrowser->append("scoket状态:HostLookupState"); break; case QAbstractSocket::ClosingState: textBrowser->append("scoket状态:ClosingState"); break; case QAbstractSocket::ListeningState: textBrowser->append("scoket状态:ListeningState"); break; case QAbstractSocket::BoundState: textBrowser->append("scoket状态:BoundState"); break; default: break; } } void Udp::back() { this->close(); AppMainView *appMainView = new AppMainView; appMainView->show(); }
|
这里也是修改了返回按钮。
添加蓝牙
蓝牙这里用到Linux开源的蓝牙的协议栈BlueZ,本人还没分析源码,只是移植了一下代码,修改了一点东西,这类后续进行补充。这类代码分类比较多,就不贴代码上来。有需要分析,可以去分析正点原子的代码。
收获总结
这次的添加,主要学会了TCP、UDP之前一直不明白这个是什么,然后接触了linux 网络编程部分明白了TCP\IP是个什么意思,同时也进入Llinx应用开发阶段的巩固,理解掌握原子操作这个概念。
下周QT将继续开发,应用编程进行跟进了解。