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. 读Coils程序示例
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
//前向声明
class QModbusClient;
class QModbusReply;
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QModbusClient *modbusDevice = nullptr;
private slots:
void onReadReady();
void onModbusStateChanged(int state);
void on_btnConnect_clicked();
void on_btnCoil_clicked();
void on_btnDiscreteInputs_clicked();
void on_btnInputRegisters_clicked();
void on_btnHoldingRegisters_clicked();
};
#endif // WIDGET_H
widget.cpp文件
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QModbusTcpClient>
//构造函数
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//1. 创建QModbusDevice对象
modbusDevice = new QModbusTcpClient(this);
//禁用所有的读操作
ui->btnCoil->setEnabled(false);
ui->btnDiscreteInputs->setEnabled(false);
ui->btnHoldingRegisters->setEnabled(false);
ui->btnInputRegisters->setEnabled(false);
connect(modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error) {
qDebug() << "new Error: " << modbusDevice->errorString();
});
if (nullptr == modbusDevice)
{
qDebug() << "Cannot Create Modbus Client";
}
else
{
connect(modbusDevice, &QModbusClient::stateChanged,
this, &Widget::onModbusStateChanged);
}
}
//析构函数
Widget::~Widget()
{
if (modbusDevice)
{
modbusDevice->disconnectDevice();
}
delete modbusDevice;
delete ui;
}
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();
}
void Widget::onModbusStateChanged(int state)
{
//判断Modbus设备连接是否处于连接状态
if (state == QModbusDevice::UnconnectedState)
{
qDebug() << "TCP Client连接到Server 未连接";
ui->btnConnect->setText(tr("连接"));
ui->btnCoil->setEnabled(false);
ui->btnDiscreteInputs->setEnabled(false);
ui->btnHoldingRegisters->setEnabled(false);
ui->btnInputRegisters->setEnabled(false);
}
else if (state == QModbusDevice::ConnectingState)
{
qDebug() << "TCP Client正在连接Server";
}
else if (state == QModbusDevice::ConnectedState)
{
qDebug() << "TCP Client已经连接到Server";
ui->btnConnect->setText(tr("断开连接"));
ui->btnCoil->setEnabled(true);
ui->btnDiscreteInputs->setEnabled(true);
ui->btnHoldingRegisters->setEnabled(true);
ui->btnInputRegisters->setEnabled(true);
}
else if (state == QModbusDevice::ClosingState)
{
qDebug() << "设备已经被关闭";
}
}
//连接按钮槽函数
void Widget::on_btnConnect_clicked()
{
if (!modbusDevice)
{
return;
}
if (modbusDevice->state() != QModbusDevice::ConnectedState)
{
modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "127.0.0.1");
modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, 10086);
//设置超时时间
modbusDevice->setTimeout(1000); //1秒
//设置失败重试次数
modbusDevice->setNumberOfRetries(3);
//连接到服务端
bool ok = modbusDevice->connectDevice();
if (!ok)
{
qDebug() << "modbusDevice->connectDevice failed";
}
}
else
{
//断开连接
modbusDevice->disconnectDevice();
ui->btnConnect->setText(tr("连接"));
ui->btnCoil->setEnabled(false);
ui->btnDiscreteInputs->setEnabled(false);
ui->btnHoldingRegisters->setEnabled(false);
ui->btnInputRegisters->setEnabled(false);
}
}
//读线圈
void Widget::on_btnCoil_clicked()
{
//QModbusDataUnit::Coils 从地址0开始读取10个线圈值
QModbusDataUnit data(QModbusDataUnit::Coils, 0, 10);
auto 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;
}
}
}
//读离散输入
void Widget::on_btnDiscreteInputs_clicked()
{
}
//读输入寄存器
void Widget::on_btnInputRegisters_clicked()
{
}
//读保持寄存器
void Widget::on_btnHoldingRegisters_clicked()
{
}
执行结果
15:18:15: Starting D:\ProgramData\Qt\build-Test1-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\Test1.exe ...
TCP Client正在连接Server
TCP Client已经连接到Server
读到数据为: QVector(0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
设备已经被关闭
TCP Client连接到Server 未连接
15:18:36: D:\ProgramData\Qt\build-Test1-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\Test1.exe exited with code 0
04. 读DiscreteInputs程序示例
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
//前向声明
class QModbusClient;
class QModbusReply;
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QModbusClient *modbusDevice = nullptr;
private slots:
void onReadReady();
void onModbusStateChanged(int state);
void on_btnConnect_clicked();
void on_btnCoil_clicked();
void on_btnDiscreteInputs_clicked();
void on_btnInputRegisters_clicked();
void on_btnHoldingRegisters_clicked();
};
#endif // WIDGET_H
widget.cpp文件
//读离散输入
void Widget::on_btnDiscreteInputs_clicked()
{
//QModbusDataUnit::DiscreteInputs 从地址0开始读取10个离散输入值
QModbusDataUnit data(QModbusDataUnit::DiscreteInputs, 0, 10);
auto 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;
}
}
}
执行结果
15:25:14: Starting D:\ProgramData\Qt\build-Test1-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\Test1.exe ...
TCP Client正在连接Server
TCP Client已经连接到Server
读到数据为: QVector(1, 1, 1, 1, 1, 0, 0, 0, 0, 0)
设备已经被关闭
TCP Client连接到Server 未连接
05. 读InputRegisters程序示例
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
//前向声明
class QModbusClient;
class QModbusReply;
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QModbusClient *modbusDevice = nullptr;
private slots:
void onReadReady();
void onModbusStateChanged(int state);
void on_btnConnect_clicked();
void on_btnCoil_clicked();
void on_btnDiscreteInputs_clicked();
void on_btnInputRegisters_clicked();
void on_btnHoldingRegisters_clicked();
};
#endif // WIDGET_H
widget.cpp文件
//读输入寄存器
void Widget::on_btnInputRegisters_clicked()
{
//QModbusDataUnit::InputRegisters 从地址0开始读取10个输入寄存器的值
QModbusDataUnit data(QModbusDataUnit::InputRegisters, 0, 10);
auto 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;
}
}
}
执行结果
15:28:06: Starting D:\ProgramData\Qt\build-Test1-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\Test1.exe ...
TCP Client正在连接Server
TCP Client已经连接到Server
读到数据为: QVector(0, 12, 0, 11, 0, 0, 13, 0, 0, 0)
设备已经被关闭
TCP Client连接到Server 未连接
06. 读HoldingRegisters程序示例
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
//前向声明
class QModbusClient;
class QModbusReply;
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QModbusClient *modbusDevice = nullptr;
private slots:
void onReadReady();
void onModbusStateChanged(int state);
void on_btnConnect_clicked();
void on_btnCoil_clicked();
void on_btnDiscreteInputs_clicked();
void on_btnInputRegisters_clicked();
void on_btnHoldingRegisters_clicked();
};
#endif // WIDGET_H
widget.cpp文件
//读保持寄存器
void Widget::on_btnHoldingRegisters_clicked()
{
//从地址0开始读取10个保持寄存器的值
QModbusDataUnit data(QModbusDataUnit::HoldingRegisters, 0, 10);
auto 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;
}
}
}
执行结果
15:29:35: Starting D:\ProgramData\Qt\build-Test1-Desktop_Qt_5_15_2_MinGW_32_bit-Debug\debug\Test1.exe ...
TCP Client正在连接Server
TCP Client已经连接到Server
读到数据为: QVector(0, 0, 11, 12, 13, 14, 15, 0, 0, 111)
设备已经被关闭
TCP Client连接到Server 未连接
07. 综合示例
程序界面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-utd9gda4-1621496836516)(assets/image-20210520154331905.png)]
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文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include "settingdialog.h"
#include <QModbusRtuSerialMaster>
#include <QModbusReply>
#include <QStandardItemModel>
#include <QModbusDataUnit>
#include <QModbusTcpClient>
//连接类型枚举变量
enum ModbusConnection {
Serial,
Tcp
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//创建对象
m_settingDialog = new SettingDialog(this);
//初始化信号与槽
initActions();
//创建写模型
writeModel = new WriteRegisterModel(this);
writeModel->setStartAddress(ui->sbWriteStartAddr->value());
writeModel->setNumberOfValues(ui->cbWriteCount->currentText());
//MVC
ui->treeViewWrite->setModel(writeModel);
//隐藏第二列
ui->treeViewWrite->hideColumn(2);
connect(writeModel, &WriteRegisterModel::updateViewport,
ui->treeViewWrite->viewport(), QOverload<>::of(&QWidget::update));
//默认为串口连接方式
ui->cbConnType->setCurrentIndex(0);
onConnectTypeChanged(0);
auto model = new QStandardItemModel(10, 1, this);
for (int i = 0; i < 10; i++)
{
model->setItem(i, new QStandardItem(QStringLiteral("%1").arg(i + 1)));
}
ui->cbWriteCount->setModel(model);
ui->cbWriteCount->setCurrentText("10");
connect(ui->cbWriteCount, &QComboBox::currentTextChanged,
writeModel, &WriteRegisterModel::setNumberOfValues);
auto valueChanged = QOverload<int>::of(&QSpinBox::valueChanged);
connect(ui->sbWriteStartAddr, valueChanged, writeModel, &WriteRegisterModel::setStartAddress);
connect(ui->sbWriteStartAddr, valueChanged, this, [this, model](int i){
int lastIndex = 0;
const int curIndex = ui->cbWriteCount->currentIndex();
for (int j = 0; j < 10; j++)
{
//设置使能
if (j < (10 - i))
{
lastIndex = j;
model->item(j)->setEnabled(true);
}
else
{
//设置禁用
model->item(j)->setEnabled(false);
}
}
if (curIndex > lastIndex)
{
ui->cbWriteCount->setCurrentIndex(lastIndex);
}
});
}
//析构函数
MainWindow::~MainWindow()
{
if (modbusDevice)
{
modbusDevice->disconnectDevice();
delete modbusDevice;
}
delete ui;
}
//信号与槽进行关联s
void MainWindow::initActions()
{
//使能部分功能
ui->actionConnect->setEnabled(true);
ui->actionDisconnect->setEnabled(false);
ui->actionQuit->setEnabled(true);
ui->actionOption->setEnabled(true);
//禁用读写操作
ui->btnRead->setEnabled(false);
ui->btnWrite->setEnabled(false);
ui->btnReadWrite->setEnabled(false);
//信号与槽关联
connect(ui->btnConnect, &QPushButton::clicked,
this, &MainWindow::onConnectButtonClicked);
connect(ui->actionConnect, &QAction::triggered,
this, &MainWindow::onConnectButtonClicked);
connect(ui->actionDisconnect, &QAction::triggered,
this, &MainWindow::onConnectButtonClicked);
//读操作按钮槽函数关联
connect(ui->btnRead, &QPushButton::clicked,
this, &MainWindow::onReadButtonClicked);
connect(ui->btnWrite, &QPushButton::clicked,
this, &MainWindow::onWriteButtonClicked);
connect(ui->btnReadWrite, &QPushButton::clicked,
this, &MainWindow::onReadWriteButtonClicked);
connect(ui->cbRegisterType, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &MainWindow::onWriteTableChanged);
connect(ui->cbConnType, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &MainWindow::onConnectTypeChanged);
//退出菜单
connect(ui->actionQuit, &QAction::triggered, this, &MainWindow::close);
//显示参数设置对话框
connect(ui->actionOption, &QAction::triggered, m_settingDialog, &QDialog::show);
connect(ui->actionAbout, &QAction::triggered, [=]() {
QMessageBox::aboutQt(this, "About Qt");
});
}
//构建请求报文
QModbusDataUnit MainWindow::readRequest() const
{
//const auto type = static_cast<QModbusDataUnit::RegisterType>(ui->cbRegisterType->currentData().toInt());
QModbusDataUnit::RegisterType type = QModbusDataUnit::Invalid;
qDebug() << ui->cbRegisterType->currentText();
if (ui->cbRegisterType->currentText() == QString("线圈"))
{
type = QModbusDataUnit::Coils;
}
else if (ui->cbRegisterType->currentText() == QString("离散输入"))
{
type = QModbusDataUnit::DiscreteInputs;
}
else if (ui->cbRegisterType->currentText() == QString("输入寄存器"))
{
type = QModbusDataUnit::InputRegisters;
}
else if (ui->cbRegisterType->currentText() == QString("保持寄存器"))
{
type = QModbusDataUnit::HoldingRegisters;
}
qDebug() << "请求报文类型: " << type;
//获取
int startAddress = ui->spReadStartAddr->value();
Q_ASSERT(startAddress >= 0 && startAddress < 10);
quint16 numberOfEntries = ui->cbReadCount->currentText().toUShort();
return QModbusDataUnit(type, startAddress, numberOfEntries);
}
//写请求数据包封装
QModbusDataUnit MainWindow::writeRequest() const
{
QModbusDataUnit::RegisterType type = QModbusDataUnit::Invalid;
qDebug() << ui->cbRegisterType->currentText();
if (ui->cbRegisterType->currentText() == QString("线圈"))
{
type = QModbusDataUnit::Coils;
}
else if (ui->cbRegisterType->currentText() == QString("离散输入"))
{
type = QModbusDataUnit::DiscreteInputs;
}
else if (ui->cbRegisterType->currentText() == QString("输入寄存器"))
{
type = QModbusDataUnit::InputRegisters;
}
else if (ui->cbRegisterType->currentText() == QString("保持寄存器"))
{
type = QModbusDataUnit::HoldingRegisters;
}
qDebug() << "请求报文类型: " << type;
//获取
int startAddress = ui->sbWriteStartAddr->value();
Q_ASSERT(startAddress >= 0 && startAddress < 10);
quint16 numberOfEntries = ui->cbWriteCount->currentText().toUShort();
//qDebug() << "Test: " << startAddress << " " << numberOfEntries;
return QModbusDataUnit(type, startAddress, numberOfEntries);
}
//连接和断开连接的槽函数
void MainWindow::onConnectButtonClicked()
{
if (!modbusDevice)
{
return;
}
//清空状态栏消息
statusBar()->clearMessage();
if (modbusDevice->state() != QModbusDevice::ConnectedState)
{
auto type = static_cast<ModbusConnection>(ui->cbConnType->currentIndex());
if (type == Serial)
{
//设置串口连接信息
modbusDevice->setConnectionParameter(QModbusDevice::SerialPortNameParameter,
m_settingDialog->settings().serialName);
modbusDevice->setConnectionParameter(QModbusDevice::SerialParityParameter,
m_settingDialog->settings().parity);
modbusDevice->setConnectionParameter(QModbusDevice::SerialBaudRateParameter,
m_settingDialog->settings().baud);
modbusDevice->setConnectionParameter(QModbusDevice::SerialDataBitsParameter,
m_settingDialog->settings().dataBits);
modbusDevice->setConnectionParameter(QModbusDevice::SerialStopBitsParameter,
m_settingDialog->settings().stopBits);
}
else
{
//TCP连接信息
const QUrl url = QUrl::fromUserInput(ui->lineEditPort->text());
modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter,
url.port());
modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter,
url.host());
}
modbusDevice->setTimeout(m_settingDialog->settings().responseTime);
modbusDevice->setNumberOfRetries(m_settingDialog->settings().numberOfRetries);
if (!modbusDevice->connectDevice())
{
statusBar()->showMessage(tr("Connect failed..") + modbusDevice->errorString(), 5000);
}
else
{
statusBar()->showMessage(tr("Connect Successfully"), 5000);
qDebug() << "连接OK";
ui->actionConnect->setEnabled(false);
ui->actionDisconnect->setEnabled(true);
//使能读写操作
ui->btnRead->setEnabled(true);
ui->btnWrite->setEnabled(true);
ui->btnReadWrite->setEnabled(true);
}
}
else
{
//断开连接
modbusDevice->disconnectDevice();
ui->actionConnect->setEnabled(true);
ui->actionDisconnect->setDisabled(true);
qDebug() << "断开连接成功";
//禁用读写操作
ui->btnRead->setEnabled(false);
ui->btnWrite->setEnabled(false);
ui->btnReadWrite->setEnabled(false);
}
}
//连接类型槽函数 TCP Serial
void MainWindow::onConnectTypeChanged(int index)
{
//如果之前存在连接,则断开连接,然后释放内存
if(modbusDevice)
{
modbusDevice->disconnectDevice();
delete modbusDevice;
modbusDevice = nullptr;
}
auto type = static_cast<ModbusConnection>(index);
if (type == Serial)
{
modbusDevice = new QModbusRtuSerialMaster(this);
qDebug() << "new QModbusRtuSerialMaster Ok";
statusBar()->showMessage("new QModbusRtuSerialMaster Ok", 3000);
}
else if (type == Tcp)
{
modbusDevice = new QModbusTcpClient(this);
if (ui->lineEditPort->text().isEmpty())
ui->lineEditPort->setText(QLatin1String("127.0.0.1:10086"));
}
else
{
statusBar()->showMessage("连接类型非法", 5000);
}
connect(modbusDevice, &QModbusClient::errorOccurred, [this](QModbusDevice::Error){
statusBar()->showMessage(modbusDevice->errorString(), 5000);
});
if (!modbusDevice)
{
//分配空间失败
ui->btnConnect->setDisabled(true);
if (type == Serial)
{
statusBar()->showMessage(tr("创建Modbus Master失败"), 5000);
}
else
{
statusBar()->showMessage(tr("创建Modbus Client失败"), 5000);
}
}
else
{
connect(modbusDevice, &QModbusClient::stateChanged,
this, &MainWindow::onModbusStateChanged);
}
}
//Modbus状态改变槽函数
void MainWindow::onModbusStateChanged(int state)
{
//判断Modbus设备连接是否处于连接状态
bool connected = (state != QModbusDevice::UnconnectedState);
ui->actionConnect->setEnabled(!connected);
ui->actionDisconnect->setEnabled(connected);
if (QModbusDevice::UnconnectedState == state)
{
ui->btnConnect->setText(tr("Connect"));
}
else
{
ui->btnConnect->setText(tr("Disconnect"));
}
}
//读操作槽函数
void MainWindow::onReadButtonClicked()
{
if (!modbusDevice)
{
return;
}
ui->textEditRead->clear();
statusBar()->clearMessage();
//发送请求报文数据
auto *reply = modbusDevice->sendReadRequest(readRequest(), ui->sbServerAddr->value());
if (reply)
{
if (!reply->isFinished())
{
//完毕之后 自动触发槽函数
connect(reply, &QModbusReply::finished, this, &MainWindow::onReadReady);
}
else
{
//广播消息 不需要返回响应
delete reply;
}
}
else
{
statusBar()->showMessage(tr("Read Error: ") + modbusDevice->errorString(), 5000);
}
}
//读取数据
void MainWindow::onReadReady()
{
auto reply = qobject_cast<QModbusReply*>(sender());
if (!reply)
{
return;
}
if (reply->error() == QModbusDevice::NoError)
{
const QModbusDataUnit data = reply->result();
for (int i = 0, total = (int)data.valueCount(); i < total; i++)
{
const QString str = tr("Address: %1 Value: %2").arg(data.startAddress() + i)
.arg(QString::number(data.value(i), data.registerType() <= QModbusDataUnit::Coils ? 10 : 16));
ui->textEditRead->append(str);
}
}
else if (reply->error() == QModbusDevice::ProtocolError)
{
statusBar()->showMessage(tr("Read response error: %1 (Modbus exception: 0x%2)").
arg(reply->errorString()).
arg(reply->rawResult().exceptionCode(), -1, 16), 5000);
}
else
{
statusBar()->showMessage(tr("Read response error: %1 (Code: 0x%2)").
arg(reply->errorString()).
arg(reply->error(), -1, 16), 5000);
}
//释放内存
reply->deleteLater();
}
void MainWindow::onWriteButtonClicked()
{
if (!modbusDevice)
{
return;
}
statusBar()->clearMessage();
QModbusDataUnit writeData = writeRequest();
QModbusDataUnit::RegisterType type = writeData.registerType();
//qDebug() << "test: " << writeData.valueCount();
for (int i = 0, total = (int)(writeData.valueCount()); i < total; i++)
{
if (type == QModbusDataUnit::Coils)
{
writeData.setValue(i, writeModel->m_coils[i + writeData.startAddress()]);
}
else
{
//qDebug() << "test: " << writeModel->m_holdingRegisters[i + writeData.startAddress()];
writeData.setValue(i, writeModel->m_holdingRegisters[i + writeData.startAddress()]);
}
}
qDebug() << "写数据内容为:" << writeData.values();
//发送请求报文数据
auto *reply = modbusDevice->sendWriteRequest(writeData, ui->sbServerAddr->value());
if (reply)
{
if (!reply->isFinished())
{
//完毕之后 自动触发槽函数
connect(reply, &QModbusReply::finished, this, [this, reply]{
if (reply->error() == QModbusDevice::ProtocolError)
{
statusBar()->showMessage(tr("Write Protocaol response error: %1").arg(reply->errorString()), 5000);
}
else if (reply->error() != QModbusDevice::NoError)
{
statusBar()->showMessage(tr("Write response error: %1").arg(reply->errorString()), 5000);
}
else
{
qDebug() << "写响应的数据: " << reply->result().values();
}
reply->deleteLater();
});
}
else
{
//广播消息 不需要返回响应
reply->deleteLater();
}
}
else
{
statusBar()->showMessage(tr("Write Error: ") + modbusDevice->errorString(), 5000);
}
}
//读写按钮槽函数
void MainWindow::onReadWriteButtonClicked()
{
if (!modbusDevice)
{
return;
}
statusBar()->clearMessage();
QModbusDataUnit writeData = writeRequest();
QModbusDataUnit::RegisterType type = writeData.registerType();
//qDebug() << "test: " << writeData.valueCount();
for (int i = 0, total = (int)(writeData.valueCount()); i < total; i++)
{
if (type == QModbusDataUnit::Coils)
{
writeData.setValue(i, writeModel->m_coils[i + writeData.startAddress()]);
}
else
{
//qDebug() << "test: " << writeModel->m_holdingRegisters[i + writeData.startAddress()];
writeData.setValue(i, writeModel->m_holdingRegisters[i + writeData.startAddress()]);
}
}
qDebug() << "写数据内容为:" << writeData.values();
//发送请求报文数据
auto *reply = modbusDevice->sendReadWriteRequest(readRequest(), writeData, ui->sbServerAddr->value());
if (reply)
{
if (!reply->isFinished())
{
connect(reply, &QModbusReply::finished, this, &MainWindow::onReadReady);
//完毕之后 自动触发槽函数
connect(reply, &QModbusReply::finished, this, [this, reply]{
if (reply->error() == QModbusDevice::ProtocolError)
{
statusBar()->showMessage(tr("Write Protocaol response error: %1").arg(reply->errorString()), 5000);
}
else if (reply->error() != QModbusDevice::NoError)
{
statusBar()->showMessage(tr("Write response error: %1").arg(reply->errorString()), 5000);
}
else
{
qDebug() << "写响应的数据: " << reply->result().values();
}
reply->deleteLater();
});
}
else
{
//广播消息 不需要返回响应
reply->deleteLater();
}
}
else
{
statusBar()->showMessage(tr("Write Error: ") + modbusDevice->errorString(), 5000);
}
}
void MainWindow::onWriteTableChanged(int index)
{
const bool coilsOrHolding = index == 0 || index == 3;
if (coilsOrHolding)
{
ui->treeViewWrite->setColumnHidden(1, index != 0);
ui->treeViewWrite->setColumnHidden(2, index != 3);
ui->treeViewWrite->resizeColumnToContents(0);
}
ui->btnReadWrite->setEnabled(index == 3);
ui->btnWrite->setEnabled(coilsOrHolding);
ui->groupBox_2->setEnabled(coilsOrHolding);
}
08. 程序下载
09. 附录
9.1 Qt教程汇总
网址:javascript:void(0)