(相關(guān)資料圖)
上一篇文章已經(jīng)打通了數(shù)據(jù)源之一的串口采集,這次要說的是網(wǎng)絡(luò)采集,網(wǎng)絡(luò)通信目前用的最多的是三種,TCP/UDP/HTTP,其中tcp通信又包括了客戶端服務(wù)端兩種,tcp通信才用了多次握手機(jī)制不丟包,但是耗費(fèi)資源多而且需要建立連接。udp通信在大數(shù)據(jù)量或者網(wǎng)絡(luò)不穩(wěn)定的情況下,可能丟包,而且順序無法保證,但是一個(gè)包的數(shù)據(jù)肯定是正確的,由于占用資源極少而且不需要建立連接,在很多場景中應(yīng)用也蠻多,我個(gè)人用udp以來,也沒發(fā)現(xiàn)過丟包的情況,可能數(shù)據(jù)量不夠大或者是在局域網(wǎng)內(nèi)的原因吧,反正用起來還是蠻爽的。http通信目前非常流行,尤其是和服務(wù)器之間做數(shù)據(jù)交互,基本上post請求然后返回一串json數(shù)據(jù),解析對應(yīng)的json數(shù)據(jù)即可。本次采用的TCP通信作為示例,其他兩種可以自行拓展,也很簡單的。
體驗(yàn)地址:https://gitee.com/feiyangqingyun/QUCSDK https://github.com/feiyangqingyun/qucsdk
void frmData::initServer(){ //實(shí)例化串口類,綁定信號槽 com = new QextSerialPort(QextSerialPort::EventDriven, this); connect(com, SIGNAL(readyRead()), this, SLOT(readDataCom())); //實(shí)例化網(wǎng)絡(luò)通信客戶端類,綁定信號槽 tcpClient = new QTcpSocket(this); connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readDataClient())); //實(shí)例化網(wǎng)絡(luò)通信服務(wù)端類,綁定信號槽 tcpSocket = NULL; tcpServer = new QTcpServer(this); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); //開啟定時(shí)器讀取數(shù)據(jù)庫采集數(shù)據(jù) timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(readDataDb())); timer->setInterval(1000);}void frmData::on_btnOpenTcpClient_clicked(){ if (ui->btnOpenTcpClient->text() == "連接") { tcpClient->connectToHost(App::TcpServerIP, App::TcpServerPort); bool ok = tcpClient->waitForConnected(1000); if (ok) { setEnable(ui->btnOpenTcpClient, false); ui->btnOpenTcpClient->setText("斷開"); } } else { tcpClient->disconnectFromHost(); setEnable(ui->btnOpenTcpClient, true); ui->btnOpenTcpClient->setText("連接"); }}void frmData::on_btnOpenTcpServer_clicked(){ if (ui->btnOpenTcpServer->text() == "監(jiān)聽") {#if (QT_VERSION >QT_VERSION_CHECK(5,0,0)) bool ok = tcpServer->listen(QHostAddress::AnyIPv4, App::TcpListenPort);#else bool ok = tcpServer->listen(QHostAddress::Any, App::TcpListenPort);#endif if (ok) { setEnable(ui->btnOpenTcpServer, false); ui->btnOpenTcpServer->setText("停止"); } } else { if (tcpSocket != NULL) { tcpSocket->disconnectFromHost(); } tcpSocket = NULL; tcpServer->close(); setEnable(ui->btnOpenTcpServer, true); ui->btnOpenTcpServer->setText("監(jiān)聽"); }}void frmData::readDataClient(){ QByteArray data = tcpClient->readAll(); if (data.length() <= 0) { return; } //默認(rèn)取第一個(gè)字節(jié)解析,可以自行更改 quint8 value = data.at(0); ui->txtValue->setText(QString::number(value)); append(3, data.toHex());}void frmData::readDataServer(){ QByteArray data = tcpSocket->readAll(); if (data.length() <= 0) { return; } //默認(rèn)取第一個(gè)字節(jié)解析,可以自行更改 quint8 value = data.at(0); ui->txtValue->setText(QString::number(value)); append(3, data.toHex());}void frmData::newConnection(){ while(tcpServer->hasPendingConnections()) { if (tcpSocket != NULL) { tcpSocket->disconnectFromHost(); } tcpSocket = tcpServer->nextPendingConnection(); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readDataServer())); }}
【領(lǐng) QT開發(fā)教程 學(xué)習(xí)資料, 點(diǎn)擊下方鏈接莬費(fèi)領(lǐng)取↓↓ ,先碼住不迷路~】
點(diǎn)擊這里:
免責(zé)聲明:本文不構(gòu)成任何商業(yè)建議,投資有風(fēng)險(xiǎn),選擇需謹(jǐn)慎!本站發(fā)布的圖文一切為分享交流,傳播正能量,此文不保證數(shù)據(jù)的準(zhǔn)確性,內(nèi)容僅供參考
關(guān)鍵詞: