在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例Android手机应用开发 → qt上位机采集51单片机温湿度数据

qt上位机采集51单片机温湿度数据

Android手机应用开发

下载此实例
  • 开发语言:C/C++
  • 实例大小:1.68M
  • 下载次数:28
  • 浏览次数:228
  • 发布时间:2021-01-21
  • 实例类别:Android手机应用开发
  • 发 布 人:hfoon
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 51单片机 上位机 湿度 数据 Qt

实例介绍

【实例简介】

【实例截图】

【文件目录】

TemAndHum_sys

├── Debug
│   ├── Makefile
│   ├── Makefile.Debug
│   ├── Makefile.Release
│   ├── debug
│   │   ├── TemAndHum_sys.exe
│   │   ├── humi.o
│   │   ├── main.o
│   │   ├── moc_humi.cpp
│   │   ├── moc_humi.o
│   │   ├── moc_predefs.h
│   │   ├── moc_temp.cpp
│   │   ├── moc_temp.o
│   │   ├── moc_test.cpp
│   │   ├── moc_test.o
│   │   ├── moc_widget.cpp
│   │   ├── moc_widget.o
│   │   ├── temp.o
│   │   ├── test.o
│   │   └── widget.o
│   └── release
├── TemAndHum_sys.pro
├── TemAndHum_sys.pro.user
├── TemAndHum_sys.pro.user.0252fe0.22
├── TemAndHum_sys.pro.user.3e2c4c7.4.8-pre1
├── humi.cpp
├── humi.h
├── main.cpp
├── temp.cpp
├── temp.h
├── widget.cpp
└── widget.h
3 directories, 29 files


【核心代码】
#include "widget.h"

#include <QPushButton>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QRect>
#include <QLabel>
#include <QTextEdit>
#include <QComboBox>
#include <QStringList>
#include <QFont>
#include <QPalette>
#include <QDebug>
#include <QByteArray>

