本文來源電子發(fā)燒友社區(qū),作者:劉建華, 帖子地址:https://bbs.elecfans.com/jishu_2308758_1_1.html
【目的】用QT實(shí)現(xiàn)一個串口助手。
因?yàn)槲业捻?xiàng)目是需要用到485接口來控制伺服電機(jī)的,所以掌握QT的串口收發(fā)是必需的。經(jīng)過幾天的學(xué)習(xí)從?Qt----Serial Port_冷月楓啊的博客-CSDN博客_qt serialport學(xué)習(xí)到了qtserialport的知識。現(xiàn)在分享如下:
1、用qtcreator新建空白項(xiàng)目:
2、在qtserial_demo.h上增加serialportQT += core gui serialport
3、編寫mainwindow.h,內(nèi)容如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
//Ui::MainWindow *ui;
/* 串口對象 */
QSerialPort *serialPort;
/* 用作接收數(shù)據(jù) */
QTextBrowser *textBrowser;
/* 用作發(fā)送數(shù)據(jù) */
QTextEdit *textEdit;
/* 按鈕 */
QPushButton *pushButton[2];
/* 下拉選擇盒子 */
QComboBox *comboBox[5];
/* 標(biāo)簽 */
QLabel *label[5];
/* 垂直布局 */
QVBoxLayout *vboxLayout;
/* 網(wǎng)絡(luò)布局 */
QGridLayout *gridLayout;
/* 主布局 */
QWidget *mainWidget;
/* 設(shè)置功能區(qū)域 */
QWidget *funcWidget;
/* 布局初始化 */
void layoutInit();
/* 掃描系統(tǒng)可用串口 */
void scanSerialPort();
/* 波特率項(xiàng)初始化 */
void baudRateItemInit();
/* 數(shù)據(jù)位項(xiàng)初始化 */
void dataBitsItemInit();
/* 檢驗(yàn)位項(xiàng)初始化 */
void parityItemInit();
/* 停止位項(xiàng)初始化 */
void stopBitsItemInit();
private slots:
void sendPushButtonClicked();
void openSerialPortPushButtonClicked();
void serialPortReadyRead();
};
#endif // MAINWINDOW_H
4、編寫mainwindow.cpp內(nèi)容如下:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/* 布局初始化 */
layoutInit();
/* 掃描系統(tǒng)的串口 */
scanSerialPort();
/* 波特率項(xiàng)初始化 */
baudRateItemInit();
/* 數(shù)據(jù)位項(xiàng)初始化 */
dataBitsItemInit();
/* 檢驗(yàn)位項(xiàng)初始化 */
parityItemInit();
/* 停止位項(xiàng)初始化 */
stopBitsItemInit();
}
void MainWindow::layoutInit()
{
/* 獲取屏幕的分辨率,Qt 官方建議使用這
* 種方法獲取屏幕分辨率,防上多屏設(shè)備導(dǎo)致對應(yīng)不上
* 注意,這是獲取整個桌面系統(tǒng)的分辨率
*/
QList list_screen = QGuiApplication::screens();
/* 如果是 ARM 平臺,直接設(shè)置大小為屏幕的大小 */
#if __arm__
/* 重設(shè)大小 */
this->resize(list_screen.at(0)->geometry().width(),
list_screen.at(0)->geometry().height());
#else
/* 否則則設(shè)置主窗體大小為 800x480 */
this->resize(800, 600);
#endif
/* 初始化 */
serialPort = new QSerialPort(this);
textBrowser = new QTextBrowser();
textEdit = new QTextEdit();
vboxLayout = new QVBoxLayout();
funcWidget = new QWidget();
mainWidget = new QWidget();
gridLayout = new QGridLayout();
/* QList 鏈表,字符串類型 */
QList list1;
list1<<"串口號:"<<"波特率:"<<"數(shù)據(jù)位:"<<"檢驗(yàn)位:"<<"停止位:";
for (int i = 0; i < 5; i++){
label[i] = new QLabel(list1[i]);
/* 設(shè)置最小寬度與高度 */
label[i]->setMinimumSize(80, 30);
/* 自動調(diào)整 label 的大小 */
label[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
/* 將 label[i]添加至網(wǎng)格的坐標(biāo)(0, i) */
gridLayout->addWidget(label[i], 0, i);
}
for (int i = 0; i < 5; i++) {
comboBox[i] = new QComboBox();
comboBox[i]->setMinimumSize(80, 30);
/* 自動調(diào)整 label 的大小 */
comboBox[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
/* 將 comboBox[i]添加至網(wǎng)格的坐標(biāo)(1, i) */
gridLayout->addWidget(comboBox[i], 1, i);
}
/* QList 鏈表,字符串類型 */
QList list2;
list2<<"發(fā)送"<<"打開串口";
for (int i = 0; i < 2; i++) {
pushButton[i] = new QPushButton(list2[i]);
pushButton[i]->setMinimumSize(80, 30);
/* 自動調(diào)整 label 的大小 */
pushButton[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
/* 將 pushButton[0]添加至網(wǎng)格的坐標(biāo)(i, 5) */
gridLayout->addWidget(pushButton[i], i, 5);
}
pushButton[0]->setEnabled(false);
/* 布局 */
vboxLayout->addWidget(textBrowser);
vboxLayout->addWidget(textEdit);
funcWidget->setLayout(gridLayout);
vboxLayout->addWidget(funcWidget);
mainWidget->setLayout(vboxLayout);
this->setCentralWidget(mainWidget);
/* 占位文本 */
textBrowser->setPlaceholderText("接收到的消息");
textEdit->setText("www.openedv.com");
/* 信號槽連接 */
connect(pushButton[0], SIGNAL(clicked()),
this, SLOT(sendPushButtonClicked()));
connect(pushButton[1], SIGNAL(clicked()),
this, SLOT(openSerialPortPushButtonClicked()));
connect(serialPort, SIGNAL(readyRead()),
this, SLOT(serialPortReadyRead()));
}
void MainWindow::scanSerialPort()
{
/* 查找可用串口 */
foreach (const QSerialPortInfo &info,
QSerialPortInfo::availablePorts()) {
comboBox[0]->addItem(info.portName());
}
}
void MainWindow::baudRateItemInit()
{
/* QList 鏈表,字符串類型 */
QList list;
list<<"1200"<<"2400"<<"4800"<<"9600"
<<"19200"<<"38400"<<"57600"
<<"115200"<<"230400"<<"460800"
<<"921600";
for (int i = 0; i < 11; i++) {
comboBox[1]->addItem(list[i]);
}
comboBox[1]->setCurrentIndex(7);
}
void MainWindow::dataBitsItemInit()
{
/* QList 鏈表,字符串類型 */
QList list;
list<<"5"<<"6"<<"7"<<"8";
for (int i = 0; i < 4; i++) {
comboBox[2]->addItem(list[i]);
}
comboBox[2]->setCurrentIndex(3);
}
void MainWindow::parityItemInit()
{
/* QList 鏈表,字符串類型 */
QList list;
list<<"None"<<"Even"<<"Odd"<<"Space"<<"Mark";
for (int i = 0; i < 5; i++) {
comboBox[3]->addItem(list[i]);
}
comboBox[3]->setCurrentIndex(0);
}
void MainWindow::stopBitsItemInit()
{
/* QList 鏈表,字符串類型 */
QList list;
list<<"1"<<"2";
for (int i = 0; i < 2; i++) {
comboBox[4]->addItem(list[i]);
}
comboBox[4]->setCurrentIndex(0);
}
void MainWindow::sendPushButtonClicked()
{
/* 獲取 textEdit 數(shù)據(jù),轉(zhuǎn)換成 utf8 格式的字節(jié)流 */
QByteArray data = textEdit->toPlainText().toUtf8();
serialPort->write(data);
}
void MainWindow::openSerialPortPushButtonClicked()
{
if (pushButton[1]->text() == "打開串口") {
/* 設(shè)置串口名 */
serialPort->setPortName(comboBox[0]->currentText());
/* 設(shè)置波特率 */
serialPort->setBaudRate(comboBox[1]->currentText().toInt());
/* 設(shè)置數(shù)據(jù)位數(shù) */
switch (comboBox[2]->currentText().toInt()) {
case 5:
serialPort->setDataBits(QSerialPort::Data5);
break;
case 6:
serialPort->setDataBits(QSerialPort::Data6);
break;
case 7:
serialPort->setDataBits(QSerialPort::Data7);
break;
case 8:
serialPort->setDataBits(QSerialPort::Data8);
break;
default: break;
}
/* 設(shè)置奇偶校驗(yàn) */
switch (comboBox[3]->currentIndex()) {
case 0:
serialPort->setParity(QSerialPort::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::EvenParity);
break;
case 2:
serialPort->setParity(QSerialPort::OddParity);
break;
case 3:
serialPort->setParity(QSerialPort::SpaceParity);
break;
case 4:
serialPort->setParity(QSerialPort::MarkParity);
break;
default: break;
}
/* 設(shè)置停止位 */
switch (comboBox[4]->currentText().toInt()) {
case 1:
serialPort->setStopBits(QSerialPort::OneStop);
break;
case 2:
serialPort->setStopBits(QSerialPort::TwoStop);
break;
default: break;
}
/* 設(shè)置流控制 */
serialPort->setFlowControl(QSerialPort::NoFlowControl);
if (!serialPort->open(QIODevice::ReadWrite))
QMessageBox::about(NULL, "錯誤",
"串口無法打開!可能串口已經(jīng)被占用!");
else {
for (int i = 0; i < 5; i++)
comboBox[i]->setEnabled(false);
pushButton[1]->setText("關(guān)閉串口");
pushButton[0]->setEnabled(true);
}
} else {
serialPort->close();
for (int i = 0; i < 5; i++)
comboBox[i]->setEnabled(true);
pushButton[1]->setText("打開串口");
pushButton[0]->setEnabled(false);
}
}
void MainWindow::serialPortReadyRead()
{
/* 接收緩沖區(qū)中讀取數(shù)據(jù) */
QByteArray buf = serialPort->readAll();
textBrowser->insertPlainText(QString(buf));
}
MainWindow::~MainWindow()
{
}
4、代碼帖完后編譯通過,在目錄工程的同級目錄下生成build_qtserial_demo-arm-debug文件夾。
5、將可執(zhí)行文件qtserial_demo拷到開發(fā)板
forlinx@forlinx:~/work/qtserial_demo/build-qtserial_demo-arm-Debug$ scp qtserial_demo root@192.168.3.168:/home/qtserial_demo
The authenticity of host '192.168.3.168 (192.168.3.168)' can't be established.
ECDSA key fingerprint is SHA256:WfJF3iuWeOnNl+KyM52kBLDLoRFcFhRDOm01a4NrZoA.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.3.168' (ECDSA) to the list of known hosts.
root@192.168.3.168's password:
qtserial_demo 100% 971KB 10.6MB/s 00:00
6、開發(fā)板上修改文件權(quán)限后執(zhí)行
-
飛凌
+關(guān)注
關(guān)注
0文章
134瀏覽量
16109
發(fā)布評論請先 登錄
相關(guān)推薦
評論