当前位置:网站首页>轮子六:QSerialPort 串口数据 收发
轮子六:QSerialPort 串口数据 收发
2022-07-28 20:56:00 【老赵的博客】
// .h
#ifndef TIMINGSYSWORKER_H
#define TIMINGSYSWORKER_H
#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include "setting/setting.h"
#include<qthread.h>
class TimingSysWorker : public QObject
{
Q_OBJECT
public:
explicit TimingSysWorker(QObject *parent = nullptr);
~TimingSysWorker();
void setPortName(const QString& strName);
void setBaudrate(int nBaudrate);
signals:
void sig_recv(QByteArray ba);
void serialOpenRes(int deviceId,bool);//串口打开结果
public slots:
//初始化并打开串口
void slot_initSerial(int deviceId);
void slot_readReady();
void slot_sendcmd(QByteArray);
private:
QSerialPort* m_pQSerialPort;
//串口名
QString m_strPortName;
//波特率
int m_nBaudrate;
bool m_isOpen = false;
QByteArray m_baBuff;
};
#endif // TIMINGSYSWORKER_H
// .cpp
#include "TimingSysWorker.h"
#include <QDebug>
TimingSysWorker::TimingSysWorker(QObject *parent) : QObject(parent)
{
m_pQSerialPort = nullptr;
}
TimingSysWorker::~TimingSysWorker()
{
if(m_pQSerialPort)
{
if(m_pQSerialPort->isOpen()){
m_pQSerialPort->close();
delete m_pQSerialPort;
m_pQSerialPort=nullptr;
}
}
}
void TimingSysWorker::setBaudrate(int nBaudrate)
{
m_nBaudrate = nBaudrate;
}
void TimingSysWorker::setPortName(const QString &strName)
{
m_strPortName = strName;
}
void TimingSysWorker::slot_initSerial(int deviceId)
{
if(nullptr==m_pQSerialPort)
{
m_pQSerialPort = new QSerialPort;
}
bool bExist=false;
foreach (const QSerialPortInfo&info, QSerialPortInfo::availablePorts())
{
if(info.portName()==m_strPortName)
{
m_pQSerialPort->setPortName(info.systemLocation());
bExist=true;
}
}
if(!bExist)
{
qDebug()<<QStringLiteral("串口%1不存在").arg(m_strPortName);
}
if(m_pQSerialPort)
{
//m_pQSerialPort->setRequestToSend(true);//设置422串口全双工通信,这个串口默认不是全双工,导致只能收,不能发
connect(m_pQSerialPort,&QSerialPort::readyRead,this,&TimingSysWorker::slot_readReady);
//设置波特率
m_pQSerialPort->setBaudRate(m_nBaudrate,QSerialPort::AllDirections);
//设置数据位
m_pQSerialPort->setDataBits(QSerialPort::Data8);
//设置校验位
m_pQSerialPort->setParity(QSerialPort::NoParity);
//设置停止位
m_pQSerialPort->setStopBits(QSerialPort::OneStop);
//流量控制
m_pQSerialPort->setFlowControl(QSerialPort::NoFlowControl);
bool bRet = m_pQSerialPort->open(QIODevice::ReadWrite);//打开串口并选择读写模式
if(bRet)
{
m_isOpen = true;
m_pQSerialPort->setRequestToSend(true);//设置串口全双工通信,这个串口默认不是全双工,导致只能收,不能发。
//qDebug()<<"Open serial "<<m_strPortName << "successfully";
}
else {
//qDebug()<<"Open serial "<<m_strPortName << "failed";
//serialOpenRes(deviceId,false);//打开失败,则重新绑定
}
}
}
void TimingSysWorker::slot_readReady()
{
//先缓存
m_baBuff.append(ba);
//判断缓存中第一个字符是不是$,最后一个字符是不是换行符,如果二者皆成立表示收到了一个完整帧
if('$' == m_baBuff.at(0) && '\n' == m_baBuff.at(m_baBuff.size()-1))
{
emit sig_timingData(m_baBuff);
m_baBuff.clear();
}
}
void TimingSysWorker::slot_sendcmd(QByteArray ba)
{
m_pQSerialPort->write(ba);
m_pQSerialPort->flush();
}
边栏推荐
- STM32 - DMA direct memory access controller (cubemx configures DMA)
- How to delete and remove the first row of elements in PHP two-dimensional array
- ATT&CK初步了解
- flask之蓝图 补充openpyxl
- 770. Word replacement
- Ngx+sql environment offline installation log (RPM installation)
- OSV_ Q write divergence operator div and Laplace stepped on the pit
- Idea generate class diagram plug-in UML (super detailed)
- Leetcode question brushing series - sum of majority type
- 微信小程序里button点击的时候会边框有黑线
猜你喜欢

Solve Jupiter: the term 'Jupiter' is not recognized as the name of a cmdlet, function, script file

PC side web page special effects (offset series, obtain the coordinates of the mouse in the box, pop-up drag effect, magnifying glass effect)

Qt+ffmpeg environment construction
![[get mobile information] - get mobile information through ADB command](/img/ad/b10c5d09a21fb0cb22aa8a002fbd99.png)
[get mobile information] - get mobile information through ADB command

2022年一级建造师考试什么时候才能报名?

STM32 - memory, I2C protocol

【三维目标检测】3DSSD(一)

es个人整理的相关面试题

Use PCL to batch convert point cloud.Bin files to.Pcd

PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
随机推荐
Fastflow [abnormal detection: normalizing flow]
Concise history of graphic technology
PC side web page effects (client series, scroll series, immediate function execution, sidebar effects)
776. 字符串移位包含问题
STM32 - DMA direct memory access controller (cubemx configures DMA)
775. Inverted words
Mysql8.0 cannot authorize users or prompt you are not allowed to create a user with grant
Kali source solution software cannot be installed correctly
《结构学》介绍
MySQL installation and configuration (super detailed, simple and practical)
776. String shift inclusion problem
Summary of recent bugs
842. 排列数字
winServer运维技术栈
Command line agent: proxychains configuration
B站713故障后的多活容灾建设|TakinTalks大咖分享
Excel-vba quick start (XIII. Common usage of date)
Why doesn't the icon on the elment plus icon input display
PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
STM32CUBEIDE(10)----ADC在DMA模式下扫描多个通道