(相关资料图)
上一篇文章已经打通了数据源之一的串口采集,这次要说的是网络采集,网络通信目前用的最多的是三种,TCP/UDP/HTTP,其中tcp通信又包括了客户端服务端两种,tcp通信才用了多次握手机制不丢包,但是耗费资源多而且需要建立连接。udp通信在大数据量或者网络不稳定的情况下,可能丢包,而且顺序无法保证,但是一个包的数据肯定是正确的,由于占用资源极少而且不需要建立连接,在很多场景中应用也蛮多,我个人用udp以来,也没发现过丢包的情况,可能数据量不够大或者是在局域网内的原因吧,反正用起来还是蛮爽的。http通信目前非常流行,尤其是和服务器之间做数据交互,基本上post请求然后返回一串json数据,解析对应的json数据即可。本次采用的TCP通信作为示例,其他两种可以自行拓展,也很简单的。
体验地址:https://gitee.com/feiyangqingyun/QUCSDK https://github.com/feiyangqingyun/qucsdk
void frmData::initServer(){ //实例化串口类,绑定信号槽 com = new QextSerialPort(QextSerialPort::EventDriven, this); connect(com, SIGNAL(readyRead()), this, SLOT(readDataCom())); //实例化网络通信客户端类,绑定信号槽 tcpClient = new QTcpSocket(this); connect(tcpClient, SIGNAL(readyRead()), this, SLOT(readDataClient())); //实例化网络通信服务端类,绑定信号槽 tcpSocket = NULL; tcpServer = new QTcpServer(this); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); //开启定时器读取数据库采集数据 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() == "监听") {#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("监听"); }}void frmData::readDataClient(){ QByteArray data = tcpClient->readAll(); if (data.length() <= 0) { return; } //默认取第一个字节解析,可以自行更改 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; } //默认取第一个字节解析,可以自行更改 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())); }}
【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】
点击这里:
关键词: