【Qt】modbus之串口模式读操作

00. 目录

01. 概述

Qt中几个常用的串口modbus类

QModbusRtuSerialSlave       //modbus串口通信方式下的服务器类
QModbusRtuSerialMaster      //串口通信方式下的客户端类
QModbusServer               // QModbusServer类接收和处理modbus的请求。
QModbusDataUnit             //存储接收和发送数据的类,数据类型为1bit和16bit
QModbusReply                //客户端访问服务器后得到的回复(如客户端读服务器数据时包含数据信息)

02. 开发环境

Windows系统:Windows10

Qt版本:Qt5.15或者Qt6

Pro配置文件如下

QT       += core gui serialbus serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \\
    main.cpp \\
    widget.cpp

HEADERS += \\
    widget.h

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

03. 读DiscreteInputs程序示例

widget.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

//前向声明
class QModbusClient;
class QModbusReply;


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();


private:
    QModbusClient *modbusDevice = nullptr;


private slots:
    void onReadReady();
};
#endif // WIDGET_H

widget.cpp文件

#include "widget.h"
#include <QModbusRtuSerialMaster>
#include <QModbusDataUnit>
#include <QModbusReply>
#include <QVariant>
#include <QSerialPort>
#include <QDebug>

//构造函数
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //1. 创建QModbusDevice对象
    modbusDevice = new QModbusRtuSerialMaster;

    //2. 如果处于连接状态,则断开连接
    if (modbusDevice->state() == QModbusDevice::ConnectedState)
    {
        //断开连接设备
        modbusDevice->disconnectDevice();
    }

    //3. 设置串口相关参数
    //设置串口信息
    modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, QVariant("COM3"));
    //设置校验 无校验
    modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
    //设置波特率
    modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, QSerialPort::Baud19200);
    //设置停止位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
    //设置数据位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);

    //4. 设置其他信息
    //设置超时时间
    modbusDevice->setTimeout(1000); //1秒
    //设置失败重试次数
    modbusDevice->setNumberOfRetries(3);

    //5. 连接到设备
    bool ok = modbusDevice->connectDevice();
    if (!ok)
    {
        qDebug() << "连接到串口失败: " << modbusDevice->errorString();
    }
    else
    {
        qDebug() << "连接到串口成功";
    }

    //6. 发送读取数据请求
    //从地址0开始读取10个保持寄存器的值
    //QModbusDataUnit data(QModbusDataUnit::HoldingRegisters, 0, 10);
    //从地址0开始读取10个离散输入量的值
    QModbusDataUnit data(QModbusDataUnit::DiscreteInputs, 0, 10);

    QModbusReply* reply = modbusDevice->sendReadRequest(data, 0x1);
    if (nullptr == reply)
    {
        qDebug() << "发送请求数据失败: " << modbusDevice->errorString();
    }
    else
    {
        if (!reply->isFinished())
        {
            connect(reply, &QModbusReply::finished, this, &Widget::onReadReady);
        }
        else
        {
            //broadcast replies return immediately
            delete reply;
        }
    }

    //7. 接收响应
    //通过槽函数实现

}


//析构函数
Widget::~Widget()
{
    if (modbusDevice)
    {
        modbusDevice->disconnectDevice();
    }

    delete modbusDevice;
}

//准备读取数据的槽函数
void Widget::onReadReady()
{
    auto reply = qobject_cast<QModbusReply*>(sender());
    if (nullptr == reply)
    {
        return;
    }

    //判断是否出错
    if (reply->error() == QModbusDevice::NoError)
    {
        //读取响应数据
        const QModbusDataUnit responseData = reply->result();

        qDebug() << responseData.values();

    }
    else if (reply->error() == QModbusDevice::ProtocolError)
    {
        qDebug() << "Read response Protocol error: " << reply->errorString();
    }
    else
    {
        qDebug() << "Read response Error: " << reply->errorString();
    }


    //删除reply
    reply->deleteLater();
}


执行结果

15:09:19: Starting D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe ...
连接到串口成功
QVector(0, 1, 0, 0, 1, 1, 0, 1, 1, 1)
15:09:22: D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe exited with code 0

04. 读Coils程序示例

widget.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

//前向声明
class QModbusClient;
class QModbusReply;


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();


private:
    QModbusClient *modbusDevice = nullptr;


private slots:
    void onReadReady();
};
#endif // WIDGET_H

widget.cpp文件

#include "widget.h"
#include <QModbusRtuSerialMaster>
#include <QModbusDataUnit>
#include <QModbusReply>
#include <QVariant>
#include <QSerialPort>
#include <QDebug>

