Skip to content

Commit

Permalink
增加日志框,用于查看通信报文记录
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojingshi committed Oct 11, 2022
1 parent 6d685e7 commit aa31e1c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
25 changes: 24 additions & 1 deletion serialport.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "serialport.h"
#include "widget.h"
#include <QIODevice>

SerialPort::SerialPort(QObject *parent) : BaseParent(parent)
Expand Down Expand Up @@ -28,12 +29,34 @@ void SerialPort::Close()

void SerialPort::handleReadyRead()
{
emit sig_recv_data(serial->readAll());
Widget* wid = (Widget*)parent();
QByteArray data = serial->readAll();
QString logString = "";

logString = serial->portName();
logString += QString::asprintf(" read(%d):", data.size());
for(int i = 0; i < data.size(); i++)
{
logString += QString::asprintf("%02X ", (unsigned char)data.at(i));
}
logString += "\n";
wid->appendLog(logString);
emit sig_recv_data(data);
}

void SerialPort::slot_send_data(QByteArray hex)
{
Widget* wid = (Widget*)parent();
if(serial->isOpen() && serial->isWritable()) {
QString logString = "";
logString = serial->portName();
logString += QString::asprintf(" write(%d):", hex.size());
for(int i = 0; i < hex.size(); i++)
{
logString += QString::asprintf("%02X ", (unsigned char)hex.at(i));
}
logString += "\n";
wid->appendLog(logString);
serial->write(hex);
serial->flush();
}
Expand Down
25 changes: 24 additions & 1 deletion tcpclient.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tcpclient.h"
#include "widget.h"
#include <QCoreApplication>
#include <QTime>

Expand Down Expand Up @@ -26,12 +27,34 @@ void TcpClient::Close()

void TcpClient::handleReadyRead()
{
emit sig_recv_data(socket->readAll());
Widget* wid = (Widget*)parent();
QByteArray data = socket->readAll();
QString logString = "";

logString = "TCPC" + socket->peerAddress().toString() + ":" + socket->peerPort();
logString += QString::asprintf(" read(%d):", data.size());
for(int i = 0; i < data.size(); i++)
{
logString += QString::asprintf("%02X ", (unsigned char)data.at(i));
}
logString += "\n";
wid->appendLog(logString);
emit sig_recv_data(data);
}

void TcpClient::slot_send_data(QByteArray hex)
{
Widget* wid = (Widget*)parent();
if(socket->state() == QAbstractSocket::ConnectedState) {
QString logString = "";
logString = "TCPC" + socket->peerAddress().toString() + ":" + socket->peerPort();
logString += QString::asprintf(" write(%d):", hex.size());
for(int i = 0; i < hex.size(); i++)
{
logString += QString::asprintf("%02X ", (unsigned char)hex.at(i));
}
logString += "\n";
wid->appendLog(logString);
socket->write(hex);
socket->flush();
}
Expand Down
6 changes: 6 additions & 0 deletions widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Widget::~Widget()
{

}
void Widget::appendLog(QString& logStr)
{
log->append(logStr);
}

void Widget::initView()
{
Expand Down Expand Up @@ -87,6 +91,7 @@ void Widget::initView()


open_btn = new QPushButton("打开",this);
log = new QTextEdit(this);
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addLayout(hLayout1);
vLayout->addLayout(hLayout2);
Expand All @@ -95,6 +100,7 @@ void Widget::initView()
vLayout->addLayout(hLayout5);
vLayout->addLayout(hLayout6);
vLayout->addWidget(open_btn);
vLayout->addWidget(log);
setLayout(vLayout);

BaseParent *serial1 = new SerialPort(this);
Expand Down
4 changes: 4 additions & 0 deletions widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QPushButton>
#include <QSpinBox>
#include <QRadioButton>
#include <QTextEdit>

class RadioButton : public QRadioButton
{
Expand All @@ -29,6 +30,7 @@ class Widget : public QWidget
Q_OBJECT

public:
void appendLog(QString &logStr);
Widget(QWidget *parent = 0);
~Widget();
private:
Expand Down Expand Up @@ -60,6 +62,8 @@ class Widget : public QWidget

QPushButton *open_btn;

QTextEdit *log;

QVector<int> fifo;
};

Expand Down

0 comments on commit aa31e1c

Please sign in to comment.