小言_互联网的博客

用Qt 5写一个串口收发桌面工具

235人阅读  评论(0)

今天用Qt写了一个串口收发工具,记录下整个流程。

1、项目工程布局

2、ui文件布局

3、widget.h文件


  
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QtSerialPort/QSerialPort>
  5. #include <QtSerialPort/QSerialPortInfo>
  6. #include <QTimer>
  7. QT_BEGIN_NAMESPACE
  8. namespace Ui { class Widget; }
  9. QT_END_NAMESPACE
  10. class Widget : public QWidget
  11. {
  12. Q_OBJECT
  13. public:
  14. Widget(QWidget *parent = nullptr);
  15. ~Widget();
  16. private:
  17. Ui::Widget *ui;
  18. QSerialPort *pSerialPort;
  19. QByteArray recvData;
  20. };
  21. #endif // WIDGET_H

4、widget.cpp文件


  
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QDebug>
  4. #include <QMessageBox>
  5. #include <QFileDialog>
  6. #include <QFile>
  7. #include <QTextStream>
  8. Widget::Widget(QWidget *parent)
  9. : QWidget(parent)
  10. , ui( new Ui::Widget)
  11. {
  12. ui->setupUi( this);
  13. ui->widget->setWindowTitle( "串口助手");
  14. pSerialPort = NULL;
  15. //枚举识别到的com口
  16. //写法一:
  17. /* foreach(QSerialPortInfo info,QSerialPortInfo::availablePorts())//读串口
  18. {
  19. QSerialPort port;
  20. port.setPort(info);
  21. if(port.open(QIODevice::ReadWrite))//如果串口可以读写的形式打开
  22. {
  23. ui->comboBox_portName->addItem(info.portName()); //显示出来
  24. port.close();//自动关闭等待人为打开
  25. }
  26. }*/
  27. //写法二
  28. QList<QSerialPortInfo> seriList = QSerialPortInfo::availablePorts();
  29. for( int i = 0;i <seriList.count();i++ )
  30. {
  31. QSerialPort port;
  32. port.setPort(seriList.at(i));
  33. if(port.open(QIODevice::ReadWrite))
  34. {
  35. ui->comboBox_portName->addItem(seriList.at(i).portName());
  36. port.close();
  37. }
  38. }
  39. //信号槽连接打开按钮和相关处理操作
  40. connect(ui->openBtn,&QPushButton::clicked,[=](){
  41. if(pSerialPort != NULL)
  42. {
  43. QMessageBox::warning( this, "警告", "已有活跃串口,请先关闭再打开");
  44. return;
  45. }
  46. pSerialPort = new QSerialPort( this);
  47. pSerialPort->setPortName(ui->comboBox_portName->currentText());
  48. pSerialPort->open(QIODevice::ReadWrite);
  49. pSerialPort->setBaudRate(ui->comboBox_bandRate->currentText().toInt());
  50. pSerialPort->setDataBits(QSerialPort::Data8);
  51. pSerialPort->setParity(QSerialPort::NoParity);
  52. pSerialPort->setFlowControl(QSerialPort::NoFlowControl);
  53. pSerialPort->setStopBits(QSerialPort::OneStop);
  54. connect(pSerialPort,&QSerialPort::readyRead, this,[=](){
  55. recvData = pSerialPort->readAll();
  56. if(!recvData.isEmpty())
  57. {
  58. ui->textEdit_read->append(recvData);
  59. }
  60. recvData.clear();
  61. });
  62. });
  63. //发送数据信号槽连接
  64. connect(ui->sendBtn,&QPushButton::clicked, this,[=](){
  65. if( NULL == pSerialPort )
  66. return;
  67. pSerialPort->write(ui->lineEdit_write->text().toUtf8());
  68. ui->lineEdit_write->clear();
  69. });
  70. //清空按钮信号槽连接
  71. connect(ui->clearBtn,&QPushButton::clicked, this,[=](){
  72. ui->textEdit_read->clear();
  73. });
  74. //关闭串口按钮信号槽连接
  75. connect(ui->closeBtn,&QPushButton::clicked, this,[=](){
  76. pSerialPort->close();
  77. delete pSerialPort;
  78. pSerialPort = NULL;
  79. });
  80. //保存按钮信号槽连接
  81. connect(ui->saveBtn,&QPushButton::clicked, this,[=](){
  82. if(ui->textEdit_read->document()->isEmpty())
  83. {
  84. QMessageBox::warning( this, "警告", "记录不能为空");
  85. return;
  86. }
  87. QString path = QFileDialog::getSaveFileName( this, "保存log记录", "log记录", "(*.txt)");
  88. qDebug()<<path;
  89. if (path.isEmpty())
  90. {
  91. QMessageBox::warning( this, "路径", "路径不能为空");
  92. return ;
  93. }
  94. QFile file(path);
  95. file.open(QIODevice::WriteOnly|QIODevice::Text);
  96. QTextStream stream(&file);
  97. stream<<ui->textEdit_read->toPlainText();
  98. file.close();
  99. });
  100. ui->lineEdit_write->setFocus();
  101. }
  102. Widget::~Widget()
  103. {
  104. delete ui;
  105. }

5、main.cpp 文件


  
  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

6、Serial.pro中要加入 serialport 模块

7.运行效果(电脑上外接串口,然后串口Tx和Rx短接,用作自发自收)

 


转载:https://blog.csdn.net/weixin_40204595/article/details/110433771
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场