Qt開源作品4-網絡調試助手

## 一、前言

網絡調試助手和串口調試助手是一對的,用Qt開發項目與硬件通信絕大部分都是要麼串口通信(RS232 RS485 Modbus等),要麼就是網絡通信(TCP UDP HTTP等),所以一旦涉及到這兩方面,多多少少肯定離不開對應的調試助手協助進行程序的調試,尤其是硬件工程師,更加需要第三方的獨立的調試工具來驗證硬件工作是否正常,這可以大大避免扯皮的事情發生,既然第三方的工具測試下來沒有問題,收發數據都正常的話,那基本上可以斷定是軟件的問題,此時估計軟件工程師心裡慌得一逼啊!

基本功能:

  1. 1. 16進制數據和ASCII數據收發。
  2. 2. 定時器自動發送。
  3. 3. 自動從配置文件加載最後一次的界面設置。
  4. 4. 自動從配置文件加載數據發送下拉框的數據。可以將經常使用的數據填寫在send.txt中。
  5. 5. 可啟用設備模擬回覆,當收到某個數據時,模擬設備自動回覆數據。對應數據格式填寫在device.txt中。
  6. 6. 可對單個在線連接發送數據,也可勾選全部進行發送。
  7. 7. 支持多個客戶端連接併發。
  8. 8. 採用單線程。
  9. 9. 四種模式,tcp客戶端、tcp服務器、udp客戶端、udp服務器。

## 二、代碼思路

<code>第一步:實例化對應的類
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;
}
}
}
}
}/<code>

## 三、效果圖


Qt開源作品4-網絡調試助手


## 四、開源主頁

1. 國內站點:[https://gitee.com/feiyangqingyun/QWidgetDemo](https://gitee.com/feiyangqingyun/QWidgetDemo)

2. 國際站點:[https://github.com/feiyangqingyun/QWidgetDemo](https://github.com/feiyangqingyun/QWidgetDemo)


分享到:


相關文章: