From aa31e1c058c1c2c8e89c60894d4134467f553a9d Mon Sep 17 00:00:00 2001 From: zhaojingshi Date: Tue, 11 Oct 2022 09:55:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E6=A1=86?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=9F=A5=E7=9C=8B=E9=80=9A=E4=BF=A1?= =?UTF-8?q?=E6=8A=A5=E6=96=87=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serialport.cpp | 25 ++++++++++++++++++++++++- tcpclient.cpp | 25 ++++++++++++++++++++++++- widget.cpp | 6 ++++++ widget.h | 4 ++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/serialport.cpp b/serialport.cpp index 8b15da7..19f6014 100644 --- a/serialport.cpp +++ b/serialport.cpp @@ -1,4 +1,5 @@ #include "serialport.h" +#include "widget.h" #include SerialPort::SerialPort(QObject *parent) : BaseParent(parent) @@ -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(); } diff --git a/tcpclient.cpp b/tcpclient.cpp index 20e88f8..6def90f 100644 --- a/tcpclient.cpp +++ b/tcpclient.cpp @@ -1,4 +1,5 @@ #include "tcpclient.h" +#include "widget.h" #include #include @@ -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(); } diff --git a/widget.cpp b/widget.cpp index 4cbdb78..b6f943f 100644 --- a/widget.cpp +++ b/widget.cpp @@ -15,6 +15,10 @@ Widget::~Widget() { } +void Widget::appendLog(QString& logStr) +{ + log->append(logStr); +} void Widget::initView() { @@ -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); @@ -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); diff --git a/widget.h b/widget.h index b53b429..e556d80 100644 --- a/widget.h +++ b/widget.h @@ -10,6 +10,7 @@ #include #include #include +#include class RadioButton : public QRadioButton { @@ -29,6 +30,7 @@ class Widget : public QWidget Q_OBJECT public: + void appendLog(QString &logStr); Widget(QWidget *parent = 0); ~Widget(); private: @@ -60,6 +62,8 @@ class Widget : public QWidget QPushButton *open_btn; + QTextEdit *log; + QVector fifo; };