Qt串口调试工具


头文件代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSerialPort>
#include <QDebug>
#include <QMessageBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QSerialPort *serial;
    void setcom();
    void stopBits();

private slots:
    void serialRead();
    void on_pushButtonOpenCom_clicked();
    void on_pushButtonCloseCom_clicked();
    void on_pushButtonClear_clicked();
    void on_pushButtonSend_clicked();
    void on_pushButtonabout_clicked();
};

#endif // MAINWINDOW_H



主要代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
#include <QDesktopWidget>

/*
 * 串口调试工具
 * 1.现实基本的串口发送接收
 * 2.支持linux/windows夸平台编译
 *
 * by dong
 * 2014.8.1
 */

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("串口调试"));
    ui->statusBar->showMessage(tr("欢迎使用串口调试-----by dong"));
    QDesktopWidget *desktop = QApplication::desktop();
    move(desktop->width()/2-this->width()/2,
         desktop->height()/2-this->height()/2);

    setcom();
    stopBits();
    ui->comboBoxBaudRate->setCurrentIndex(4);
    ui->comboBoxParity->setCurrentIndex(2);
    ui->comboBoxFlowControl->setCurrentIndex(2);

    ui->pushButtonSend->setEnabled(false);
    ui->pushButtonCloseCom->setEnabled(false);

    connect(ui->lineEditSend, SIGNAL(returnPressed()),
            this, SLOT(on_pushButtonSend_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

//    设置串口
void MainWindow::setcom()
{
    //5.2
    ui->comboBoxPort->clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QStringList list;
        list << info.portName()
             << info.description()
             << info.manufacturer()
             << info.systemLocation()
             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());

        ui->comboBoxPort->addItem(list.first(), list);
    }

    //5.3
//    ui->comboBoxPort->clear();
//    static const QString blankString = QObject::tr("N/A");
//    QString description;
//    QString manufacturer;
//    QString serialNumber;
//    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
//        QStringList list;
//        description = info.description();
//        manufacturer = info.manufacturer();
//        serialNumber = info.serialNumber();
//        list << info.portName()
//             << (!description.isEmpty() ? description : blankString)
//             << (!manufacturer.isEmpty() ? manufacturer : blankString)
//             << (!serialNumber.isEmpty() ? serialNumber : blankString)
//             << info.systemLocation()
//             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
//             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);

//        ui->comboBoxPort->addItem(list.first(), list);
//    }
}

//停止位
void MainWindow::stopBits()
{
    ui->comboBoxStopBits->addItem("1");
#ifdef Q_OS_WIN
    ui->comboBoxStopBits->addItem("1.5");
#endif
    ui->comboBoxStopBits->addItem("2");
}

//读串口
void MainWindow::serialRead()
{
    QByteArray readData = serial->readAll();
    while(serial->waitForReadyRead(10)){
        readData+=(serial->readAll());
    }
    ui->textBrowser->insertPlainText(QString(readData));
    ui->textBrowser->moveCursor(QTextCursor::End);
}

//打开串口
void MainWindow::on_pushButtonOpenCom_clicked()
{
    QString portName = ui->comboBoxPort->currentText();
    qDebug()<<portName<<endl;
    serial = new QSerialPort(portName);
    //以读写方式打开串口
    if(serial->open(QIODevice::ReadWrite)){
        ui->pushButtonSend->setEnabled(true);
        ui->pushButtonOpenCom->setEnabled(false);
        ui->pushButtonCloseCom->setEnabled(true);
        ui->groupBox->setEnabled(false);
        ui->statusBar->showMessage(tr("open succeed"));
    }else{
        QMessageBox::critical(this, tr("open error"), serial->errorString());
    }

    //设置波特率
    serial->setBaudRate(ui->comboBoxBaudRate->currentText().toInt());
    //设置数据位
    switch (ui->comboBoxDataBits->currentIndex()) {
    case 0:
        serial->setDataBits(QSerialPort::Data8);
        break;
    case 1:
        serial->setDataBits(QSerialPort::Data7);
        break;
    case 2:
        serial->setDataBits(QSerialPort::Data6);
        break;
    case 3:
        serial->setDataBits(QSerialPort::Data5);
        break;
    default:
        serial->setDataBits(QSerialPort::Data8);
        break;
    }
    //设置奇偶校验位
    switch (ui->comboBoxParity->currentIndex()) {
    case 0:
        serial->setParity(QSerialPort::EvenParity);
        break;
    case 1:
        serial->setParity(QSerialPort::OddParity);
        break;
    case 2:
        serial->setParity(QSerialPort::NoParity);
        break;
    case 3:
        serial->setParity(QSerialPort::MarkParity);
        break;
    case 4:
        serial->setParity(QSerialPort::SpaceParity);
        break;
    default:
        serial->setParity(QSerialPort::NoParity);
        break;
    }
    //设置停止位
//    if(ui->comboBoxStopBits->currentText()==tr("1")){
//        serial->setStopBits(QSerialPort::OneStop);
//    }else if(ui->comboBoxStopBits->currentText()==tr("1.5")){
//        serial->setStopBits(QSerialPort::OneAndHalfStop);
//    }else if(ui->comboBoxStopBits->currentText()==tr("2")){
//        serial->setStopBits(QSerialPort::TwoStop);
//    }
    //设置控制流
    switch (ui->comboBoxFlowControl->currentIndex()) {
    case 0:
        serial->setFlowControl(QSerialPort::SoftwareControl);
        break;
    case 1:
        serial->setFlowControl(QSerialPort::HardwareControl);
        break;
    case 2:
        serial->setFlowControl(QSerialPort::NoFlowControl);
        break;
    default:
        serial->setFlowControl(QSerialPort::NoFlowControl);
        break;
    }

    connect(serial, SIGNAL(readyRead()),
            this, SLOT(serialRead()));
}

//关闭串口
void MainWindow::on_pushButtonCloseCom_clicked()
{
    serial->close();
    ui->pushButtonOpenCom->setEnabled(true);
    ui->pushButtonSend->setEnabled(false);
    ui->pushButtonCloseCom->setEnabled(false);
    ui->groupBox->setEnabled(true);
    ui->statusBar->showMessage(tr("close serial"));
}

//清除
void MainWindow::on_pushButtonClear_clicked()
{
    ui->textBrowser->clear();
}

//发送
void MainWindow::on_pushButtonSend_clicked()
{
    serial->write(ui->lineEditSend->text().toLocal8Bit());
}

//关于
void MainWindow::on_pushButtonabout_clicked()
{
    QMessageBox::about(this, tr("关于"), tr("<b>by dong 2014.8.1"));
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值