当前位置:网站首页>轮子六: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();
}
边栏推荐
- Research cup element recognition multi label classification task based on ernie-3.0 cail2019 method
- WinForm jump to the second form case
- How to delete and remove the first row of elements in PHP two-dimensional array
- [virtual machine _2]-hyper-v and vmware/virtualbox cannot coexist
- Labelme labels circular objects [tips]
- OSV_ q AttributeError: ‘numpy. ndarray‘ object has no attribute ‘clone‘
- OSV-q ValueError: axes don‘t match array
- ATT&CK初步了解
- Pictures are named in batches in order (change size /jpg to PNG) [tips]
- 2022年一级建造师考试什么时候才能报名?
猜你喜欢
随机推荐
从 IPv4 向 IPv6 的迁移
Winserver operation and maintenance technology stack
How do we do full link grayscale on the database?
JS获取当前时间(年月日时分秒)
es个人整理的相关面试题
微信小程序剪切图片的功能
842. Arrange numbers
Log4j vulnerability elk platform processing method (logstah5.5.1)
2022年一级建造师考试什么时候才能报名?
WinForm jump to the second form case
STM32 - external interrupt application (exti) (use cubemx to configure interrupts)
imx6q gpio复用
Find out the maximum value of all indicators in epoch [tips]
Padim [anomaly detection: embedded based]
hp proliant dl380从U盘启动按哪个键
For loops and functions
Install PCL and VTK under the background of ROS installation, and solve VTK and PCL_ ROS conflict problem
Closure, prototype and original link
Stm32subeide (10) -- ADC scans multiple channels in DMA mode
Redis related









