QT的使用----串口的使用

#-------------------------------------------------
#
# Project created by QtCreator 2019-12-23T16:58:18
#
#-------------------------------------------------

QT       += core gui
QT       += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MyUart
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        main.cpp \
        myuart.cpp

HEADERS += \
        myuart.h

FORMS += \
        myuart.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MYUART_H
#define MYUART_H

#include <QWidget>
#include <QtSerialPort/QSerialPortInfo>
#include <QtSerialPort/QSerialPort>
#include <QComboBox>
#include <QVector>
namespace Ui {
class MyUart;
}

class MyUart : public QWidget
{
    Q_OBJECT

public:
    explicit MyUart(QWidget *parent = nullptr);
    ~MyUart();
    bool openserial(QComboBox *comboxname,QString comboud);
    void CloseSerial(void);
    void Seralopen(void);
    void SerialConfig(void);
    void SetWidgetEnable(bool);
    bool SerialIsOpen;
    QSerialPort *myserial;
signals:
    void serialinfo(QString tmp);
    void SendComData(QString ComData);
public slots:
    void serialRead();
private slots:
    void on_DevUartFlash_clicked();
    void on_DevSerialOpenCloseBtn_clicked();
private:
    Ui::MyUart *ui;
};

#endif // MYUART_H
#include "myuart.h"
#include "ui_myuart.h"
#include <QDebug>
#include <QMessageBox>
QVector<QString> SerialName_Vector3;
MyUart::MyUart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyUart)
{
    ui->setupUi(this);
    //串口下拉选项
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        SerialName_Vector3<<info.portName();
        ui->DevComPort->addItem(info.portName());
    }
    ui->DevComBoud->setCurrentText("38400");
}
void MyUart::serialRead()
{
    //qDebug()<<"serialRead"<<endl;
    QByteArray recvdata = myserial->readAll();
    QString RecvData=recvdata.toHex().data();
    qDebug()<<"收到串口数据:"<<RecvData;
    //qDebug()<<"RecvData:"<<RecvData<<endl;
    //qDebug()<<"RecvSize:"<<recvdata.count()<<endl;
    uint32_t s32Count = (uint32_t)recvdata.count();//s32Count是16进制的数不是获取到数据的长度?
    uint8_t* pu8Data = (uint8_t*)recvdata.data();
    emit SendComData(RecvData);
}
bool MyUart::openserial(QComboBox *comboxname,QString comboud)
{
    QString comname=comboxname->currentText();
    qDebug()<<"正在打开串口:%s"<<comname<<endl;
    myserial = new QSerialPort(comname);       //串口号,一定要对应好,大写!!!下面这些设置都是针对单片机11.0592的晶振的,但是串口号就要修改,假如不是这种情况,那么波特率之类很可能也要改!!
    //myserial->setPortName(comname);
    bool res=myserial->open(QIODevice::ReadWrite);      //读写打开
    if(res!=true)
    {
        qDebug()<<"打开串口失败:";
        QMessageBox box(QMessageBox::Warning,"提示","   串口打开失败\r\n请检查是否已选择端口!");
        box.setStandardButtons (QMessageBox::Ok);
        box.setButtonText (QMessageBox::Ok,QString("确 定"));
        box.exec ();
        return false;
    }
    qDebug()<<"打开串口:%s"<<comname<<endl;
    //波特率设置
    if(comboud=="1200")
    {
     myserial->setBaudRate(QSerialPort::Baud1200);  //波特率
    }
    else if(comboud=="2400")
    {
     myserial->setBaudRate(QSerialPort::Baud2400);  //波特率
    }
    else if(comboud=="4800")
    {
      myserial->setBaudRate(QSerialPort::Baud4800);  //波特率
    }
    else if(comboud=="9600")
    {
      myserial->setBaudRate(QSerialPort::Baud9600);  //波特率
    }
    else if(comboud=="19200")
    {
      myserial->setBaudRate(QSerialPort::Baud19200);  //波特率
    }
    else if(comboud=="38400")
    {
      myserial->setBaudRate(QSerialPort::Baud38400);  //波特率
    }
    else if(comboud=="57600")
    {
      myserial->setBaudRate(QSerialPort::Baud57600);  //波特率
    }
    else if(comboud=="11520000")
    {
      myserial->setBaudRate(QSerialPort::Baud115200);  //波特率
    }
    else
    {
      myserial->setBaudRate(QSerialPort::Baud38400);  //波特率
      comboud="38400";
      qWarning("波特率不存在,启用默认波特率38400");
    }
    qDebug()<<"当前波特率:"<<comboud;
    myserial->setDataBits(QSerialPort::Data8);     //数据位8
    myserial->setParity(QSerialPort::NoParity);    //无奇偶校验
    myserial->setStopBits(QSerialPort::OneStop);   //1位停止位
    myserial->setFlowControl(QSerialPort::NoFlowControl);  //无控制
    connect(myserial,SIGNAL(readyRead()),this,SLOT(serialRead())); //连接槽
    ui->DevSerialOpenCloseBtn->setText("关闭\n串口");
    return true;
}
void MyUart::SerialConfig(void)
{
    if(ui->DevSerialOpenCloseBtn->text()=="打开\n串口")
    {
        bool openres=this->openserial(ui->DevComPort,ui->DevComBoud->currentText());
        if(openres==false)
        {
            qDebug()<<"open serial 1 error!";
            return;
        }
        qDebug()<<"open serial %s"<<ui->DevComPort->currentText()<<endl;
        ui->DevSerialOpenCloseBtn->setText("关闭\n串口");
        this->SerialIsOpen=1;
    }
}
void MyUart::CloseSerial()
{
    if (myserial->isOpen())
    {
        myserial->close();
    }
    ui->DevSerialOpenCloseBtn->setText("打开\n串口");
    this->SerialIsOpen=0;
}
void MyUart::SetWidgetEnable(bool en)
{
  ui->DevComBoud->setEnabled(en);
  ui->DevComPort->setEnabled(en);
  ui->DevUartFlash->setEnabled(en);
  ui->DevSerialOpenCloseBtn->setEnabled(en);
}
void MyUart::on_DevUartFlash_clicked()
{
    SerialName_Vector3.clear();
    ui->DevComPort->clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        SerialName_Vector3<<info.portName();
        ui->DevComPort->addItem(info.portName());
    }
    ui->DevComBoud->setCurrentText("38400");
}
void MyUart::on_DevSerialOpenCloseBtn_clicked()
{
    if(ui->DevSerialOpenCloseBtn->text()=="打开\n串口")
    {
     openserial(ui->DevComPort,ui->DevComBoud->currentText());
    }
    else
    {
     CloseSerial();
    }
}
MyUart::~MyUart()
{
    delete ui;
}
#include "myuart.h"
#include <QApplication>

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

    return a.exec();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值