关闭

Qt实现UART串口调试功能

标签: qtUART调试
837人阅读 评论(2) 收藏 举报
分类:

如何在Qt上实现UART串口调试功能

需要的工具

Qt版本:Qt4.8.6
外部包:qextserialport-1.2rc.zip http://download.csdn.net/detail/u010564071/7644363

关于qextserialport-1.2rc.zip

解压后“/src”目录下会有以下几个文件:

qextserialport.pri
qextserialport_p.h
qextserialport_global.h
qextserialport.h
qextserialenumerator_p.h
qextserialenumerator.h
qextserialport_win.cpp
qextserialport_unix.cpp
qextserialport.cpp
qextserialenumerator_win.cpp
qextserialenumerator_unix.cpp
qextserialenumerator_osx.cpp
qextserialenumerator_linux.cpp
qextserialenumerator.cpp

根据操作系统选择需要的文件,下面以windows环境举例,介绍一下如何使用Qt在windows环境下开发UART串口调试功能。


首先通过Qt设计师设计Ui界面,主要是添加UART配置信息

常见的包括串口、波特率、数据位、校验位、停止位、超时时间、查询模式,再添加一个自动发送的功能

这里写图片描述

uarttrans.h

#ifndef UARTTRANS_H
#define UARTTRANS_H

#include "ui_uarttrans.h"
#include <QtGui/QMainWindow>
#include "qextserialport.h"
#include "qextserialenumerator.h"

class uarttrans : public QWidget
{
    Q_OBJECT
public:
    uarttrans(QWidget *parent = 0);
    Ui::uarttrans ui;
    ~uarttrans();

private:
    QTimer *timer; //超时时间
    QTimer *autoTimer; //自动发送时间
    QextSerialPort *port; //串口类
    QextSerialEnumerator *enumerator; //枚举串口

public:
    void initial();//初始化界面
public slots:
    void onPortNameChanged(const QString &name); //串口COM选择
    void onBandRateChanged(int idx); //设置波特率
    void onParityChanged(int idx); //设置校验位
    void onDataBitsChanged(int idx);//设置数据位
    void onStopBitsChanged(int idx);//设置停止位
    void onQueryModeChanged(int idx);//设置查询模式
    void onTimeoutChanged(int val);//设置超时
    void onOpenCloseButtonClicked();//开关串口设置
    void onSendButtonClicked();//发送数据功能
    void onReadyRead();//接收数据功能
    void onPortAddedOrRemoved();//串口插拔检测
    void onAutoWriteClicked();//自动发送功能
};

extern uarttrans *Uarttrans;
#endif

uarttrans.cpp

#include "uarttrans.h"

uarttrans::uarttrans(QWidget *parent):QWidget(parent)
{
    ui.setupUi(this);

    //先检测已经连接的设备串口,并添加进串口下拉菜单
    foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
        ui.portBox->addItem(info.portName);
    ui.portBox->setEditable(true);

    //初始化界面
    initial();

    timer = new QTimer(this);
    timer->setInterval(40);

    PortSettings settings = {BAUD115200, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
    port = new QextSerialPort(ui.portBox->currentText(), settings, QextSerialPort::Polling);

    enumerator = new QextSerialEnumerator(this);
    enumerator->setUpNotifications();

    //检测设备插拔,添加或删除串口
    connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
    connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));

    connect(ui.bandRateBox, SIGNAL(currentIndexChanged(int)), SLOT(onBandRateChanged(int)));
    connect(ui.parityBox, SIGNAL(currentIndexChanged(int)), SLOT(onParityChanged(int)));
    connect(ui.dataBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onDataBitsChanged(int)));
    connect(ui.stopBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onStopBitsChanged(int)));
    connect(ui.queryModeBox, SIGNAL(currentIndexChanged(int)), SLOT(onQueryModeChanged(int)));
    connect(ui.timeoutBox, SIGNAL(valueChanged(int)), SLOT(onTimeoutChanged(int)));
    connect(ui.portBox, SIGNAL(editTextChanged(QString)), SLOT(onPortNameChanged(QString)));
    connect(ui.openCloseButton, SIGNAL(clicked()), SLOT(onOpenCloseButtonClicked()));
    connect(ui.sendButton, SIGNAL(clicked()), SLOT(onSendButtonClicked()));
    connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
    connect(port, SIGNAL(readyRead()), SLOT(onReadyRead()));
    connect(ui.autoWriteBox,SIGNAL(clicked()),this,SLOT(onAutoWriteClicked()));

    //自动发送功能
    autoTimer = new QTimer(this);
    connect(autoTimer,SIGNAL(timeout()),this,SLOT(onSendButtonClicked()));
}