//构造函数
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //1. 创建QModbusDevice对象
    modbusDevice = new QModbusRtuSerialMaster;

    //2. 如果处于连接状态,则断开连接
    if (modbusDevice->state() == QModbusDevice::ConnectedState)
    {
        //断开连接设备
        modbusDevice->disconnectDevice();
    }

    //3. 设置串口相关参数
    //设置串口信息
    modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, QVariant("COM3"));
    //设置校验 无校验
    modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
    //设置波特率
    modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, QSerialPort::Baud19200);
    //设置停止位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
    //设置数据位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);

    //4. 设置其他信息
    //设置超时时间
    modbusDevice->setTimeout(1000); //1秒
    //设置失败重试次数
    modbusDevice->setNumberOfRetries(3);

    //5. 连接到设备
    bool ok = modbusDevice->connectDevice();
    if (!ok)
    {
        qDebug() << "连接到串口失败: " << modbusDevice->errorString();
    }
    else
    {
        qDebug() << "连接到串口成功";
    }

    //6. 发送读取数据请求
    //从地址0开始读取10个保持寄存器的值
    //QModbusDataUnit data(QModbusDataUnit::HoldingRegisters, 0, 10);
    //从地址0开始读取10个离散输入量的值
    //QModbusDataUnit data(QModbusDataUnit::DiscreteInputs, 0, 10);

    //QModbusDataUnit::Coils 从地址0开始读取10个线圈值
    QModbusDataUnit data(QModbusDataUnit::Coils, 0, 10);


    QModbusReply* reply = modbusDevice->sendReadRequest(data, 0x1);
    if (nullptr == reply)
    {
        qDebug() << "发送请求数据失败: " << modbusDevice->errorString();
    }
    else
    {
        if (!reply->isFinished())
        {
            connect(reply, &QModbusReply::finished, this, &Widget::onReadReady);
        }
        else
        {
            //broadcast replies return immediately
            delete reply;
        }
    }

    //7. 接收响应
    //通过槽函数实现

}


//析构函数
Widget::~Widget()
{
    if (modbusDevice)
    {
        modbusDevice->disconnectDevice();
    }

    delete modbusDevice;
}

//准备读取数据的槽函数
void Widget::onReadReady()
{
    auto reply = qobject_cast<QModbusReply*>(sender());
    if (nullptr == reply)
    {
        return;
    }

    //判断是否出错
    if (reply->error() == QModbusDevice::NoError)
    {
        //读取响应数据
        const QModbusDataUnit responseData = reply->result();

        qDebug() << responseData.values();

    }
    else if (reply->error() == QModbusDevice::ProtocolError)
    {
        qDebug() << "Read response Protocol error: " << reply->errorString();
    }
    else
    {
        qDebug() << "Read response Error: " << reply->errorString();
    }


    //删除reply
    reply->deleteLater();
}


执行结果

15:14:35: Starting D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe ...
连接到串口成功
QVector(1, 0, 1, 1, 1, 0, 1, 1, 1, 1)
15:15:12: D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe exited with code 0

05. 读InputRegisters程序示例

widget.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

//前向声明
class QModbusClient;
class QModbusReply;


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();


private:
    QModbusClient *modbusDevice = nullptr;


private slots:
    void onReadReady();
};
#endif // WIDGET_H

widget.cpp文件

#include "widget.h"
#include <QModbusRtuSerialMaster>
#include <QModbusDataUnit>
#include <QModbusReply>
#include <QVariant>
#include <QSerialPort>
#include <QDebug>

//构造函数
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //1. 创建QModbusDevice对象
    modbusDevice = new QModbusRtuSerialMaster;

    //2. 如果处于连接状态,则断开连接
    if (modbusDevice->state() == QModbusDevice::ConnectedState)
    {
        //断开连接设备
        modbusDevice->disconnectDevice();
    }

    //3. 设置串口相关参数
    //设置串口信息
    modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, QVariant("COM3"));
    //设置校验 无校验
    modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
    //设置波特率
    modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, QSerialPort::Baud19200);
    //设置停止位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
    //设置数据位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);

    //4. 设置其他信息
    //设置超时时间
    modbusDevice->setTimeout(1000); //1秒
    //设置失败重试次数
    modbusDevice->setNumberOfRetries(3);

    //5. 连接到设备
    bool ok = modbusDevice->connectDevice();
    if (!ok)
    {
        qDebug() << "连接到串口失败: " << modbusDevice->errorString();
    }
    else
    {
        qDebug() << "连接到串口成功";
    }

    //6. 发送读取数据请求
    //从地址0开始读取10个保持寄存器的值
    //QModbusDataUnit data(QModbusDataUnit::HoldingRegisters, 0, 10);
    //从地址0开始读取10个离散输入量的值
    //QModbusDataUnit data(QModbusDataUnit::DiscreteInputs, 0, 10);

    //QModbusDataUnit::Coils 从地址0开始读取10个线圈值
    //QModbusDataUnit data(QModbusDataUnit::Coils, 0, 10);

    //QModbusDataUnit::InputRegisters 从地址0开始读取10个输入寄存器的值
    QModbusDataUnit data(QModbusDataUnit::InputRegisters, 0, 10);

    QModbusReply* reply = modbusDevice->sendReadRequest(data, 0x1);
    if (nullptr == reply)
    {
        qDebug() << "发送请求数据失败: " << modbusDevice->errorString();
    }
    else
    {
        if (!reply->isFinished())
        {
            connect(reply, &QModbusReply::finished, this, &Widget::onReadReady);
        }
        else
        {
            //broadcast replies return immediately
            delete reply;
        }
    }

    //7. 接收响应
    //通过槽函数实现

}


