Qt下实现多线程的串口通信
class SerialPort : public QObject
{
Q_OBJECT
public:
explicit SerialPort(QObject *parent = NULL);
~SerialPort();
void init_port(); //初始化串口
public slots:
void handle_data(); //处理接收到的数据
void write_data(); //发送数据
signals:
//接收数据
void receive_data(QByteArray tmp);
private:
QThread *my_thread;
QSerialPort *port;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
serailport.cpp如下
#include "serialport.h"
SerialPort::SerialPort(QObject *parent) : QObject(parent)
{
my_thread = new QThread();
port = new QSerialPort();
init_port();
this->moveToThread(my_thread);
port->moveToThread(my_thread);
my_thread->start(); //启动线程
}
SerialPort::~SerialPort()
{
port->close();
port->deleteLater();
my_thread->quit();
my_thread->wait();
my_thread->deleteLater();
}
void SerialPort::init_port()
{
port->setPortName("/dev/ttyS1"); //串口名 windows下写作COM1
port->setBaudRate(38400); //波特率
port->setDataBits(QSerialPort::Data8); //数据位
port->setStopBits(QSerialPort::OneStop); //停止位
port->setParity(QSerialPort::NoParity); //奇偶校验
port->setFlowControl(QSerialPort::NoFlowControl); //流控制
if (port->open(QIODevice::ReadWrite))
{
qDebug() << “Port have been opened”;
}
else
{
qDebug() << “open it failed”;
}
connect(port, SIGNAL(readyRead()), this, SLOT(handle_data()), Qt::QueuedConnection); //Qt::DirectConnection
}
void SerialPort::handle_data()
{
QByteArray data = port->readAll();
qDebug() << QStringLiteral(“data received(收到的数据):”) << data;
qDebug() << “handing thread is:” << QThread::currentThreadId();
emit receive_data(data);
}
void SerialPort::write_data()
{
qDebug() << “write_id is:” << QThread::currentThreadId();
port->write(“data”, 4); //发送“data”字符
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
widget.h的调用代码
#include "serialport.h"
public slots:
void on_receive(QByteArray tmpdata);
private:
SerialPort *local_serial;
- 1
- 2
- 3
- 4
- 5
widget.cpp调用代码
//构造函数中
local_serial = new SerialPort();
connect(ui->pushButton, SIGNAL(clicked()), local_serial, SLOT(write_data()),Qt::QueuedConnection);
connect(local_serial, SIGNAL(receive_data(QByteArray)), this, SLOT(on_receive(QByteArray)), Qt::QueuedConnection);
//on_receive槽函数
void Widget::on_receive(QByteArray tmpdata)
{
ui->textEdit->append(tmpdata);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
写在最后
本文例子实现的串口号是 /dev/ttyS1(对应windows系统是COM1口),波特率38400,数据位8,停止位1,无校验位的串口通信。当然,使用串口程序前,需要在.pro文件中添加 QT += serialport,把串口模块加入程序。
</div><div><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">5</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/lusanshui">
<img src="https://profile.csdnimg.cn/0/7/5/3_lusanshui" class="avatar_pic" username="lusanshui">
<img src="https://g.csdnimg.cn/static/user-reg-year/2x/7.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/lusanshui" data-report-click="{"mod":"popu_379"}" target="_blank">lusanshui</a></span>
</div>
<div class="text"><span>发布了48 篇原创文章</span> · <span>获赞 47</span> · <span>访问量 8万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=lusanshui" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
</article>