1.工程文件
工程文件中添加serilport
QT += serialport
2.主程序
主程序文件main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
3.文本控制台类
控制台类头文件console.h
#ifndef CONSOLE_H
#define CONSOLE_H
#include <QPlainTextEdit>
class Console : public QPlainTextEdit
{
Q_OBJECT
signals:
void getData(const QByteArray &data);
public:
explicit Console(QWidget *parent = nullptr);
void putData(const QByteArray &data);
void setLocalEchoEnabled(bool set);
protected:
void keyPressEvent(QKeyEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseDoubleClickEvent(QMouseEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;
private:
bool m_localEchoEnabled = false;
};
#endif // CONSOLE_H
控制台实现文件console.cpp
#include "console.h"
#include <QScrollBar>
Console::Console(QWidget *parent) :
QPlainTextEdit(parent)
{
document()->setMaximumBlockCount(100);
QPalette p = palette();
p.setColor(QPalette::Base, Qt::black);
p.setColor(QPalette::Text, Qt::green);
setPalette(p);
}
void Console::putData(const QByteArray &data)
{
insertPlainText(data);
QScrollBar *bar = verticalScrollBar();
bar->setValue(bar->maximum());
}
void Console::setLocalEchoEnabled(bool set)
{
m_localEchoEnabled = set;
}
void Console::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Backspace:
case Qt::Key_Left:
case Qt::Key_Right:
case Qt::Key_Up:
case Qt::Key_Down:
break;
default:
if (m_localEchoEnabled)
QPlainTextEdit::keyPressEvent(e);
emit getData(e->text().toLocal8Bit());
}
}
void Console::mousePressEvent(QMouseEvent *e)
{
Q_UNUSED(e)
setFocus();
}
void Console::mouseDoubleClickEvent(QMouseEvent *e)
{
Q_UNUSED(e)
}
void Console::contextMenuEvent(QContextMenuEvent *e)
{
Q_UNUSED(e)
}
4. 串口设置类
串口设置类头文件settingdialog.h
串口设置类实现文件settingdialog.cpp
5. 主窗口类
主窗口类头文件mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSerialPort>
QT_BEGIN_NAMESPACE
class QLabel;
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class Console;
class SettingsDialog;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openSerialPort();
void closeSerialPort();
void about();
void writeData(const QByteArray &data);
void readData();
void handleError(QSerialPort::SerialPortError error);
private:
void initActionsConnections();
private:
void showStatusMessage(const QString &message);
Ui::MainWindow *m_ui = nullptr;
QLabel *m_status = nullptr;
Console *m_console = nullptr;
SettingsDialog *m_settings = nullptr;
QSerialPort *m_serial = nullptr;
};
#endif // MAINWINDOW_H
主窗口实现文件mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "console.h"
#include "settingsdialog.h"
#include <QLabel>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::MainWindow),
m_status(new QLabel),
m_console(new Console),
m_settings(new SettingsDialog),
m_serial(new QSerialPort(this))
{
m_ui->setupUi(this);
setWindowTitle(tr("Serial Servo"));
m_console->setEnabled(false);
setCentralWidget(m_console);
m_ui->actionConnect->setEnabled(true);
m_ui->actionDisconnect->setEnabled(false);
m_ui->actionQuit->setEnabled(true);
m_ui->statusBar->addWidget(m_status);
initActionsConnections();
connect(m_serial, &QSerialPort::errorOccurred, this, &MainWindow::handleError);
connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);
connect(m_console, &Console::getData, this, &MainWindow::writeData);
}
MainWindow::~MainWindow()
{
delete m_ui;
delete m_settings;
}
void MainWindow::openSerialPort()
{
const SettingsDialog::Settings p = m_settings->settings();
m_serial->setPortName(p.name);
m_serial->setBaudRate(p.baudRate);
m_serial->setDataBits(p.dataBits);
m_serial->setParity(p.parity);
m_serial->setStopBits(p.stopBits);
m_serial->setFlowControl(p.flowControl);
if(m_serial->open(QIODevice::ReadWrite)){
m_console->setEnabled(true);
m_console->setLocalEchoEnabled(p.localEchoEnabled);
m_ui->actionConnect->setEnabled(false);
m_ui->actionDisconnect->setEnabled(true);
m_ui->actionConfigure->setEnabled(false);
showStatusMessage(tr("Connect to %1 :%2, %3, %4, %5, %6")
.arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
} else {
QMessageBox::critical(this, tr("Error"), m_serial->errorString());
showStatusMessage(tr("Open error"));
}
}
void MainWindow::closeSerialPort()
{
if(m_serial->isOpen())
{
m_serial->close();
}
m_console->setEnabled(false);
m_ui->actionConnect->setEnabled(true);
m_ui->actionDisconnect->setEnabled(false);
m_ui->actionConfigure->setEnabled(true);
showStatusMessage(tr("Disconnected"));
}
void MainWindow::about()
{
QMessageBox::about(this, "About Serial Servo",
tr("Serial Servo Demo"));
}
void MainWindow::writeData(const QByteArray &data)
{
m_serial->write(data);
}
void MainWindow::readData()
{
const QByteArray data = m_serial->readAll();
m_console->putData(data);
}
void MainWindow::handleError(QSerialPort::SerialPortError error)
{
if(error == QSerialPort::ResourceError){
QMessageBox::critical(this, tr("Critical Error"), m_serial->errorString());
closeSerialPort();
}
}
void MainWindow::initActionsConnections()
{
connect(m_ui->actionConnect, &QAction::triggered, this, &MainWindow::openSerialPort);
connect(m_ui->actionDisconnect, &QAction::triggered, this, &MainWindow::closeSerialPort);
connect(m_ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
connect(m_ui->actionConfigure, &QAction::triggered, m_settings, &SettingsDialog::show);
connect(m_ui->actionClear, &QAction::triggered, m_console, &Console::clear);
connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::about);
}
void MainWindow::showStatusMessage(const QString &message)
{
m_status->setText(message);
}
6. 运行结果