• 大小: 24KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-18
  • 语言: 其他
  • 标签: QT5  串口通信  

资源简介

QT5串口通信,通过上位机对下位机进行幅值、频率等参数的调制。

资源截图

代码片段和文件信息

#include “control.h“
#include “ui_control.h“
#include 
#include 
#include 

control::control(QWidget *parent) :
    QMainWindow(parent)
    ui(new Ui::control)
{
    ui->setupUi(this);
    timeTimer = new QTimer(this);
    connect(timeTimer SIGNAL(timeout()) this SLOT(showTimeMessage()));
    connect(this->ui->btn_scan SIGNAL(clicked(bool)) this SLOT(scanPort()));
    connect(this->ui->btn_open SIGNAL(clicked(bool)) this SLOT(openPort()));
    connect(this->ui->btn_send SIGNAL(clicked(bool)) this SLOT(sendData()));
    timeTimer->start(1000);
    //设置默认值
    this->ui->cb_baud->setCurrentIndex(2);
    this->ui->cb_bit->setCurrentIndex(3);
    this->ui->btn_send->setEnabled(false);
    scanPort();
}

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

// 显示时间
void control::showTimeMessage(){

    QDateTime currentTime = QDateTime::currentDateTime();
    QString time = currentTime.toString(“yyyy-MM-dd hh:mm:ss  ddd“);
    this->ui->statusBar->showMessage(time);
}
//查找可用串口
void control::scanPort()
{
    this->ui->cb_port->clear();
    int isHavePort = 0;
    foreach(const QSerialPortInfo &info QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        //判断端口能否打开
        if(serial.open(QIODevice::ReadWrite))
        {
            this->ui->cb_port->addItem(serial.portName());
            isHavePort ++;
        }
        if(isHavePort == 0){
            QMessageBox::information(NULL “警告“ “没有可用串口“);
        }
    }
}

// 打开串口
void control::openPort()
{
    if(this->ui->btn_open->text() == tr(“打开串口“))
    {
        serial = new QSerialPort;
        //设置串口名
        serial->setPortName(this->ui->cb_port->currentText());
        //打开串口
        bool isOpenSucc = serial->open(QIODevice::ReadWrite);
        if(isOpenSucc)
        {
            //设置波特率
            switch(this->ui->cb_baud->currentIndex())
            {
                case 0:
                    serial->setBaudRate(QSerialPort::Baud2400);
                    break;
                case 1:
                    serial->setBaudRate(QSerialPort::Baud4800);
                    break;
                case 2:
                    serial->setBaudRate(QSerialPort::Baud9600);
                    break;
                case 3:
                    serial->setBaudRate(QSerialPort::Baud19200);
                    break;
                case 4:
                    serial->setBaudRate(QSerialPort::Baud38400);
                    break;
                case 5:
                    serial->setBaudRate(QSerialPort::Baud57600);
                    break;
                case 6:
                    serial->setBaudRate(QSerialPort::Baud115200);
                    break;
                default:
                    break;
            }

            //设置数据位
            switch(this->ui->cb_bit->currentIndex())
            {
                ca

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-10-25 10:16  FPGA_Controler\
     文件        9760  2018-10-24 20:59  FPGA_Controler\control.cpp
     文件         872  2018-10-23 11:11  FPGA_Controler\control.h
     文件       11443  2018-10-24 20:15  FPGA_Controler\control.ui
     文件         454  2018-10-19 21:20  FPGA_Controler\control_3.pro
     文件       23803  2018-10-24 22:12  FPGA_Controler\control_3.pro.user
     文件         839  2018-10-24 20:13  FPGA_Controler\control_3_resource.rc
     文件        4286  2018-10-15 19:55  FPGA_Controler\hit.ico
     文件         177  2018-10-14 15:29  FPGA_Controler\main.cpp
     文件       21658  2018-10-24 20:15  FPGA_Controler\Makefile
     文件       40925  2018-10-24 20:15  FPGA_Controler\Makefile.Debug
     文件       41004  2018-10-24 20:15  FPGA_Controler\Makefile.Release
     文件       17205  2018-10-24 20:15  FPGA_Controler\ui_control.h

评论

共有 条评论