当前位置:网站首页>QT serial port programming
QT serial port programming
2022-06-29 06:55:00 【Mr.codeee】
1. Classes used
need pro Add modules to QT += serialport
QSerialPort
Common interfaces :
// Set serial port number
void setPortName(const QString &name)
// set baud rate
bool setBaudRate(qint32 baudRate, Directions directions = AllDirections)
// set data bit
bool setDataBits(DataBits dataBits)
// Set check bit
bool setParity(Parity parity)
// Set stop bit
bool setStopBits(StopBits stopBits)
2. Run a screenshot

3. Source code
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>
namespace Ui {
class MainWidget;
}
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
private slots:
void on_btnOpen_clicked();
void slotReadRead();
private:
Ui::MainWidget *ui;
private:
QSerialPort *m_port = nullptr;
};
#endif // MAINWIDGET_H
#include "MainWidget.h"
#include "ui_MainWidget.h"
#include <QMessageBox>
#include <QDebug>
#include <QThread>
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->cbxSerialNum->addItem(info.portName());
}
QStringList baud;
baud.append("1800");
baud.append("2400");
baud.append("4800");
baud.append("9600");
baud.append("19200");
baud.append("38400");
baud.append("57600");
baud.append("115200");
baud.append("230400");
ui->cbxBuad->addItems(baud);
ui->cbxBuad->setCurrentText("115200");
m_port = new QSerialPort(this);
connect(m_port,&QSerialPort::readyRead,this,&MainWidget::slotReadRead);
}
MainWidget::~MainWidget()
{
delete ui;
}
void MainWidget::on_btnOpen_clicked()
{
if(m_port->isOpen())
{
m_port->close();
ui->btnOpen->setText(" open ");
return;
}
m_port->setPortName(ui->cbxSerialNum->currentText());
m_port->setBaudRate(ui->cbxBuad->currentText().toInt());
m_port->setDataBits((QSerialPort::DataBits)ui->cbxDataBit->currentText().toInt());
if(ui->cbxJybit->currentIndex() == 0)
{
m_port->setParity(QSerialPort::NoParity);
}
else
{
m_port->setParity((QSerialPort::Parity)(ui->cbxJybit->currentIndex()+1));
}
m_port->setStopBits((QSerialPort::StopBits)ui->cbxStopBit->currentIndex());
if(!m_port->open(QIODevice::ReadWrite))
{
QMessageBox::critical(NULL,"tips","serial open failed");
}
qDebug()<<"serial open success";
ui->btnOpen->setText(" close ");
}
// When data comes from the serial port , Trigger slot
void MainWidget::slotReadRead()
{
QByteArray resvBuffer;
resvBuffer = m_port->readAll();
qDebug()<<"strlen "<<resvBuffer.size();
qDebug()<<resvBuffer;
}
边栏推荐
- Hyperledger Fabric 2. X custom smart contract
- 关于DDNS
- Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future
- The annual technology inventory of cloud primitives was released, and it was the right time to ride the wind and waves
- About DDNS
- Qt 程序打包发布-windeployqt工具
- [when OSPF introduces direct connection routes, it makes a summary by using static black hole routes]
- Vite quick start
- Single application and microservice application
- Antd work item memo w3.0
猜你喜欢

Json tobean

Easy to understand TCP four waves (multi picture explanation)

Are there too many programmers in China at present?

Introduction to Ceres Quartet

百度小程序自动提交搜索

Move disassembly of exclusive delivery of script (the first time)

How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)

How to fix Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorli

Yyds dry goods inventory meituan's two-sided experience, and finally there was a surprise?

Games101 Lecture 10 geometry 1 Notes
随机推荐
[Flink] flinksql and table programming cases
Fresnel diffraction with rectangular aperture based on MATLAB
Go basic data type conversion
Analytic hierarchy process
It is the only one in China that Alibaba cloud container service has entered the Forrester leader quadrant
Chapter IV introduction to FPGA development platform
解析学习幼儿机器人教育的浪潮
力扣每日一题-第30天-1523.位1的个数
力扣今日题-324. 摆动排序 II
JDBC连接数据库,socket发送客户端。
idea使用
数据库-同义词
[deep learning] - maze task learning I (to realize the random movement of agents)
Qt 容器类
Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future
2022.02.15 - SX10-31. House raiding III
String and variable are spliced into variable name
Some thoughts on port forwarding program
Li Kou today's question -324 Swing sort II
JDBC | Chapter 6: simple use of database connection pool