赞
踩
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,串口通信这门技术也越来越重要,很多人都开启了学习串口,本文就介绍了串口通信的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
使用qt5的QSerialPort类,所有代码都在文中。
采用qt5 c++实现,
创建一个qt工程文件夹,名字命名为samp2_7,基类选择widget类。
//samp2_7.pro #------------------------------------------------- # # Project created by QtCreator 2022-07-18T17:43:55 # #------------------------------------------------- QT += core gui QT += serialport greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = samp2_7 TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as 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 SOURCES += \ main.cpp \ widget.cpp HEADERS += \ widget.h FORMS += \ widget.ui
//widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_openPortButton_clicked(); void read_com(); private: Ui::Widget *ui; QSerialPort serial1; }; #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); } Widget::~Widget() { delete ui; } void Widget::on_openPortButton_clicked() { //打开串口 if(ui->portNameBox->isEnabled()) //如果串口能够操作 { ui->openPortButton->setText("closePort"); //调用函数修改按钮显示为closePort ui->portNameBox->setDisabled(true); //禁止再修改COM串口 serial1.setPortName(ui->portNameBox->currentText()); //得到串口名,设置COM口 serial1.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections); //设置波特率和读写方向 serial1.open(QIODevice::ReadWrite); } else{ ui->openPortButton->setText("openPort"); //按下closeport之后,按键显示为openport ui->portNameBox->setEnabled(true); //按下closeport后,COM口可以修改 serial1.close(); //关闭串口 } } //槽函数,在槽函数之后使用readAll()把收到的数据读到requestData中。 void Widget::read_com() { QByteArray requestData; requestData = serial1.readAll(); QString buf; ui->plainTextEdit->appendPlainText(requestData.toHex()); }
//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>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Widget</string> </property> <widget class="QPlainTextEdit" name="plainTextEdit"> <property name="geometry"> <rect> <x>20</x> <y>10</y> <width>351</width> <height>181</height> </rect> </property> </widget> <widget class="QComboBox" name="portNameBox"> <property name="geometry"> <rect> <x>50</x> <y>230</y> <width>87</width> <height>22</height> </rect> </property> <item> <property name="text"> <string>1200</string> </property> </item> <item> <property name="text"> <string>2400</string> </property> </item> <item> <property name="text"> <string>4800</string> </property> </item> <item> <property name="text"> <string>9600</string> </property> </item> <item> <property name="text"> <string>12500</string> </property> </item> </widget> <widget class="QPushButton" name="openPortButton"> <property name="geometry"> <rect> <x>240</x> <y>230</y> <width>93</width> <height>28</height> </rect> </property> <property name="text"> <string>open</string> </property> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
生成的可执行文件:
提示:这里对文章进行总结:
以上就是今天要讲的内容,本文仅仅简单介绍了qt串口组件的简单使用,而qt提供了大量能使我们快速便捷地处理串口数据的函数和方法。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。