//析构函数
Widget::~Widget()
{
    if (modbusDevice)
    {
        modbusDevice->disconnectDevice();
    }

    delete modbusDevice;
}

//准备读取数据的槽函数
void Widget::onReadReady()
{
    auto reply = qobject_cast<QModbusReply*>(sender());
    if (nullptr == reply)
    {
        return;
    }

    //判断是否出错
    if (reply->error() == QModbusDevice::NoError)
    {
        //读取响应数据
        const QModbusDataUnit responseData = reply->result();

        qDebug() << responseData.values();

    }
    else if (reply->error() == QModbusDevice::ProtocolError)
    {
        qDebug() << "Read response Protocol error: " << reply->errorString();
    }
    else
    {
        qDebug() << "Read response Error: " << reply->errorString();
    }


    //删除reply
    reply->deleteLater();
}


执行结果

15:18:19: Starting D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe ...
连接到串口成功
QVector(10, 11, 12, 13, 14, 15, 16, 17, 18, 11)
15:18:22: D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe exited with code 0

06. 读HoldingRegisters程序示例

widget.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

//前向声明
class QModbusClient;
class QModbusReply;


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();


private:
    QModbusClient *modbusDevice = nullptr;


private slots:
    void onReadReady();
};
#endif // WIDGET_H

widget.cpp文件

#include "widget.h"
#include <QModbusRtuSerialMaster>
#include <QModbusDataUnit>
#include <QModbusReply>
#include <QVariant>
#include <QSerialPort>
#include <QDebug>

//构造函数
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    //1. 创建QModbusDevice对象
    modbusDevice = new QModbusRtuSerialMaster;

    //2. 如果处于连接状态,则断开连接
    if (modbusDevice->state() == QModbusDevice::ConnectedState)
    {
        //断开连接设备
        modbusDevice->disconnectDevice();
    }

    //3. 设置串口相关参数
    //设置串口信息
    modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter, QVariant("COM3"));
    //设置校验 无校验
    modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter, QSerialPort::NoParity);
    //设置波特率
    modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter, QSerialPort::Baud19200);
    //设置停止位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter, QSerialPort::OneStop);
    //设置数据位
    modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter, QSerialPort::Data8);

    //4. 设置其他信息
    //设置超时时间
    modbusDevice->setTimeout(1000); //1秒
    //设置失败重试次数
    modbusDevice->setNumberOfRetries(3);

    //5. 连接到设备
    bool ok = modbusDevice->connectDevice();
    if (!ok)
    {
        qDebug() << "连接到串口失败: " << modbusDevice->errorString();
    }
    else
    {
        qDebug() << "连接到串口成功";
    }

    //6. 发送读取数据请求
    //从地址0开始读取10个保持寄存器的值
    QModbusDataUnit data(QModbusDataUnit::HoldingRegisters, 0, 10);

    QModbusReply* reply = modbusDevice->sendReadRequest(data, 0x1);
    if (nullptr == reply)
    {
        qDebug() << "发送请求数据失败: " << modbusDevice->errorString();
    }
    else
    {
        if (!reply->isFinished())
        {
            connect(reply, &QModbusReply::finished, this, &Widget::onReadReady);
        }
        else
        {
            //broadcast replies return immediately
            delete reply;
        }
    }

    //7. 接收响应
    //通过槽函数实现

}


//析构函数
Widget::~Widget()
{
    if (modbusDevice)
    {
        modbusDevice->disconnectDevice();
    }

    delete modbusDevice;
}

//准备读取数据的槽函数
void Widget::onReadReady()
{
    auto reply = qobject_cast<QModbusReply*>(sender());
    if (nullptr == reply)
    {
        return;
    }

    //判断是否出错
    if (reply->error() == QModbusDevice::NoError)
    {
        //读取响应数据
        const QModbusDataUnit responseData = reply->result();

        qDebug() << responseData.values();

    }
    else if (reply->error() == QModbusDevice::ProtocolError)
    {
        qDebug() << "Read response Protocol error: " << reply->errorString();
    }
    else
    {
        qDebug() << "Read response Error: " << reply->errorString();
    }


    //删除reply
    reply->deleteLater();
}


