python写网络调试助手_Qt开源作品4-网络调试助手
生活随笔
收集整理的這篇文章主要介紹了
python写网络调试助手_Qt开源作品4-网络调试助手
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
## 一、前言
網絡調試助手和串口調試助手是一對的,用Qt開發項目與硬件通信絕大部分都是要么串口通信(RS232 RS485 Modbus等),要么就是網絡通信(TCP UDP HTTP等),所以一旦涉及到這兩方面,多多少少肯定離不開對應的調試助手協助進行程序的調試,尤其是硬件工程師,更加需要第三方的獨立的調試工具來驗證硬件工作是否正常,這可以大大避免扯皮的事情發生,既然第三方的工具測試下來沒有問題,收發數據都正常的話,那基本上可以斷定是軟件的問題,此時估計軟件工程師心里慌得一逼啊!
基本功能:
## 二、代碼思路
第一步:實例化對應的類tcpSocket = new QTcpSocket(this);connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected()));connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(disconnected()));connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));tcpServer = new TcpServer(this);connect(tcpServer, SIGNAL(clientConnected(QString, int)), this, SLOT(clientConnected(QString, int)));connect(tcpServer, SIGNAL(clientDisconnected(QString, int)), this, SLOT(clientDisconnected(QString, int)));connect(tcpServer, SIGNAL(sendData(QString, int, QString)), this, SLOT(sendData(QString, int, QString)));connect(tcpServer, SIGNAL(receiveData(QString, int, QString)), this, SLOT(receiveData(QString, int, QString)));udpSocket = new QUdpSocket(this);connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readData()));第二步:收發數據void frmTcpClient::readData(){ QByteArray data = tcpSocket->readAll(); if (data.length() <= 0) { return; } QString buffer; if (App::HexReceiveTcpClient) { buffer = QUIHelper::byteArrayToHexStr(data); } else if (App::AsciiTcpClient) { buffer = QUIHelper::byteArrayToAsciiStr(data); } else { buffer = QString(data); } append(1, buffer); //自動回復數據,可以回復的數據是以;隔開,每行可以帶多個;所以這里不需要繼續判斷 if (App::DebugTcpClient) { int count = App::Keys.count(); for (int i = 0; i < count; i++) { if (App::Keys.at(i) == buffer) { sendData(App::Values.at(i)); break; } } }}void frmUdpClient::readData(){ QHostAddress host; quint16 port; QByteArray data; QString buffer; while (udpSocket->hasPendingDatagrams()) { data.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(data.data(), data.size(), &host, &port); if (App::HexReceiveUdpClient) { buffer = QUIHelper::byteArrayToHexStr(data); } else if (App::AsciiUdpClient) { buffer = QUIHelper::byteArrayToAsciiStr(data); } else { buffer = QString(data); } QString ip = host.toString(); ip = ip.replace("::ffff:", ""); if (ip.isEmpty()) { continue; } QString str = QString("[%1:%2] %3").arg(ip).arg(port).arg(buffer); append(1, str); if (App::DebugUdpClient) { int count = App::Keys.count(); for (int i = 0; i < count; i++) { if (App::Keys.at(i) == buffer) { sendData(ip, port, App::Values.at(i)); break; } } } }}## 三、效果圖
## 四、開源主頁
1. 國內站點:[https://gitee.com/feiyangqingyun/QWidgetDemo](https://gitee.com/feiyangqingyun/QWidgetDemo)
2. 國際站點:[https://github.com/feiyangqingyun/QWidgetDemo](https://github.com/feiyangqingyun/QWidgetDemo)
總結
以上是生活随笔為你收集整理的python写网络调试助手_Qt开源作品4-网络调试助手的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《永劫无间》双排英雄推荐
- 下一篇: 一款免费好用的电脑磁盘必备清理神器好用的