案例分享:Qt modbus485除錯工具(讀寫Byte、Int、DInt、Real、DReal)(當前v1.3.0)

語言: CN / TW / HK

若該文為原創文章,轉載請註明原文出處 本文章部落格地址:https://blog.csdn.net/qq21497936/article/details/117398968

紅胖子(紅模仿)的博文大全:開發技術集合(包含Qt實用技術、樹莓派、三維、OpenCV、OpenGL、ffmpeg、OSG、微控制器、軟硬結合等等)持續更新中...(點選傳送門)

合作案例專欄:案例分享(體驗Demo可下載,只定制)

<br>

前言

  西門子PLC、臺達PLC、法蘭克機床等等多年以前玩得比較多,有tcp/ip通訊也有modbus通訊,modbus又分為網路,485,232等。   醫療專案,焊接機器人專案,工控機床專案,數控專案,物聯網專案用的挺多的,將關鍵的通訊技術抽離出來,單獨弄成工具。

<br>

需求

  與各種其他行業儀器通訊軟體開發的通訊核心模組,modbus功能模組,製作成除錯工具,可滿足一般情況的除錯。   1.支援通過串列埠232,485埠進行通訊;   2.支援設定開啟埠的引數;   3.支援寫入bool,int,dInt,real,dReal引數型別資料;   4.支援讀取bool,int,dInt,real,dReal引數型別資料;   5.增加10種主題風格切換(僅用於預選擇主題風格);

<br>

Demo

  在這裡插入圖片描述   在這裡插入圖片描述   在這裡插入圖片描述   在這裡插入圖片描述

<br>

工具下載地址

  CSDN(0積分下載):https://download.csdn.net/download/qq21497936/19236703 QQ群:1047134658(點選“檔案”搜尋“plcModbus ”,群內與博文同步更新)

<br>

十種主題風格切換

  在這裡插入圖片描述

<br>

相關標頭檔案原始碼

PlcModbusMasterWidget.h

在這裡插入圖片描述

#ifndef PLCMODBUSMASTERWIDGET_H
#define PLCMODBUSMASTERWIDGET_H

#include <QWidget>
#include <QThread>
#include <QDateTime>
#include <QElapsedTimer>
#include <QHash>
#include <QFile>
#include <QMessageBox>
#include <QAbstractButton>
#include "ModbusManager.h"

namespace Ui {
class PlcModbusMasterWidget;
}

class PlcModbusMasterWidget : public QWidget
{
    Q_OBJECT

public:
    explicit PlcModbusMasterWidget(QWidget *parent = 0);
    ~PlcModbusMasterWidget();

protected slots:
    void slot_opened(bool opened);
    void slot_readBitsResult(bool result, int startAddr, QList<bool> listBool);
    void slot_readIntsResult(bool result, int startAddr, QList<int> listInt);
    void slot_readDIntsResult(bool result, int startAddr, QList<int> listInt);
    void slot_readRealsResult(bool result, int startAddr, QList<float> listFloat);
    void slot_readDRealsResult(bool result, int startAddr, QList<double> listDouble);

    void slot_writeBitsResult(bool result, int startAddr, QList<bool> listBool);
    void slot_writeIntsResult(bool result, int startAddr, QList<int> listInt);
    void slot_writeDIntsResult(bool result, int startAddr, QList<int> listInt);
    void slot_writeRealsResult(bool result, int startAddr, QList<float> listFloat);
    void slot_writeDRealsResult(bool result, int startAddr, QList<double> listDouble);

private slots:
    void on_comboBox_writeType_currentIndexChanged(int index);

    void on_lineEdit_readStartAddr_textChanged(const QString &arg1);
    void on_lineEdit_writeStartAddr_textChanged(const QString &arg1);
    void on_lineEdit_readStartAddrHex_textChanged(const QString &arg1);
    void on_lineEdit_writeStartAddrHex_textChanged(const QString &arg1);

protected slots:
    void slot_buttonClicked(int index);

private slots:
    void on_pushButton_open_clicked();
    void on_pushButton_close_clicked();
    void on_pushButton_read_clicked();
    void on_pushButton_write_clicked();
    void on_pushButton_clearM_clicked();

private:
    Ui::PlcModbusMasterWidget *ui;

    QThread *_pModbusManagerThread;
    QElapsedTimer _elapsedTimer;

    QHash<int, QString> _hashInt2Str;
};

#endif // PLCMODBUSMASTERWIDGET_H

在這裡插入圖片描述

ModbusManager.h

#ifndef MODBUSMANAGER_H
#define MODBUSMANAGER_H

#include <QObject>
#include <QMutex>

class ModbusManager : public QObject
{
    Q_OBJECT
private:
    explicit ModbusManager(QObject *parent = 0);

public:
    bool testEnvAndRtu();

public:
    static ModbusManager *getInstance();

public:
    void setPortName(const QString &portName);
    void setBaud(int baud);
    void setParity(char parity);
    void setDataBit(int dataBit);
    void setStopBit(int stopBit);
    void setTimeOutMs(int timeOutMs);
    void setSlaveAddr(int slaveAddr);

public:
    QString getPortName() const;
    int getBaud() const;
    char getParity() const;
    int getDataBit() const;
    int getStopBit() const;
    int getTimeOutMs() const;
    int getSlaveAddr() const;

signals:
    void signal_opened(bool opened);

    void signal_readBitsResult(bool result, int startAddr, QList<bool> listBool);
    void signal_readIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_readDIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_readRealsResult(bool result, int startAddr, QList<float> listFloat);
    void signal_readDRealsResult(bool result, int startAddr, QList<double> listDouble);

    void signal_writeBitsResult(bool result, int startAddr, QList<bool> listBool);
    void signal_writeIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_writeDIntsResult(bool result, int startAddr, QList<int> listInt);
    void signal_writeRealsResult(bool result, int startAddr, QList<float> listFloat);
    void signal_writeDRealsResult(bool result, int startAddr, QList<double> listDouble);

public slots:
    void slot_start();
    void slot_stop();
    void slot_open();
    void slot_close();

    void slot_readBits(int startAddr, int size);
    void slot_readInts(int startAddr, int size);
    void slot_readDInts(int startAddr, int size);
    void slot_readReals(int startAddr, int size);
    void slot_readDReals(int startAddr, int size);

    void slot_writeBits(int startAddr, QList<bool> listBool);
    void slot_writeInts(int startAddr, QList<int> listInt);
    void slot_writeDInts(int startAddr, QList<int> listInt);
    void slot_writeReals(int startAddr, QList<float> listFloat);
    void slot_writeDReals(int startAddr, QList<double> listDouble);

private:
    static ModbusManager *_pInstance;
    static QMutex _mutex;

private:
    QString _portName;
    int _baud;
    char _parity;
    int _dataBit;
    int _stopBit;
    int _timeOutMs;
    int _slaveAddr;

private:
    bool _running;
    bool _opened;
};

#endif // MODBUSMANAGER_H

<br>

若該文為原創文章,轉載請註明原文出處 本文章部落格地址:https://blog.csdn.net/qq21497936/article/details/117398968

分享到:
「其他文章」