执行结果

14:33:54: Starting D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe ...
连接到串口成功
QVector(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
14:33:57: D:\\ProgramData\\Qt\\build-Test-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\\debug\\Test.exe exited with code 0

07. 综合示例

程序界面
在这里插入图片描述

settingdialog.h文件

#ifndef SETTINGDIALOG_H
#define SETTINGDIALOG_H

#include <QDialog>
#include <QtSerialPort>

namespace Ui {
class SettingDialog;
}

//串口设置相关类
class SettingDialog : public QDialog
{
    Q_OBJECT

public:
    struct Settings
    {
        //串口名
        QString serialName = "COM3";
        //校验位
        int parity = QSerialPort::NoParity;
        //波特率
        int baud = QSerialPort::Baud19200;
        //数据位
        int dataBits = QSerialPort::Data8;
        //停止位
        int stopBits = QSerialPort::OneStop;

        //响应时间
        int responseTime = 1000;
        //重试次数
        int numberOfRetries = 3;
    };

    explicit SettingDialog(QWidget *parent = nullptr);
    ~SettingDialog();

    //返回参数设置信息
    Settings  settings() const;

private slots:
    void on_btnApply_clicked();

private:
    Ui::SettingDialog *ui;
    Settings m_settings;
};

#endif // SETTINGDIALOG_H

settingdialog.cpp文件

#include "settingdialog.h"
#include "ui_settingdialog.h"

//构造函数
SettingDialog::SettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingDialog)
{
    ui->setupUi(this);

    //设置默认参数信息
    ui->serialNameLineEdit->setText(tr("COM3"));
    ui->parityComboBox->setCurrentIndex(0);

    ui->baudComboBox->setCurrentText(QString::number(m_settings.baud));
    ui->dataBitComboBox->setCurrentText(QString::number(m_settings.dataBits));
    ui->stopBitComboBox->setCurrentText(QString::number(m_settings.stopBits));

    ui->spinBoxTimeOut->setValue(m_settings.responseTime);
    ui->spinBoxRetry->setValue(m_settings.numberOfRetries);

}

//析构函数
SettingDialog::~SettingDialog()
{
    delete ui;
}

//返回参数信息
SettingDialog::Settings SettingDialog::settings() const
{
    return m_settings;
}

//引用按钮槽函数
void SettingDialog::on_btnApply_clicked()
{
    m_settings.serialName = ui->serialNameLineEdit->text();
    m_settings.parity = ui->parityComboBox->currentText().toInt();
    m_settings.baud = ui->baudComboBox->currentText().toInt();
    m_settings.dataBits = ui->dataBitComboBox->currentText().toInt();
    m_settings.stopBits = ui->stopBitComboBox->currentText().toInt();

    m_settings.responseTime = ui->spinBoxTimeOut->value();
    m_settings.numberOfRetries = ui->spinBoxRetry->value();

    //隐藏参数设置对话框
    hide();
}

mainwindow.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QModbusDataUnit>

QT_BEGIN_NAMESPACE

namespace Ui
{
    class MainWindow;
}

class SettingDialog;
class QModbusClient;
class QModbusReply;


QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    //信号与槽进行关联
    void initActions();

    //读请求数据包封装
    QModbusDataUnit readRequest() const;

private slots:
    void onConnectButtonClicked();

    void onConnectTypeChanged(int);

    void onModbusStateChanged(int state);

    void onReadButtonClicked();
    void onReadReady();

private:
    Ui::MainWindow *ui = nullptr;

    SettingDialog *m_settingDialog = nullptr;

    QModbusClient *modbusDevice = nullptr;

    QModbusReply *reply = nullptr;

};
#endif // MAINWINDOW_H

mainwindow.cpp文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QModbusDataUnit>

QT_BEGIN_NAMESPACE

namespace Ui
{
    class MainWindow;
}

class SettingDialog;
class QModbusClient;
class QModbusReply;


QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    //信号与槽进行关联
    void initActions();

    //读请求数据包封装
    QModbusDataUnit readRequest() const;

private slots:
    void onConnectButtonClicked();

    void onConnectTypeChanged(int);

    void onModbusStateChanged(int state);

    void onReadButtonClicked();
    void onReadReady();

private:
    Ui::MainWindow *ui = nullptr;

    SettingDialog *m_settingDialog = nullptr;

    QModbusClient *modbusDevice = nullptr;

    QModbusReply *reply = nullptr;

};
#endif // MAINWINDOW_H

08. 程序下载

8.1 Qt RTU Master示例一.rar
8.2 RTUMasterTest(二).rar

09. 附录

9.1 Qt教程汇总
网址:https://dengjin.blog.csdn.net/article/details/115174639

© 版权声明
THE END
喜欢就支持一下吧
点赞219 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容