#include "temp.h"
#include "humi.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    tabwidget = new QTabWidget(this);

    QWidget *widget1 = new QWidget(this);
    QWidget *widget2 = new QWidget(this);

    QFont font; //设置字体属性
    font.setBold(true);
    font.setPixelSize(20);
    QPalette palette; //使用调色板设置颜色属性
    palette.setColor(QPalette::WindowText,QColor(255,0,0));

    QLabel *label_IOset = new QLabel("\n串口设置");
    label_IOset->setFont(font);
    label_IOset->setPalette(palette);
    QLabel *label_TemHumdata = new QLabel("\n温湿度数据");
    label_TemHumdata->setFont(font);
    label_TemHumdata->setPalette(palette);

    QLabel *label_IOnum = new QLabel("1.串口号");
    QLabel *label_Baud = new QLabel("2.波特率");
    QLabel *label_Checkbit = new QLabel("3.校验位");
    QLabel *label_Databit = new QLabel("4.数据位");
    QLabel *label_Stopbit = new QLabel("5.停止位");

    combo_IOnum = new QComboBox(this);   combo_IOnum->setEditable(true); //可设置
    combo_Baud = new QComboBox(this);    combo_Baud->setEditable(true);
    combo_Checkbit = new QComboBox(this);combo_Checkbit->setEditable(true);
    combo_Databit = new QComboBox(this); combo_Databit->setEditable(true);
    combo_Stopbit = new QComboBox(this); combo_Stopbit->setEditable(true);

    QStringList list_IOnum;
    list_IOnum << "COM1" << "COM2" << "COM3" << "COM4" << "COM5" << "COM6" << "COM7" << "COM8" << "COM9";
    combo_IOnum->addItems(list_IOnum);

    QStringList list_Baud;
    list_Baud << "115200" << "57600" << "56000" << "38400" << "19200" << "14400" << "9600" << "4800" << "2400";
    combo_Baud->addItems(list_Baud);

    QStringList list_Checkbit;
    list_Checkbit << "NONE" << "ODD" << "EVEN";
    combo_Checkbit->addItems(list_Checkbit);

    QStringList list_Databit;
    list_Databit << "8bit" << "7bit" << "6bit" << "5bit";
    combo_Databit->addItems(list_Databit);

    QStringList list_Stopbit;
    list_Stopbit << "1bit" << "1.5bit" << "2bit";
    combo_Stopbit->addItems(list_Stopbit);

    QHBoxLayout *layout_IOset = new QHBoxLayout;
    QHBoxLayout *layout_IOnum = new QHBoxLayout;
    QHBoxLayout *layout_Baud = new QHBoxLayout;
    QHBoxLayout *layout_Checkbit = new QHBoxLayout;
    QHBoxLayout *layout_Databit = new QHBoxLayout;
    QHBoxLayout *layout_Stopbit = new QHBoxLayout;
    QHBoxLayout *layout_Button = new QHBoxLayout;

    connetButton = new QPushButton("连接");
    connetButton->setStyleSheet("QPushButton{ \
                                font-family:'Microsoft YaHei';font-size:20px;color:#009FCC;\
                                            }");
    offButton = new QPushButton("断开");
    //offButton->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:20px;color:#009FCC;}");
    offButton->setEnabled(false);
    if(offButton->isEnabled() == false){ //判断获得button是否可被点击
        offButton->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:20px;color:#888888;}");
    }

    //将label和comboBox先用水平布局
    layout_IOset->addWidget(label_IOset);
    layout_IOnum->addWidget(label_IOnum);       layout_IOnum->addWidget(combo_IOnum);       layout_IOnum->setSpacing(25);
    layout_Baud->addWidget(label_Baud);         layout_Baud->addWidget(combo_Baud);         //layout_Baud->setSpacing(40);
    layout_Checkbit->addWidget(label_Checkbit); layout_Checkbit->addWidget(combo_Checkbit); //layout_Checkbit->setSpacing(30);
    layout_Databit->addWidget(label_Databit);   layout_Databit->addWidget(combo_Databit);   //layout_Databit->setSpacing(30);
    layout_Stopbit->addWidget(label_Stopbit);   layout_Stopbit->addWidget(combo_Stopbit);   //layout_Stopbit->setSpacing(30);
    layout_Button->addWidget(connetButton);     layout_Button->addWidget(offButton);        layout_Button->setSpacing(45);

    //再将水平布局用垂直布局排版
    QVBoxLayout *vlayout = new QVBoxLayout;
    vlayout->addLayout(layout_IOset);
    vlayout->addLayout(layout_IOnum);
    vlayout->addLayout(layout_Baud);
    vlayout->addLayout(layout_Checkbit);
    vlayout->addLayout(layout_Databit);
    vlayout->addLayout(layout_Stopbit);
    vlayout->addLayout(layout_Button);
    vlayout->setSpacing(25);
    vlayout->addStretch();

    //水平布局左边是串口设置,右边是温湿度数据
    edit = new QTextEdit(this);
    edit->setStyleSheet("QTextEdit{ font-size:20px; }");

    QVBoxLayout *vlayout1 = new QVBoxLayout;
    vlayout1->addWidget(label_TemHumdata);
    vlayout1->addWidget(edit);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addLayout(vlayout);
    layout->addLayout(vlayout1);
    layout->setSpacing(25);

    widget1->setLayout(layout);

    //1.
    tabwidget->addTab(widget1,"串口连接");

//******************************************************************

    mytemp = new Temp();
    myhumi = new Humi();

    QVBoxLayout *seriesLayout =  new QVBoxLayout(this);
    //seriesLayout->addWidget(mytemp);
    seriesLayout->addWidget(myhumi);
    widget2->setLayout(seriesLayout);
    //2.
    tabwidget->addTab(widget2,"温湿度监控曲线");

    QHBoxLayout *hboxlayout = new QHBoxLayout(this); //水平布局
    hboxlayout->addWidget(tabwidget);
    hboxlayout->setMargin(0);
    hboxlayout->setSpacing(0);
    //hboxlayout->addStretch(); //弹簧分割
    this->setLayout(hboxlayout);
    this->resize(1366,780);
    this->setWindowTitle("简易温湿度监控系统 (by:Hfoon 2020/4/27)");
    this->setStyleSheet("background-color:#FFFAF0;"); //样式表,这里使用QColor样式表的色卡不准
    QPalette pal;
    pal.setBrush(QPalette::Background,QColor(48,54,64));
    //this->setPalette(pal);

    //信号槽函数
    connect(connetButton,SIGNAL(clicked()),this,SLOT(onConnetButtonclicked()));
    connect(offButton,SIGNAL(clicked()),this,SLOT(onOffButtonclicked()));

    serialport = new QSerialPort(this);

    connect(serialport,SIGNAL(readyRead()),this,SLOT(onSerialreadyRead())); //读取缓冲区数据,readyRead()读缓冲区信号

    localtime = new QDateTime;

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(onTimerOut()));



}


