设备调试核心就是将整个系统中的所有打印数据统一显示到一个模块上,一般都会将硬件通信的收发数据和对应的解析信号发出来或者qdebug出来,这个在调试阶段非常有用,可以具体追踪问题出在哪,哪个数据解析不对直接定位过去,这个模块直接留出界面进行打印查看等,可以在各种现场捕捉异常情况,让现场工程师将调试的数据保存好发给开发人员即可,帮助分析问题,因为很多时候是家里公司测试的好好的,到了现场各种千奇百怪的情况都会出现的,开发人员不可能一天到晚在各种现场跑来跑去,太浪费精力了,不如直接做好对应的设备调试模块,捕捉错误数据将日志文件发过来分析。
作为一个做了十年的这种软硬件通信项目开发的人员来说,这种设备调试的模块肯定考虑的比较多,比如可以直接在界面上过滤某个通信端口、某个设备进行捕捉,可以只监听发送数据、接收数据、解析数据等,最关键的一点,可以自由设定关键字进行捕捉,关键字的数据特殊颜色显示,可以设定多个关键字,这样效率大大提高,毕竟设备数据的收发是很快的,刷刷刷的,一秒钟一个屏幕就过去了,设定关键字进行捕捉自动打印不同颜色,防止眼睛疲劳,一眼老远就看到了,多么美好,在这个模块中还可以勾选输出到日志文件,设定最大行数用来自动清空数据,毕竟文本框不能一直加载数据下去,到了最大行数自动清空。
公众号:Qt实战,各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发。
公众号:Qt入门和进阶,专门介绍Qt/C++相关知识点学习,帮助Qt开发者更好的深入学习Qt。多位Qt元婴期大神,一步步带你从入门到进阶,走上财务自由之路。
官方网店:https://shop114595942.taobao.com/
在线文档:http://www.qtcdev.com/iotsystem/
#include "frmconfigdebug.h"
#include "ui_frmconfigdebug.h"
#include "quiwidget.h"
#include "deviceserver.h"
#include "dblocalthread.h"
#include "dbtcpclientthread.h"
#include "dbreceive.h"
frmConfigDebug::frmConfigDebug(QWidget *parent) : QWidget(parent), ui(new Ui::frmConfigDebug)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
}
frmConfigDebug::~frmConfigDebug()
{
delete ui;
}
void frmConfigDebug::initForm()
{
QFont font;
font.setPixelSize(QUIConfig::FontSize + 5);
ui->txtMain->setFont(font);
ui->frameRight->setFixedWidth(App::RightWidth);
ui->cboxPortName->addItem("所有端口");
ui->cboxPortName->addItems(DBData::PortInfo_PortName);
QUIHelper::setLabStyle(ui->labPortName, 1);
QUIHelper::setLabStyle(ui->labDeviceName, 1);
QUIHelper::setLabStyle(ui->labKeyValue1, 3);
QUIHelper::setLabStyle(ui->labKeyValue2, 3);
QUIHelper::setPushButtonQss(ui->btnClearData);
QUIHelper::setPushButtonQss(ui->btnReadValueAll);
#if (QT_VERSION >= QT_VERSION_CHECK(5,2,0))
ui->txtKeyValue1->setPlaceholderText("支持多个关键字,用 | 隔开");
ui->txtKeyValue2->setPlaceholderText("支持多个关键字,用 | 隔开");
#endif
//颜色下拉框
QStringList colorList = QColor::colorNames();
foreach (QString strColor, colorList) {
QPixmap pix(ui->cboxKeyColor1->iconSize());
pix.fill(strColor);
ui->cboxKeyColor1->addItem(QIcon(pix), strColor);
ui->cboxKeyColor2->addItem(QIcon(pix), strColor);
}
//绑定设备采集服务信号槽
connect(DeviceServer::Instance(), SIGNAL(sendData(QString, quint8, QByteArray)),
this, SLOT(sendData(QString, quint8, QByteArray)));
connect(DeviceServer::Instance(), SIGNAL(receiveData(QString, quint8, QByteArray)),
this, SLOT(receiveData(QString, quint8, QByteArray)));
connect(DeviceServer::Instance(), SIGNAL(receiveInfo(QString, quint8, QString)),
this, SLOT(receiveInfo(QString, quint8, QString)));
connect(DeviceServer::Instance(), SIGNAL(receiveError(QString, quint8, QString)),
this, SLOT(receiveError(QString, quint8, QString)));
}
void frmConfigDebug::initConfig()
{
ui->cboxPortName->setCurrentIndex(App::PortNameIndex);
connect(ui->cboxPortName, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->cboxDeviceName->setCurrentIndex(App::DeviceNameIndex);
connect(ui->cboxDeviceName, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
keys1 = App::KeyValue1.split("|");
ui->txtKeyValue1->setText(App::KeyValue1);
connect(ui->txtKeyValue1, SIGNAL(textChanged()), this, SLOT(saveConfig()));
ui->cboxKeyColor1->setCurrentIndex(ui->cboxKeyColor1->findText(App::KeyColor1));
connect(ui->cboxKeyColor1, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
keys2 = App::KeyValue2.split("|");
ui->txtKeyValue2->setText(App::KeyValue2);
connect(ui->txtKeyValue2, SIGNAL(textChanged()), this, SLOT(saveConfig()));
ui->cboxKeyColor2->setCurrentIndex(ui->cboxKeyColor2->findText(App::KeyColor2));
connect(ui->cboxKeyColor2, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
}
void frmConfigDebug::saveConfig()
{
App::PortNameIndex = ui->cboxPortName->currentIndex();
App::DeviceNameIndex = ui->cboxDeviceName->currentIndex();
App::KeyValue1 = ui->txtKeyValue1->toPlainText().trimmed();
App::KeyColor1 = ui->cboxKeyColor1->currentText();
App::KeyValue2 = ui->txtKeyValue2->toPlainText().trimmed();
App::KeyColor2 = ui->cboxKeyColor2->currentText();
if (App::KeyValue1.isEmpty()) {
App::KeyValue1 = "|";
}
if (App::KeyValue2.isEmpty()) {
App::KeyValue2 = "|";
}
App::writeConfig();
keys1 = App::KeyValue1.split("|");
keys2 = App::KeyValue2.split("|");
}
void frmConfigDebug::append(int type, const QString &portName, quint8 addr, const QString &data, bool clear)
{
static int currentCount = 0;
static int maxCount = 500;
//执行清空数据命令
if (clear) {
ui->txtMain->clear();
currentCount = 0;
return;
}
//执行条数到了清空数据
if (currentCount >= maxCount) {
ui->txtMain->clear();
currentCount = 0;
}
//暂停显示
if (ui->ckPause->isChecked()) {
return;
}
//过滤端口
QString currentPortName = ui->cboxPortName->currentText();
if (currentPortName != "所有端口") {
if (currentPortName != portName) {
return;
}
}
//过滤地址
quint8 deviceAddr = ui->cboxDeviceName->itemData(App::DeviceNameIndex).toInt();
if (deviceAddr != 255) {
if (deviceAddr != addr) {
return;
}
}
//过滤回车换行符
QString strData = data;
strData = strData.replace("\r", "");
strData = strData.replace("\n", "");
//不同类型不同颜色显示
QString strType;
if (type == 0) {
strType = "发送";
ui->txtMain->setTextColor(QColor("#009679"));
} else if (type == 1) {
strType = "接收";
ui->txtMain->setTextColor(QColor("#D64D54"));
} else if (type == 2) {
strType = "解析";
ui->txtMain->setTextColor(QColor("#B59481"));
} else if (type == 3) {
strType = "错误";
ui->txtMain->setTextColor(QColor("#A279C5"));
} else if (type == 4) {
strType = "解析";
ui->txtMain->setTextColor(QColor("#047058"));
} else if (type == 5) {
strType = "解析";
ui->txtMain->setTextColor(QColor("#9157C8"));
} else if (type == 6) {
strType = "解析";
ui->txtMain->setTextColor(QColor("#BA5656"));
}
//过滤关键字1,设置不同颜色
foreach (QString key, keys1) {
if (!key.isEmpty() && data.contains(key)) {
ui->txtMain->setTextColor(QColor(App::KeyColor1));
break;
}
}
//过滤关键字2,设置不同颜色
foreach (QString key, keys2) {
if (!key.isEmpty() && data.contains(key)) {
ui->txtMain->setTextColor(QColor(App::KeyColor2));
break;
}
}
QString strAddr = QString("%1").arg(addr, 3, 10, QChar('0'));
strData = QString("%1 %2[%3] %4: %5").arg(TIMEMS).arg(portName).arg(strAddr).arg(strType).arg(strData);
ui->txtMain->append(strData);
currentCount++;
//输出数据
if (ui->ckDebug->isChecked()) {
qDebug() << strData;
}
}
void frmConfigDebug::sendData(const QString &portName, quint8 addr, const QByteArray &data)
{
if (!ui->ckSendData->isChecked()) {
return;
}
append(0, portName, addr, QUIHelper::byteArrayToHexStr(data));
}
void frmConfigDebug::receiveData(const QString &portName, quint8 addr, const QByteArray &data)
{
if (!ui->ckReceiveData->isChecked()) {
return;
}
append(1, portName, addr, QUIHelper::byteArrayToHexStr(data));
}
void frmConfigDebug::receiveInfo(const QString &portName, quint8 addr, const QString &data)
{
if (!ui->ckReceiveInfo->isChecked()) {
return;
}
append(2, portName, addr, data);
}
void frmConfigDebug::receiveError(const QString &portName, quint8 addr, const QString &data)
{
append(3, portName, addr, data);
}
void frmConfigDebug::debugDbLocalThread(const QString &msg)
{
append(4, "本地数据库", 255, msg);
}
void frmConfigDebug::debugDbTcpClientThread(const QString &msg)
{
append(5, "云端数据库", 255, msg);
}
void frmConfigDebug::debugDbReceive(const QString &msg)
{
append(6, "下载数据库", 255, msg);
}
void frmConfigDebug::on_btnClearData_clicked()
{
append(0, "", 255, "", true);
}
void frmConfigDebug::on_btnReadValueAll_clicked()
{
DeviceServer::Instance()->readValueAll();
}
void frmConfigDebug::on_cboxPortName_currentIndexChanged(const QString &arg1)
{
//重新载入该端口对应的所有设备
ui->cboxDeviceName->clear();
ui->cboxDeviceName->addItem("所有设备", 255);
for (int i = 0; i < DBData::DeviceInfo_Count; i++) {
if (DBData::DeviceInfo_PortName.at(i) == arg1) {
QString deviceName = DBData::DeviceInfo_DeviceName.at(i);
quint8 deviceAddr = DBData::DeviceInfo_DeviceAddr.at(i);
ui->cboxDeviceName->addItem(deviceName, deviceAddr);
}
}
}
void frmConfigDebug::on_ckOther_stateChanged(int arg1)
{
on_btnClearData_clicked();
if (arg1 != 0) {
//先解除设备采集服务信号槽
disconnect(DeviceServer::Instance(), SIGNAL(sendData(QString, quint8, QByteArray)),
this, SLOT(sendData(QString, quint8, QByteArray)));
disconnect(DeviceServer::Instance(), SIGNAL(receiveData(QString, quint8, QByteArray)),
this, SLOT(receiveData(QString, quint8, QByteArray)));
disconnect(DeviceServer::Instance(), SIGNAL(receiveInfo(QString, quint8, QString)),
this, SLOT(receiveInfo(QString, quint8, QString)));
disconnect(DeviceServer::Instance(), SIGNAL(receiveError(QString, quint8, QString)),
this, SLOT(receiveError(QString, quint8, QString)));
//绑定其他信号槽
connect(DbLocalThread::Instance(), SIGNAL(debug(QString)), this, SLOT(debugDbLocalThread(QString)));
connect(DbTcpClientThread::Instance(), SIGNAL(debug(QString)), this, SLOT(debugDbTcpClientThread(QString)));
connect(DbReceive::Instance(), SIGNAL(debug(QString)), this, SLOT(debugDbReceive(QString)));
} else {
//先绑定设备采集服务信号槽
connect(DeviceServer::Instance(), SIGNAL(sendData(QString, quint8, QByteArray)),
this, SLOT(sendData(QString, quint8, QByteArray)));
connect(DeviceServer::Instance(), SIGNAL(receiveData(QString, quint8, QByteArray)),
this, SLOT(receiveData(QString, quint8, QByteArray)));
connect(DeviceServer::Instance(), SIGNAL(receiveInfo(QString, quint8, QString)),
this, SLOT(receiveInfo(QString, quint8, QString)));
connect(DeviceServer::Instance(), SIGNAL(receiveError(QString, quint8, QString)),
this, SLOT(receiveError(QString, quint8, QString)));
//解除其他信号槽
disconnect(DbLocalThread::Instance(), SIGNAL(debug(QString)), this, SLOT(debugDbLocalThread(QString)));
disconnect(DbTcpClientThread::Instance(), SIGNAL(debug(QString)), this, SLOT(debugDbTcpClientThread(QString)));
disconnect(DbReceive::Instance(), SIGNAL(debug(QString)), this, SLOT(debugDbReceive(QString)));
}
}
版权说明:如非注明,本站文章均为 扬州驻场服务-网络设备调试-监控维修-南京泽同信息科技有限公司 原创,转载请注明出处和附带本文链接。
请在这里放置你的在线分享代码