查看完整版本: [-- Qt开源作品4-网络调试助手 --]

QTCN开发网 -> Qt 作品展 -> Qt开源作品4-网络调试助手 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

liudianwu 2020-04-28 10:17

Qt开源作品4-网络调试助手

## 一、前言
网络调试助手和串口调试助手是一对的,用Qt开发项目与硬件通信绝大部分都是要么串口通信(RS232 RS485 Modbus等),要么就是网络通信(TCP UDP HTTP等),所以一旦涉及到这两方面,多多少少肯定离不开对应的调试助手协助进行程序的调试,尤其是硬件工程师,更加需要第三方的独立的调试工具来验证硬件工作是否正常,这可以大大避免扯皮的事情发生,既然第三方的工具测试下来没有问题,收发数据都正常的话,那基本上可以断定是软件的问题,此时估计软件工程师心里慌得一逼啊!

基本功能:
1. 16进制数据和ASCII数据收发。
2. 定时器自动发送。
3. 自动从配置文件加载最后一次的界面设置。
4. 自动从配置文件加载数据发送下拉框的数据。可以将经常使用的数据填写在send.txt中。
5. 可启用设备模拟回复,当收到某个数据时,模拟设备自动回复数据。对应数据格式填写在device.txt中。
6. 可对单个在线连接发送数据,也可勾选全部进行发送。
7. 支持多个客户端连接并发。
8. 采用单线程。
9. 四种模式,tcp客户端、tcp服务器、udp客户端、udp服务器。

## 二、代码思路
```c++
第一步:实例化对应的类
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)
3. 个人主页:[https://blog.csdn.net/feiyangqingyun](https://blog.csdn.net/feiyangqingyun)
4. 知乎主页:[https://www.zhihu.com/people/feiyangqingyun/](https://www.zhihu.com/people/feiyangqingyun/)

九重水 2020-04-28 14:35
老刘,你们公司没有对外的官网啊?

niangu 2020-04-29 20:01
你这个我看截图是好的,我搞的时候以前那个服务端没显示

liudianwu 2020-04-30 09:04
niangu:你这个我看截图是好的,我搞的时候以前那个服务端没显示 (2020-04-29 20:01) 

羊哥你好!

liudianwu 2020-04-30 09:05
九重水:[表情] 老刘,你们公司没有对外的官网啊? (2020-04-28 14:35) 

我有个几个亿的项目,有没有兴趣

九重水 2020-04-30 09:07
liudianwu:我有个几个亿的项目,有没有兴趣 (2020-04-30 09:05) 

巧了,我也有个好几十亿项目了。

xiaoke123 2020-04-30 09:23



xiaoke123 2020-04-30 09:23
九重水:巧了,我也有个好几十亿项目了。[表情] (2020-04-30 09:07) 

精力旺盛的大佬们,老板娘没意见?

圣域天子 2020-04-30 10:24
九重水:巧了,我也有个好几十亿项目了。[表情] (2020-04-30 09:07) 

哦哟,你们能赏我一勺汤汁吗?

liudianwu 2020-04-30 16:46
九重水:巧了,我也有个好几十亿项目了。[表情] (2020-04-30 09:07) 

你屌爆了,你牛逼!


查看完整版本: [-- Qt开源作品4-网络调试助手 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled