找传奇、传世资源到传世资源站!

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

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈


【文件目录】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() ); //当串口连接成功的时候才传温湿度值}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复