void uarttrans::initial()
{
    ui.bandRateBox->addItem("1200",BAUD1200);
    ui.bandRateBox->addItem("2400", BAUD2400);
    ui.bandRateBox->addItem("4800", BAUD4800);
    ui.bandRateBox->addItem("9600", BAUD9600);
    ui.bandRateBox->addItem("19200", BAUD19200);
    ui.bandRateBox->addItem("115200",BAUD115200);
    ui.bandRateBox->setCurrentIndex(5);

    ui.dataBitsBox->addItem("5", DATA_5);
    ui.dataBitsBox->addItem("6", DATA_6);
    ui.dataBitsBox->addItem("7", DATA_7);
    ui.dataBitsBox->addItem("8", DATA_8);
    ui.dataBitsBox->setCurrentIndex(3);

    ui.stopBitsBox->addItem("1", STOP_1);
    ui.stopBitsBox->addItem("2", STOP_2);
    ui.stopBitsBox->setCurrentIndex(0);

    ui.parityBox->addItem("NONE", PAR_NONE);
    ui.parityBox->addItem("ODD", PAR_ODD);
    ui.parityBox->addItem("EVEN", PAR_EVEN);
    ui.parityBox->setCurrentIndex(0);

    ui.queryModeBox->addItem("Polling", QextSerialPort::Polling);
    ui.queryModeBox->addItem("EventDriven", QextSerialPort::EventDriven);
    ui.queryModeBox->setCurrentIndex(0);

}
uarttrans::~uarttrans()
{
    delete port;
}
void uarttrans::onPortNameChanged(const QString & /*name*/)
{
    if (port->isOpen())
    {
        port->close();
        ui.openCloseButton->setText("open");
    }else
        ui.openCloseButton->setText("close");
}
void uarttrans::onBandRateChanged(int idx)
{
     port->setBaudRate((BaudRateType)ui.bandRateBox->itemData(idx).toInt());
}
void uarttrans::onParityChanged(int idx)
{
    port->setParity((ParityType)ui.parityBox->itemData(idx).toInt());
}
void uarttrans::onDataBitsChanged(int idx)
{
    port->setDataBits((DataBitsType)ui.dataBitsBox->itemData(idx).toInt());
}
void uarttrans::onStopBitsChanged(int idx)
{
    port->setStopBits((StopBitsType)ui.stopBitsBox->itemData(idx).toInt());
}
void uarttrans::onQueryModeChanged(int idx)
{
    port->setQueryMode((QextSerialPort::QueryMode)ui.queryModeBox->itemData(idx).toInt());
}
void uarttrans::onTimeoutChanged(int val)
{
    port->setTimeout(val);
}
void uarttrans::onOpenCloseButtonClicked()
{
    if (!port->isOpen())
    {
        port->setPortName(ui.portBox->currentText());
        port->open(QIODevice::ReadWrite);
        ui.openCloseButton->setText("close");
    }else
    {
        port->close();
        ui.openCloseButton->setText("open");
    }

    if (port->isOpen() && port->queryMode() == QextSerialPort::Polling)
        timer->start();
    else
        timer->stop();
}
void uarttrans::onSendButtonClicked()
{
    if (port->isOpen() && !ui.sendEdit->toPlainText().isEmpty())
        port->write(ui.sendEdit->toPlainText().toLatin1());

}
void uarttrans::onReadyRead()
{
    if (port->bytesAvailable()) {
        ui.recvEdit->moveCursor(QTextCursor::End);
        ui.recvEdit->insertPlainText(QString::fromLatin1(port->readAll()));  
    }
}
void uarttrans::onPortAddedOrRemoved()
{
    QString current = ui.portBox->currentText();

    ui.portBox->blockSignals(true);
    ui.portBox->clear();
    foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
        ui.portBox->addItem(info.portName);

    ui.portBox->setCurrentIndex(ui.portBox->findText(current));

    ui.portBox->blockSignals(false);
}
void uarttrans::onAutoWriteClicked()
{
    if (ui.autoWriteBox->isChecked())
    {
        autoTimer->start(ui.AutotimeoutBox->value());
        ui.AutotimeoutBox->setEnabled(false);
    }else
    {
        autoTimer->stop();
        ui.AutotimeoutBox->setEnabled(true);
    }
}
3
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2307次
    • 积分:108
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论