【目的】用QT实现一个串口助手。
因为我的项目是需要用到485接口来控制伺服电机的,所以掌握QT的串口收发是必需的。经过几天的学习从(68条消息) Qt----Serial Port_冷月枫啊的博客-CSDN博客_qt serialport学习到了qtserialport的知识。现在分享如下:
1、用qtcreator新建空白项目:
2、在qtserial_demo.h上增加serialport
QT += core gui serialport
3、编写mainwindow.h,内容如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGuiApplication>
#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QPushButton>
#include <QTextBrowser>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QGridLayout>
#include <QMessageBox>
#include <QDebug>
#include <QScreen>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QSerialPort *serialPort;
QTextBrowser *textBrowser;
QTextEdit *textEdit;
QPushButton *pushButton[2];
QComboBox *comboBox[5];
QLabel *label[5];
QVBoxLayout *vboxLayout;
QGridLayout *gridLayout;
QWidget *mainWidget;
QWidget *funcWidget;
void layoutInit();
void scanSerialPort();
void baudRateItemInit();
void dataBitsItemInit();
void parityItemInit();
void stopBitsItemInit();
private slots:
void sendPushButtonClicked();
void openSerialPortPushButtonClicked();
void serialPortReadyRead();
};
#endif
4、编写mainwindow.cpp内容如下:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
layoutInit();
scanSerialPort();
baudRateItemInit();
dataBitsItemInit();
parityItemInit();
stopBitsItemInit();
}
void MainWindow::layoutInit()
{
QList <QScreen *> list_screen = QGuiApplication::screens();
this->resize(list_screen.at(0)->geometry().width(),
list_screen.at(0)->geometry().height());
this->resize(800, 600);
serialPort = new QSerialPort(this);
textBrowser = new QTextBrowser();
textEdit = new QTextEdit();
vboxLayout = new QVBoxLayout();
funcWidget = new QWidget();
mainWidget = new QWidget();
gridLayout = new QGridLayout();
QList <QString> list1;
list1<<"串口号:"<<"波特率:"<<"数据位:"<<"检验位:"<<"停止位:";
for (int i = 0; i < 5; i++){
label[i] = new QLabel(list1[i]);
label[i]->setMinimumSize(80, 30);
label[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
gridLayout->addWidget(label[i], 0, i);
}
for (int i = 0; i < 5; i++) {
comboBox[i] = new QComboBox();
comboBox[i]->setMinimumSize(80, 30);
comboBox[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
gridLayout->addWidget(comboBox[i], 1, i);
}
QList <QString> list2;
list2<<"发送"<<"打开串口";
for (int i = 0; i < 2; i++) {
pushButton[i] = new QPushButton(list2[i]);
pushButton[i]->setMinimumSize(80, 30);
pushButton[i]->setSizePolicy(
QSizePolicy::Expanding,
QSizePolicy::Expanding
);
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 <QString> 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 <QString> 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 <QString> 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 <QString> list;
list<<"1"<<"2";
for (int i = 0; i < 2; i++) {
comboBox[4]->addItem(list[i]);
}
comboBox[4]->setCurrentIndex(0);
}
void MainWindow::sendPushButtonClicked()
{
QByteArray data = textEdit->toPlainText().toUtf8();
serialPort->write(data);
}
void MainWindow::openSerialPortPushButtonClicked()
{
if (pushButton[1]->text() == "打开串口") {
serialPort->setPortName(comboBox[0]->currentText());
serialPort->setBaudRate(comboBox[1]->currentText().toInt());
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;
}
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;
}
switch (comboBox[4]->currentText().toInt()) {
case 1:
serialPort->setStopBits(QSerialPort::OneStop);
break;
case 2:
serialPort->setStopBits(QSerialPort::TwoStop);
break;
default: break;
}
serialPort->setFlowControl(QSerialPort::NoFlowControl);
if (!serialPort->open(QIODevice::ReadWrite))
QMessageBox::about(NULL, "错误",
"串口无法打开!可能串口已经被占用!");
else {
for (int i = 0; i < 5; i++)
comboBox[i]->setEnabled(false);
pushButton[1]->setText("关闭串口");
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()
{
QByteArray buf = serialPort->readAll();
textBrowser->insertPlainText(QString(buf));
}
MainWindow::~MainWindow()
{
}
4、代码帖完后编译通过,在目录工程的同级目录下生成build_qtserial_demo-arm-debug文件夹。
5、将可执行文件qtserial_demo拷到开发板
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、开发板上修改文件权限后执行