Widget::~Widget()
{


}

void Widget::setTempvalue(QString value)
{
    tempvalue = value;
}

QString Widget::getTempvalue()
{
    return tempvalue;
}

void Widget::onConnetButtonclicked()
{
    qDebug() << "连接成功!!!!";

    choiceport = combo_IOnum->currentText();
    qDebug() << "PortName::" << choiceport;
    serialport->setPortName(choiceport);    //设置串口号
    if(serialport->open(QIODevice::ReadWrite)){

        offButton->setEnabled(true);
        if(offButton->isEnabled() == true){ //判断获得button是否可被点击
            offButton->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:20px;color:#009FCC;}");
            connetButton->setEnabled(false);
            connetButton->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:20px;color:#888888;}");
        }

        edit->append("********************* \n串口连接成功!!! \n串口号为:" choiceport "\n*********************");

        timer->start(2000);
        myhumi->timeStart();

        //设置波特率
        serialport->setBaudRate(combo_Baud->currentText().toInt());
        serialport->setFlowControl(QSerialPort::NoFlowControl);
        qDebug() << "BaudRate::" << combo_Baud->currentText().toInt();

        //检验位
        QString parity = combo_Checkbit->currentText();
        qDebug() << "Parity::" << parity;
        if(parity == "NONE"){
            serialport->setParity(QSerialPort::NoParity);
        }
        if(parity == "ODD"){
            serialport->setParity(QSerialPort::OddParity);
        }
        if(parity == "EVEN"){
            serialport->setParity(QSerialPort::EvenParity);
        }

        //数据位
        QString databits = combo_Databit->currentText();
        qDebug() << "DataBits::" << databits;
        if(databits == "8bit"){
            serialport->setDataBits(QSerialPort::Data8);
        }
        if(databits == "7bit"){
            serialport->setDataBits(QSerialPort::Data7);
        }
        if(databits == "6bit"){
            serialport->setDataBits(QSerialPort::Data6);
        }
        if(databits == "5bit"){
            serialport->setDataBits(QSerialPort::Data5);
        }

        //停止位
        QString stopbits = combo_Stopbit->currentText();
        qDebug() << "StopBits::" << stopbits;
        if(stopbits == "1bit"){
            serialport->setStopBits(QSerialPort::OneStop);
        }
        if(stopbits == "1.5bit"){
             serialport->setStopBits(QSerialPort::OneAndHalfStop);
        }
        if(stopbits == "2bit"){
             serialport->setStopBits(QSerialPort::TwoStop);
        }

    }

}

int Widget::onOffButtonclicked( )
{

    serialport->clear();
    serialport->close();

    timer->stop();

    mytemp->timeStop(); //同时温湿度的折线定时也要stop()
    myhumi->timeStop();

    edit->append("***串口关闭!!!");

    offButton->setEnabled(false);
    offButton->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:20px;color:#888888;}");

    connetButton->setEnabled(true);
    connetButton->setStyleSheet("QPushButton{ font-family:'Microsoft YaHei';font-size:20px;color:#009FCC;}");

}

void Widget::onSerialreadyRead()
{
    QString gettime = localtime->currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    QString readdata = serialport->readAll();
    if(readdata != NULL){
        edit->append(gettime);
        edit->append(readdata);
        qDebug() << "readdata::" << readdata << "\n";

        QStringList strlist = readdata.split("\r\n");

        QString humi = strlist[0];
        //qDebug() << humi.mid(5);

        QString temp = strlist[1];
        //qDebug() << temp.mid(5,9);

        setTempvalue( temp.mid(5,9) );

        emit tempSignal( temp.mid(5,9) );
    }

    readdata.clear();
}

void Widget::onTimerOut()
{
    static int counttimer = 0;
    counttimer ;
    qDebug() << "counttimer::" << counttimer;

    if(counttimer > 300){ //每过5分钟textedit清除历史记录
        edit->clear();
        counttimer = NULL;  //重新设置开始时间要在TimerOut()函数中,因为计数 在此!
    }


    myhumi->setTemp( getTempvalue() ); //当串口连接成功的时候才传温湿度值
}


实例下载地址

qt上位机采集51单片机温湿度数据

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警