QT5.11串列埠除錯助手
阿新 • • 發佈:2018-12-19
pro
#------------------------------------------------- # # Project created by QtCreator 2018-10-31T21:14:36 # #------------------------------------------------- QT += core gui QT += serialport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = uart 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 \ widget.cpp HEADERS += \ widget.h FORMS += \ widget.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QString> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_OpenButton_clicked(); void on_OutputText_Dispaly(); void on_Send_clicked(); private: Ui::Widget *ui; QSerialPort *serial; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); //查詢可用的串列埠 foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) { QSerialPort serial; serial.setPort(info); if(serial.open(QIODevice::ReadWrite)) { ui->PortBox->addItem(serial.portName()); serial.close(); } } } Widget::~Widget() { delete ui; } void Widget::on_OpenButton_clicked() { if(ui->OpenButton->text() == tr("Open")) { serial = new QSerialPort; serial->setPortName(ui->PortBox->currentText()); serial->open(QIODevice::ReadWrite); if(ui->BaudBox->currentText() == tr("9600")) { serial->setBaudRate(QSerialPort::Baud9600); } else if(ui->BaudBox->currentText() == tr("19200")) { serial->setBaudRate(QSerialPort::Baud19200); } else if(ui->BaudBox->currentText() == tr("38400")) { serial->setBaudRate(QSerialPort::Baud38400); } else if(ui->BaudBox->currentText() == tr("115200")) { serial->setBaudRate(QSerialPort::Baud115200); } serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->setFlowControl(QSerialPort::NoFlowControl); ui->PortBox->setEnabled(false); ui->BaudBox->setEnabled(false); ui->OpenButton->setText("Close"); //連線訊號槽 當串列埠緩衝區有資料時,進行讀串列埠操作 QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(on_OutputText_Dispaly())); } else { serial->clear(); serial->close(); serial->deleteLater(); ui->PortBox->setEnabled(true); ui->BaudBox->setEnabled(true); ui->OpenButton->setText("Open"); } } void Widget::on_Send_clicked() { serial->write(ui->SendEdit->toPlainText().toLatin1()); } void Widget::on_OutputText_Dispaly() { QByteArray buf; buf = serial->readAll(); if(!buf.isEmpty()) { QString str = ui->OutputText->toPlainText(); str += tr(buf); ui->OutputText->clear(); ui->OutputText->append(str); } buf.clear(); }
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>478</width>
<height>282</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>串列埠設定</string>
</property>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QTextEdit" name="SendEdit"/>
</item>
<item row="0" column="1">
<widget class="QTextEdit" name="OutputText"/>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QLabel" name="Port">
<property name="text">
<string>串列埠</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="PortBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="Baud">
<property name="text">
<string>波特率</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="BaudBox">
<item>
<property name="text">
<string>9600</string>
</property>
</item>
<item>
<property name="text">
<string>19200</string>
</property>
</item>
<item>
<property name="text">
<string>38400</string>
</property>
</item>
<item>
<property name="text">
<string>115200</string>
</property>
</item>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="Baud_2">
<property name="text">
<string>資料位</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="BaudBox_2">
<item>
<property name="text">
<string>8</string>
</property>
</item>
<item>
<property name="text">
<string>7</string>
</property>
</item>
<item>
<property name="text">
<string>6</string>
</property>
</item>
<item>
<property name="text">
<string>5</string>
</property>
</item>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="Baud_3">
<property name="text">
<string>校驗位</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="PortBox_2">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="Baud_4">
<property name="text">
<string>停止位</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="PortBox_3">
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="Baud_5">
<property name="text">
<string>流控</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QComboBox" name="PortBox_4">
<item>
<property name="text">
<string>None</string>
</property>
</item>
</widget>
</item>
<item row="6" column="0">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="7" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="8" column="0">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="9" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="10" column="0">
<widget class="QPushButton" name="OpenButton">
<property name="text">
<string>Open</string>
</property>
</widget>
</item>
<item row="10" column="1">
<widget class="QPushButton" name="Send">
<property name="text">
<string>Send</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>