Qt5.7下的串口编程

需要在pro文件添加

QT       += serialport

serial.h

#ifndef SERIAL_H
#define SERIAL_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class serial;
}

class serial : public QMainWindow
{
    Q_OBJECT

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


private slots:
    void on_send_clicked();

    void on_clear_clicked();

    void on_open_clicked();

    void read_date();


private:
    Ui::serial *ui;
    QSerialPort *serial_port_t;

    void baud_activated(const QString &arg1);

    void jiaoyan_activated(int arg1);

    void date_activated(const QString &arg1);

    void stop_activated(const QString &arg1);

    void liu_kong_zhi(int arg1);
};

#endif // SERIAL_H

serial.cpp

#include "serial.h"
#include "ui_serial.h"

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

    serial_port_t = new QSerialPort();

    connect(serial_port_t, &QSerialPort::readyRead, this, &serial::read_date);


    QList port = QSerialPortInfo::availablePorts();
    if(port.isEmpty())
        ui->com->addItem("无效");
    else
    {
        foreach(const QSerialPortInfo &port_info, port)
        {
            ui->com->addItem(port_info.portName());
        }
    }
}

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

void serial::read_date()
{
    QByteArray buf;
    buf = serial_port_t->readAll();
    QString s = buf;
    qDebug() << s << endl;
    ui->recv_text->append(s);
}

void serial::on_send_clicked()
{
    QString q = ui->send_text->toPlainText();
    QByteArray f = q.toLatin1();
    char *temp = f.data();
    serial_port_t->write(temp);
}

void serial::on_clear_clicked()
{
    ui->recv_text->clear();
}

void serial::on_open_clicked()
{
    QSerialPortInfo *port_info = new QSerialPortInfo(ui->com->currentText());
    serial_port_t->setPort(*port_info);

    if(!serial_port_t->open(QIODevice::ReadWrite))
        QMessageBox::warning(this,"警告","打开串口失败!");

    baud_activated(ui->baud->currentText()); //设置波特率
    jiaoyan_activated(ui->jiaoyan->currentIndex()); //设置奇偶校验
    date_activated(ui->date->currentText()); //设置数据位
    stop_activated(ui->stop->currentText()); //设置停止位
    liu_kong_zhi(ui->liukongzhi->currentIndex()); //设置流控制
}

void serial::baud_activated(const QString &arg1)
{
    switch(arg1.toInt())
    {
    case 1200:
        serial_port_t->setBaudRate(QSerialPort::Baud1200);
        break;
    case 2400:
        serial_port_t->setBaudRate(QSerialPort::Baud2400);
        break;
    case 9600:
        serial_port_t->setBaudRate(QSerialPort::Baud9600);
        break;
    case 38400:
        serial_port_t->setBaudRate(QSerialPort::Baud38400);
        break;
    case 115200:
        serial_port_t->setBaudRate(QSerialPort::Baud115200);
        break;
    }
}

void serial::jiaoyan_activated(int arg1)
{
    switch(arg1)
    {
    case 0:
        serial_port_t->setParity(QSerialPort::OddParity);
        break;
    case 1:
        serial_port_t->setParity(QSerialPort::EvenParity);
        break;
    case 2:
        serial_port_t->setParity(QSerialPort::NoParity);
        break;
    }
}

void serial::date_activated(const QString &arg1)
{
    switch(arg1.toInt())
    {
    case 5:
        serial_port_t->setDataBits(QSerialPort::Data5);
        break;
    case 6:
        serial_port_t->setDataBits(QSerialPort::Data6);
        break;
    case 7:
        serial_port_t->setDataBits(QSerialPort::Data7);
        break;
    case 8:
        serial_port_t->setDataBits(QSerialPort::Data8);
        break;
    }
}

void serial::stop_activated(const QString &arg1)
{
    switch(arg1.toInt())
    {
    case 1:
        serial_port_t->setStopBits(QSerialPort::OneStop);
        break;
    case 2:
        serial_port_t->setStopBits(QSerialPort::TwoStop);
        break;
    }
}

void serial::liu_kong_zhi(int arg1)
{
    switch(arg1)
    {
    case 0:
        serial_port_t->setFlowControl(QSerialPort::HardwareControl);
        break;
    case 1:
        serial_port_t->setFlowControl(QSerialPort::SoftwareControl);
        break;
    case 2:
        serial_port_t->setFlowControl(QSerialPort::NoFlowControl);
        break;
    }
}


运行效果:

Qt5.7下的串口编程_第1张图片

完整工程代码:

https://download.csdn.net/download/c1194758555/10530723


你可能感兴趣的:(Qt)