打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt编程之“串口助手”

Qt编程之“串口助手”

(2011-11-19 11:58:01)
标签:

串口助手

杂谈

分类: Qt编程

首先展示一下我的效果图,可以大致了解一下我这个串口助手的基本功能:



下面是该软件mainwindow的相关源代码,也是这个小软件的精华部分,重要代码行后面均有注释,有祝大家理解:

#include "mainwindow.h"

#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(tr("草原神鹰串口助手"));//初始化主窗口的标题
    ui->closeMyComBtn->setEnabled(false);//开始“关闭摁钮”不可用
    ui->sendMsgBtn->setEnabled(false);////开始“发送数据摁钮”不可用
    ui->choosetextpushButton->setEnabled(false);
    ui->choose_positionpushButton->setEnabled(false);
    ui->savedatalineEdit->setEnabled(false);
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(false);
    ui->sendtextpushButton->setEnabled(false);
    ui->savedatalineEdit->setEnabled(false);
    ui->sendtextkeylineEdit->setEnabled(false);
    ui->sendtextpathlineEdit->setEnabled(false);
    ui->sendMsglineEdit->setEnabled(false);
    ui->textBrowser->setEnabled(false);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::readMyCom()
{
    QByteArray temp = myCom->readAll();
    //读取数据缓冲区中的所有数据给临时变量temp
    ui->textBrowser->insertPlainText(temp);
    //将串口数据显示在窗口的文本浏览器中
}
void MainWindow::on_openMyComBtn_clicked()
{
    QString portName = ui->portNamecomboBox->currentText();//获取串口名
    myCom = new Win_QextSerialPort(portName,QextSerialBase::EventDriven);
    //定义串口对象
    myCom->open(QIODevice::ReadWrite);//以读写方式打开串口
    if(ui->baudRatecomboBox->currentText()==tr("110"))//根据波特率窗口设置串口波特率
        myCom->setBaudRate(BAUD9600);
    else if(ui->baudRatecomboBox->currentText()==tr("9600"))
        myCom->setBaudRate(BAUD300);
   else if(ui->baudRatecomboBox->currentText()==tr("600"))
        myCom->setBaudRate(BAUD600);
   else if(ui->baudRatecomboBox->currentText()==tr("1200"))
        myCom->setBaudRate(BAUD1200);
    else if(ui->baudRatecomboBox->currentText()==tr("2400"))
        myCom->setBaudRate(BAUD2400);
    else if(ui->baudRatecomboBox->currentText()==tr("4800"))
        myCom->setBaudRate(BAUD4800);
    else if(ui->baudRatecomboBox->currentText()==tr("300"))
        myCom->setBaudRate(BAUD9600);
    else if(ui->baudRatecomboBox->currentText()==tr("19200"))
        myCom->setBaudRate(BAUD19200);
    //设置数据位
    if(ui->dataBitscomboBox->currentText()==tr("8"))
        myCom->setDataBits(DATA_8);
    else if(ui->dataBitscomboBox->currentText()==tr("7"))
        myCom->setDataBits(DATA_7);
    //设置奇偶校验位
    if(ui->paritycomboBox->currentText()==tr("无"))
        myCom->setParity(PAR_NONE);
    else if(ui->paritycomboBox->currentText()==tr("奇"))
        myCom->setParity(PAR_ODD);
    else if(ui->paritycomboBox->currentText()==tr("偶"))
        myCom->setParity(PAR_EVEN);
    //设置停止位
    if(ui->stopBitscomboBox->currentText()==tr("1"))
        myCom->setStopBits(STOP_1);
    else if(ui->stopBitscomboBox->currentText()==tr("2"))
        myCom->setStopBits(STOP_2);
    myCom->setFlowControl(FLOW_OFF);//设置为无数据流控制
    myCom->setTimeout(500);//延时
    connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
    //信号和槽函数连接,当串口缓冲区有数据时,进行读串口操作
    ui->openMyComBtn->setEnabled(false);//打开串口后“打开串口摁钮”不可用
    ui->closeMyComBtn->setEnabled(true);//打开串口后”关闭串口摁钮“可用
    ui->sendMsgBtn->setEnabled(true);////打开串口后“发送数据摁钮”可用
    ui->baudRatecomboBox->setEnabled(false);
    ui->portNamecomboBox->setEnabled(false);
    ui->paritycomboBox->setEnabled(false);
    ui->stopBitscomboBox->setEnabled(false);
    ui->dataBitscomboBox->setEnabled(false);
    ui->choosetextpushButton->setEnabled(true);
    ui->choose_positionpushButton->setEnabled(true);
    ui->savedatalineEdit->setEnabled(true);
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(true);
    ui->sendtextpushButton->setEnabled(true);
    ui->savedatalineEdit->setEnabled(true);
    ui->sendtextkeylineEdit->setEnabled(true);
    ui->sendtextpathlineEdit->setEnabled(true);
    ui->sendMsglineEdit->setEnabled(true);
    ui->textBrowser->setEnabled(true);
}
void MainWindow::on_closeMyComBtn_clicked()
{
    myCom->close();//关闭串口
    ui->openMyComBtn->setEnabled(true);//关闭串口后“打开串口摁钮”可用
    ui->sendMsgBtn->setEnabled(false);////关闭串口后“发送数据摁钮”不可用
    ui->closeMyComBtn->setEnabled(false);//关闭串口后”关闭串口摁钮“不可用
    ui->baudRatecomboBox->setEnabled(true);
    ui->portNamecomboBox->setEnabled(true);
    ui->paritycomboBox->setEnabled(true);
    ui->stopBitscomboBox->setEnabled(true);
    ui->dataBitscomboBox->setEnabled(true);
    ui->choosetextpushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(false);
    ui->sendtextpushButton->setEnabled(false);
    ui->savedatalineEdit->setEnabled(false);
    ui->sendtextkeylineEdit->setEnabled(false);
    ui->sendtextpathlineEdit->setEnabled(false);
    ui->sendMsglineEdit->setEnabled(false);
}
void MainWindow::on_sendMsgBtn_clicked()
{
    myCom->write(ui->sendMsglineEdit->text().toAscii());
    //将行编辑器中文本转化成ascii码形式写入串口
}
bool MainWindow::on_choosetextpushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this);
    if(!fileName.isEmpty())
    {
        QFile file(fileName);
        if(!file.open(QFile::ReadOnly|QFile::Text)) //以只读方式打开文件,如果打开失败则返回,弹出对话框
        {
           QMessageBox::warning(this,tr("读取文件"),tr("无法读取文件 %1:\n %2.").arg(fileName).arg(file.errorString()));
            return false;//%1和%2表示后面两个arg的值
        }
        QTextStream in(&file);//新建流对象,指向选定文件
       ui->sendtextkeylineEdit->setText(in.readAll());//将文件中所有的内容写到行编辑器中
        curFile = QFileInfo(fileName).canonicalFilePath();
        ui->sendtextpathlineEdit->setText(curFile);
        ui->sendtextpathlineEdit->setVisible(true);//将文本设置为可见,此句话必须放在return true前,放在其后就见不到了,因为return语句返回了,此句执行不到
        return true;
    }
}
void MainWindow::on_sendtextpushButton_clicked()
{
   myCom->write(ui->sendtextkeylineEdit->text().toAscii());
    //将行编辑器中文本转化成ascii码形式写入串口
}
void MainWindow::on_pushButton_clicked()
{
    ui->textBrowser->clear();
}
void MainWindow::on_pushButton_2_clicked()
{
    ui->sendMsglineEdit->clear();
}
void MainWindow::on_choose_positionpushButton_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,tr("另存为"),curFile);//获取文件名
    if(!fileName.isEmpty())//文件名不为空,则保存文件
    {
        saveFile(fileName);
    }
}
bool MainWindow::saveFile(const QString& fileName)//存储文件
{
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly|QFile::Text))
    //以只写方式打开文件,如果打开失败则返回,弹出对话框
    {
        QMessageBox::warning(this,tr("保存文件"),tr("无法保存文件 %1:\n %2").arg(fileName).arg(file.errorString()));
        return false;//%1和%2表示后面两个arg的值
    }
    QTextStream out(&file);//新建流对象,指向选定文件
    out<<ui->textBrowser->toPlainText();//将文本编辑器中的内容以纯文本的形式输出到流对象中
    curFile = QFileInfo(fileName).canonicalFilePath();//获取文件的标准路径
     ui->savedatalineEdit->setText(curFile);
   // isSaved = true;
    return true;
}
void MainWindow::on_action_W_triggered()
{
    QDialog *md = new QDialog(this);
    md->setWindowTitle(tr("关于作者"));
    QLabel *tell = new QLabel(md);
    QLabel *mpic = new QLabel(md);
    QVBoxLayout* layout = new QVBoxLayout(md);//新建一个垂直布局管理器,并将行编辑器和按钮加入
    layout->addWidget(tell);
    layout->addWidget(mpic);
    tell->setText(tr("罗晓亮,microliang,喜欢编程,尤爱Qt编程。\nQQ:995846665,Email:lifeliang@hotmail.com"));
    mpic->setPixmap(QPixmap("C:/icon/liang.jpg"));
    md->show();
}
void MainWindow::on_action_H_triggered()
{
    aboutu.show();
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Qt编写串口通信程序全程图文讲解(四)
Qt编写串口通信程序全程图文讲解
Qt----常用的基本组件按钮组(Buttons)常用方法
Linux下 QT 实现串口通讯小实例
线程+定时实现Linux下的Qt串口编程
QGroupBox
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服