QT制作串口工具和源码

main.cpp文件

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include <QDir>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // QLoggingCategory::defaultCategory()->setEnabled(QtDebugMsg, true);
    //获取串口信息
    QList<QSerialPortInfo> list = QSerialPortInfo::availablePorts();

    //bpsList.clear();
    //bpsList.append(QString("无串口"));
    //ui->comboBox->addItem(bpsList.at(0));

    foreach (QSerialPortInfo info, list){
        QString Name;
        QString DEC;
        //打印串口名字
        qDebug() << info.portName();
        qDebug() << info.description();
        qDebug() << info.serialNumber();
        qDebug() << info.productIdentifier();

        //显示串口信息
        Name = info.portName();
        /*DEC = info.description();
        if(DEC.indexOf("UART") > 0)
        {
            Name += '(';
            Name += "UART";
            Name += ')';
        }*/

        ui->comboBox->addItem(Name);
    }

    bpsList.clear();
    bpsList.append(QString("100000"));
    bpsList.append(QString("460800"));
    bpsList.append(QString("230400"));
    bpsList.append(QString("115200"));
    bpsList.append(QString("38400"));
    bpsList.append(QString("57600"));
    bpsList.append(QString("9600"));
    for (int i = 0; i < 7; i++) {
        ui->comboBox_2->addItem(bpsList.at(i));
    }

    dataList.clear();
    dataList.append(QString("8bits"));
    dataList.append(QString("6bits"));
    dataList.append(QString("5bits"));
    for (int i = 0; i < 3; i++) {
        ui->comboBox_3->addItem(dataList.at(i));
    }

    parityList.clear();
    parityList.append(QString("NONE"));
    parityList.append(QString("EVEN"));
    parityList.append(QString("ODD"));
    for (int i = 0; i < 3; i++) {
        ui->comboBox_4->addItem(parityList.at(i));
    }

    stopList.clear();
    stopList.append(QString("one"));
    stopList.append(QString("two"));
    for (int i = 0; i < 2; i++) {
        ui->comboBox_5->addItem(stopList.at(i));
    }

    setWindowTitle("串口工具");
    ui->label->setText(QString("串口工具"));
    //qDebug()<<"串口工具 debug"<<endl;

    openFlag = false;
    connect(&port, SIGNAL(readyRead()), this, SLOT(readDataHandler()));
}

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

void Widget::readDataHandler()
{
    QByteArray ba = port.readAll();
    ui->textEdit->append(QString(ba.toStdString().c_str()));

    QString Uart_data = QString(ba.toStdString().c_str());

    //QString Temp = ;

   uint32_t pre =  Uart_data.indexOf("BARO   :");


    if(pre < 100)
    {
        QString Pressure = Uart_data.mid(pre + 8, 6);

        QString Temp = Uart_data.mid(pre + 15);
        uint32_t te  = Temp.indexOf("TEMP   :");
        QString Temp1 = Temp.mid(te + 8, 3);

        bool ok;
        double Pressurenum = double(Pressure.toInt(&ok,16));
        Pressurenum = Pressurenum / 4096;
        QString Pressurenum1 = QString("%1").arg(Pressurenum);

        double Tempnum = double(Temp1.toInt(&ok,16));
        Tempnum = Tempnum / 100;
        Temp = QString("%1").arg(Tempnum);

        ui->textEdit_2->append("Pressure:" + Pressurenum1);
        ui->textEdit_2->append("Temp:" + Temp);
    }
}

void Widget::on_pushButton_clicked()
{
    if (openFlag == false) {
        port.setPortName(ui->comboBox->currentText());
        if (port.open(QIODevice::ReadWrite)) {
            port.setBaudRate(ui->comboBox_2->currentText().toInt());
            switch (ui->comboBox_3->currentIndex()) {
            case 0:
                port.setDataBits(QSerialPort::Data8);
                break;
            case 1:
                port.setDataBits(QSerialPort::Data6);
                break;
            case 2:
                port.setDataBits(QSerialPort::Data5);
                break;
            default:
                break;
            }

            switch (ui->comboBox_4->currentIndex()) {
            case 0:
                port.setParity(QSerialPort::NoParity);
                break;
            case 1:
                port.setParity(QSerialPort::EvenParity);
                break;
            case 2:
                port.setParity(QSerialPort::OddParity);
                break;
            default:
                break;
            }

            switch (ui->comboBox_5->currentIndex()) {
            case 0:
                port.setStopBits(QSerialPort::OneStop);
                break;
            case 1:
                port.setStopBits(QSerialPort::TwoStop);
                break;
            default:
                break;
            }

            openFlag = true;
            ui->pushButton->setText(QString("关闭串口"));
        }
    } else {
        port.close();
        openFlag = false;
        ui->pushButton->setText(QString("打开串口"));

    }
    //qDebug() << ui->comboBox->currentText();
}

void Widget::on_pushButton_2_clicked()
{
    port.write(ui->textEdit_2->toPlainText().toLatin1());
}

void Widget::on_clear_data_clicked()
{
    ui->textEdit->clear();
    ui->textEdit_2->clear();
}

