Qt下实现多线程串口通信

155 篇文章 25 订阅

Qt下实现多线程串口通信

Qt下无论是RS232RS422RS485的串口通信都可以使用统一的编码实现。本文把每路串口的通信各放在一个线程中,使用movetoThread的方式实现。

SerialPort类实现串口功能,Widget类调用串口。

serialport.h如下:

#include <QObject>

#include <QSerialPort>

#include <QString>

#include <QByteArray>

#include <QObject>

#include <QDebug>

#include <QObject>

#include <QThread>

 

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;

};

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”字符

}

widget.h的调用代码

#include "serialport.h"

public slots:

  void on_receive(QByteArray tmpdata);

private:

  SerialPort *local_serial;

widget.cpp调用代码

//构造函数中

local_serial = new QSerialPort();

connect(ui->pushButton, SIGNAL(clicked()), local_serial, SLOT(write_data()));

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);

}

本文例子实现的串口号是 /dev/ttyS1(对应windows系统是COM1口),波特率38400,数据位8,停止位1,无校验位的串口通信。当然,使用串口程序前,需要在.pro文件中添加 QT += serialport,把串口模块加入程序。

  • 3
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值