QT 串口
串口的实现。
1、在.pro文件内加入
QT += serialport
2、.h文件
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSerialPort> #include <QSerialPortInfo> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void readData(); void on_openSerialPort_clicked(); void on_sendData_clicked(); private: Ui::MainWindow *ui; QSerialPort *serial = nullptr; }; #endif // MAINWINDOW_H
.cpp文件
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { serial->setPort(info); qDebug() << "Name : " << info.portName(); qDebug() << "Description : " << info.description(); qDebug() << "Manufacturer: " << info.manufacturer(); qDebug() << "Serial Number: " << info.serialNumber(); qDebug() << "System Location: " << info.systemLocation(); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::readData() { QByteArray buf; buf = serial->readAll(); if(!buf.isEmpty()) { QString str = ui->textEdit->toPlainText(); str+=tr(buf); ui->textEdit->clear(); ui->textEdit->append(str); } buf.clear(); } void MainWindow::on_openSerialPort_clicked() { if(ui->openSerialPort->text() == "打开串口"){ serial = new QSerialPort; // 设置串口名 serial->setPortName(ui->comboBox->currentText()); // 打开串口 serial->open(QIODevice::ReadWrite); // 设置波特率 serial->setBaudRate(QSerialPort::Baud115200); // 设置数据位 switch (ui->comboBox_3->currentIndex()) { case 0: serial->setDataBits(QSerialPort::Data5); break; case 1: serial->setDataBits(QSerialPort::Data6); break; case 2: serial->setDataBits(QSerialPort::Data7); break; case 3: serial->setDataBits(QSerialPort::Data8); break; default: break; } // 设置校验位 switch (ui->comboBox_4->currentIndex()) { case 0: serial->setParity(QSerialPort::NoParity); break; case 1: serial->setParity(QSerialPort::OddParity); break; case 2: serial->setParity(QSerialPort::EvenParity); break; case 3: serial->setParity(QSerialPort::MarkParity); break; case 4: serial->setParity(QSerialPort::SpaceParity); break; default: break; } // 设置停止位 switch (ui->comboBox_5->currentIndex()) { case 0: serial->setStopBits(QSerialPort::OneStop); break; case 1: serial->setStopBits(QSerialPort::OneAndHalfStop); break; case 2: serial->setStopBits(QSerialPort::TwoStop); break; default: break; } // 设置流控制 serial->setFlowControl(QSerialPort::NoFlowControl); // 设置为无流控制 ui->comboBox->setEnabled(false); ui->comboBox_2->setEnabled(false); ui->comboBox_3->setEnabled(false); ui->comboBox_4->setEnabled(false); ui->comboBox_5->setEnabled(false); ui->openSerialPort->setText("关闭串口"); QObject::connect(serial, SIGNAL(readyRead()), this, SLOT(readData())); } else{ //关闭串口 serial->clear(); serial->close(); serial->deleteLater(); //恢复设置使能 ui->comboBox->setEnabled(true); ui->comboBox_2->setEnabled(true); ui->comboBox_3->setEnabled(true); ui->comboBox_4->setEnabled(true); ui->comboBox_5->setEnabled(true); ui->openSerialPort->setText(tr("打开串口")); } } void MainWindow::on_sendData_clicked() { serial->write(ui->textEdit_2->toPlainText().toLatin1()); }
ui的样式图