void Widget::on_Save_Date_clicked()
{
    QString Save_date = ui->textEdit->toPlainText();
    QString Save_date1 = ui->textEdit_2->toPlainText();


    QString dirName = "C:/Users/Administrator/Desktop/串口工具";
    QDir dir(dirName);
    if(!dir.exists())
    {
        ui->textEdit->append("创建串口工具文件夹成功");
        dir.mkdir(dirName);
    }
    else
    {
        ui->textEdit->append("串口工具文件夹已存在");
    }

    QString creathPath = "C:/Users/Administrator/Desktop/串口工具/lps22hh.c";

    QFileInfo fileInfo(creathPath);
    if(fileInfo.isFile())
    {
        QFile::remove(creathPath);//删除文件
        ui->textEdit->append("lps22hh.c文件已删除");
    }
    else
    {

    }

    QFile creathfile(creathPath);
    if(!creathfile.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
    {
        ui->textEdit->append("lps22hh.c文件打开失败");
    }
    creathfile.write(Save_date.toUtf8());
    creathfile.close();
    ui->textEdit->append("lps22hh.c文件保存成功");


    creathPath = "C:/Users/Administrator/Desktop/串口工具/lps22hh整理后数据.c";
    QFileInfo fileInfo1(creathPath);
    if(fileInfo1.isFile())
    {
        QFile::remove(creathPath);//删除文件
        ui->textEdit->append("lps22hh整理后数据.c文件已删除");
    }
    else
    {

    }

    QFile creathfile1(creathPath);
    if(!creathfile1.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
    {
        ui->textEdit->append("lps22hh整理后数据.c文件打开失败");
    }
    creathfile1.write(Save_date1.toUtf8());
    creathfile1.close();
    ui->textEdit->append("lps22hh整理后数据.c文件保存成功");
}

widget.h 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();
    void readDataHandler();

    void on_pushButton_2_clicked();

    void on_clear_data_clicked();

    void on_Save_Date_clicked();

private:
    Ui::Widget *ui;
    QList<QString> bpsList;
    QList<QString> dataList;
    QList<QString> parityList;
    QList<QString> stopList;

    bool openFlag;

    QSerialPort port;
};

#endif // WIDGET_H

 ui

 ui文件内容

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>636</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QFrame" name="frame">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>50</y>
     <width>201</width>
     <height>371</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Raised</enum>
   </property>
   <widget class="QComboBox" name="comboBox">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>20</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 15pt &quot;Ubuntu&quot;;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>24</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;Ubuntu&quot;;</string>
    </property>
    <property name="text">
     <string>串口名</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>64</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;Ubuntu&quot;;</string>
    </property>
    <property name="text">
     <string>波特率</string>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_2">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>60</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 15pt &quot;Ubuntu&quot;;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>104</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;Ubuntu&quot;;</string>
    </property>
    <property name="text">
     <string>数据位</string>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_3">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>100</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 15pt &quot;Ubuntu&quot;;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_5">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>144</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;Ubuntu&quot;;</string>
    </property>
    <property name="text">
     <string>校验位</string>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_4">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>140</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 15pt &quot;Ubuntu&quot;;</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_6">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>184</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 16pt &quot;Ubuntu&quot;;</string>
    </property>
    <property name="text">
     <string>停止位</string>
    </property>
   </widget>
   <widget class="QComboBox" name="comboBox_5">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>180</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 15pt &quot;Ubuntu&quot;;</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>220</y>
      <width>181</width>
      <height>51</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true">font: 18pt &quot;Ubuntu&quot;;
color: rgb(255, 255, 255);
background-color: rgb(206, 92, 0);</string>
    </property>
    <property name="text">
     <string>打开串口</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>280</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>十六进制显示</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>310</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>ASCII显示</string>
    </property>
   </widget>
   <widget class="QCheckBox" name="checkBox_3">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>340</y>
      <width>111</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>自动发送换行</string>
    </property>
   </widget>
  </widget>
  <widget class="QFrame" name="frame_2">
   <property name="geometry">
    <rect>
     <x>250</x>
     <y>140</y>
     <width>531</width>
     <height>321</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Raised</enum>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>811</width>
     <height>41</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 17pt &quot;Ubuntu&quot;;
color: rgb(255, 255, 255);
background-color: rgb(7, 54, 249);</string>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>430</y>
     <width>201</width>
     <height>71</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 18pt &quot;Ubuntu&quot;;
color: rgb(255, 255, 255);
background-color: rgb(206, 92, 0);</string>
   </property>
   <property name="text">
    <string>发送数据</string>
   </property>
  </widget>
  <widget class="QPushButton" name="clear_data">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>580</y>
     <width>201</width>
     <height>51</height>
    </rect>
   </property>
   <property name="text">
    <string>清除数据</string>
   </property>
  </widget>
  <widget class="QPushButton" name="Save_Date">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>510</y>
     <width>201</width>
     <height>51</height>
    </rect>
   </property>
   <property name="text">
    <string>保存数据</string>
   </property>
  </widget>
  <widget class="QTextEdit" name="textEdit_2">
   <property name="geometry">
    <rect>
     <x>220</x>
     <y>430</y>
     <width>581</width>
     <height>201</height>
    </rect>
   </property>
  </widget>
  <widget class="QTextEdit" name="textEdit">
   <property name="geometry">
    <rect>
     <x>220</x>
     <y>50</y>
     <width>581</width>
     <height>371</height>
    </rect>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

编译效果

源码链接:跳转链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值